Recently we moved to IntelliJ and using it for JAVA, earlier we were using Eclipse, we are able to configure the IntelliJ code format setting same as Eclipse except for toString().
toString() output in POJO is very different from eclipse as shown below,
toString() in IntelliJ
#Override
public String toString() {
return "Entity{" +
"entityName='" + entityName + '\'' +
", isBaseEntity=" + isBaseEntity +
", entityAttribute=" + entityAttribute +
'}';
}
toString() in Eclipse
#Override
public String toString() {
return "Entity [entityName=" + entityName + ", isBaseEntity=" + isBaseEntity
+ ", entityAttribute=" + entityAttribute;
}
Question
Is there any way/setting through which I can make IntelliJ toString() same as eclipse?
Code > Generate... > toString()
On the dialog that opens, in the top right, there is a button labelled 'Settings...'. You can specify a template. There are 11 default templates. You can add your own to generate whatever you want.
Better yet, use Lombok to generate your toString. It's opinionated about the format, but I much prefer that to having my code littered with boilerplate. It also automatically updates if you add a field to the class, which an IDE generated method will not do.
Related
According to the following question and response:
How find out static string result concatenation in Intellij Idea without running code?
it is possible to copy result of the String concatenation from IntelliJ into the clipboard.
Let me cite:
just put cursor on the string concatenation
Press alt + enter
Select "Copy String concatenation text into clipboard"
Paste result somewhere.
However, I find out that this solution does not work in case of the String.format.
Is there any workaround or plugin for the IntelliJ that allows to perform this operation?
public class Main {
public static void main(String[] args) {
final String one = "want";
final String two = "copy";
final String three = "concatenated string";
// I want to copy whole concatenated string
String canBeCopiedIntoClipBoard = "I " + one + " to " + two + " whole " + three;
String cannotCopyResultIntoClipBoard = String.format("I %s to %s whole %s", one, two, three);
assert canBeCopiedIntoClipBoard.equals(cannotCopyResultIntoClipBoard);
System.out.println(canBeCopiedIntoClipBoard);
}
}
It cannot be done directly from the IDE because of the simple fact that the variables used in the construction of the result may come from anywhere and may not be known at compile-time, as well as intricacies of the implementation of whichever external class you invoke (think of environment variables that may play a role).
You can do it if you can run to the point of the construction of the result.
Put a breakpoint at the line of 'cannotCopyResultIntoClipBoard' and start debug,
Select the part that generates the string,
Open to the 'Evaluate' panel (Sometimes Alt+F8, always Run→Evaluate Expression…),
Press enter,
In the result panel, right click, Copy Value.
I have made a class and want to call getTicketsCollection() and have it display the output below to the console.
My question is how do i make it print the listand not print the addresses(?)
Here is the full method
public List<TicketInfo> getTicketsCollection() {
Tickets Ticket = new Tickets();
List<TicketInfo> tickets = Ticket.getTicketCollection();
System.out.println("Company name: " + nextTicket.getCompanyName() + " Symbol: " + nextTicket.getCompanySymbol()
}
return tickets_today;
}
and prints this to the console (class output tab):
[WSpackageWS.TicketInfo#177bea38, WSpackageWS.TicketInfo#7f132176, WSpackageWS.TicketInfo#6bca7e0d]
My question is how do i make it print the list (the stuff in the
server tab) to the class output tab (and not print the addresses(?)
It is very obvious, you are getting below output in server console tab, because that code is executed at server and your are printing each member of TicketInfo class.
Info:
Company name: Microsoft Symbol: Frog Currency: GBP Available tickets: 625
Value per share: 500 DateOLU: 04/04/2017
This isn't happening client side code, because you are trying to print the object without overriding toString.
So, there are couple of things :
You need to show the implementation of getSharesCollection() method, which seems to be returning List, but can't tell until you share the implementation.
Override toString () method by adding following code
public String toString ()
{
return "Company name: " + nextTicket.getCompanyName() + " Symbol: " + nextTicket.getCompanySymbol() + " Currency: " + nextTicket.getCurrency()
+ " Available shares: " + getAvailableShares() + " Value per share: " + getValuePerShare()
+ " DateOLU: " + getDateOLU();
}
Where you use System.out.println(getSharesCollection()); you are calling the toString() method on the List.
This is what gives you the [WSpackageWS.TicketInfo#177bea38, ...] output. The #177bea38 is a hashcode which is the default toString() implementation (for your TicketInfo class).
In order to display the output properly you need to extract the data needed from your TicketInfo class.
The best way to do that is to implement (and Override) the toString() method in TicketInfo. You can use the lines from the System.out.println in your getTicketsCollection() method to do this.
Write now the hash code of the objects is getting printed, because System.out.println(object) calls the toString() of the class whose object you want to print. By default, the toString() of the java.lang.Object class is called. To change that, override the toString() of the class whose object you want to print..
Hope it helps..
I have a blank text field which I'm trying to use settext on after a button click, so I can change the blank text field to the text in the stored variables.
The variables are two ints which I've converted to string and I'm trying to do the following:
blankText.setText("" + var1);
currently works but when I try to add the other variable in the same field I'm not sure how to go about it?
For example I tried to do: blankText.setText("" + var1, var2) which throws an error. I want them listed side by side in the same text field. is this possible?
To combine Strings, use +:
blankText.setText(var1 + var2);
blankText.setText(var1 + " " + var2);
blankText.setText("" + var1 + " " + var2);
if you want to space your variables
If you are trying to add or append the text in already written textview then use
blankText.append(""+var2);
if you want to write two variables in textview at the same time then use the concatination symbol
blankText.setText(" " + var1 + " " + var2); // also will remove the previous written text
How would I go about renaming a variable in only part of the code?
For example:
System.out.println("Rectangle 1: " + "\n" + "Width: " + r1.width + "\n" + "Height: " +
r1.height + "\n" + "Color: " + r1.color + "\n" + "Area and Perimeter: " +
r1.getArea(r1.width, r1.height) + ", " + r1.getPerimeter(r1.width, r1.height));
So if I want to type out the same for a second rectangle using r2 as the refVar, is there a way I can quickly do this? I tried copy and pasting then using Alt + Shift + R, but, it ends up changing all of the r1 refvars.
I suggest you to use Find/Replace dialog for this problem it suites for your need. Select a set of statements then press Ctrl + F. A Find/Replace dialog will popup, note that in Scope group Selected lines option is selected.
You can use Replace All or Replace/Find. But be careful that it also replaces the string in comments if found.
Refer picture below.
Take a look at don't repeat yourself (DRY) principle which was designed to prevent situations like yours. Instead of renaming variables create separate method which will accept any Rectangle and print its details.
public static void printRectangleDetails(Rectangle r){
System.out.println("Rectangle 1: " + "\n" + "Width: " + r.width + "\n" + "Height: " +
r.height + "\n" + "Color: " + r.color + "\n" + "Area and Perimeter: " +
r.getArea(r.width, r.height) + ", " + r.getPerimeter(r.width, r.height));
}
Now you can use it with your r1 and r2 rectangles when needed
printRectangleDetails(r1);
...
printRectangleDetails(r2);
If for some reason where you can't create separate method and use DRY principle you can do something like this:
lets say we have
String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);
String bar = "bar";
System.out.println(foo+" hello wordls"+ foo);
and you want to replace foo in second print statement to bar. Using Alt + Shift + R (or from menu: Refactor -> Rename..) on second printing statement would rename all foo references . To prevent it redeclare your foo reference (compiler will give you error, but don't worry, we will later remove it, it is helpful only while renaming process) just before statements from which you want to change foo to bar like
String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);
String bar = "bar";
String foo = "whatever";
// ^^^
System.out.println(foo+" hello wordls"+ foo);
Now use Alt + Shift + R on this new duplicate foo and eclipse will look for foo from this new reference, and ignore earlier foos, so you should be able to see something like
(as you can see first two foo are not selected for renaming)
so you can change it to bar like
String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);
String bar = "bar";
String bar = "whatever";
System.out.println(bar+" hello wordls"+ bar);
and after that just removed this additional String bar = "whatever"; since you don't need it any more.
BUT BE CAREFUL. This way you will rename all foo variables after duplicate foo, not only those in System.out.println(foo+" hello wordls"+ foo); which you wanted to rename. To make sure you are not changing anything you don't want to, place code you want to change at the end of your method (where you are sure there is no foo which shouldn't be changed after it). After you are done move your changed code to place you want.
Can I change description of variable?
I want to see my own string at this place. I want generate this string by myself.
For example
"RTKAccount number=111 and FGSFDS"
insead of
"RTKAccount (id=830039244504)".
I tried to change toString() method in my class, but it did not work.
public String toString() {
return "RTKAccount id=" + this.id + " number=" + this.number;
}
What you're searching for are the detail formatters.
Right-click on the variable in the Variable view and select "New Detail Formatter...". In the wizard type this into the big text area:
"RTKAccount id=" + id + " number=" + number
And there you have it ;-)
You can change the value of a variable by right clicking it -> Change value