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
Related
I'm a newcomer in this, and I'm learning kotlin in android studio, and I'm stacked in this error, "None of the following functions can be called with the arguments supplied" when I want to concatenate a variable + string + variable the error appears, and when delete the first variable and the plus signal the error disappears, Could anyone help me whit this? this is the part of the code
/**
* This method is called when the order button is clicked.
*/
fun submitOrder(view: View?) {
var price = quantity * 5
var priceMessage = price + "dollars for " + quantity + " of coffee. Pay up"
displayMessage(priceMessage)
}
Try String Templates in Kotlin
String templates allow you to include variable references and expressions into strings. When the value of a string is requested, all references and expressions are substituted with actual values.
Take this kotlin example. https://play.kotlinlang.org/byExample/08_productivity_boosters/02_String%20Templates
fun submitOrder(view: View?) {
var price = quantity * 5
*// Templates try like this*
var priceMessage = "$price dollars for $quantity of
coffee. Pay up"
(or)
*//(you can execute command inside like this)*
var priceMessage = "${String.format("%.3f", price)} dollars
for ${quantity+1} of coffee. Pay up"
displayMessage(priceMessage)
}
price + "dollars for " won't work because you can't add a string to a number. If you want to concatenate them all, you can do like this using string templates:
val priceMessage = "$price dollars for $quantity of coffee. Pay up"
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.
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
I am trying to reference a string stored in a .xml file, and every time i reference it I just get a string of numbers.
if (Medals.medal_counter1 == 0) {
Medals.medal_counter1++;
victory.setMessage("you won " + R.string.medal01);
}
victory.show();
Here is the stored string.
<string name="medal01">"A medal"</string>
The dialog I actually get is something more like this,
"you won 21310334567"
Any solutions?
That's because you are getting the string resource's ID. If you want to retrieve the actual string content you need to use Context.getString(int resourceId) method.
For instance, from inside an Activity:
victory.setMessage("you won " + getString(R.string.medal01));
Otherwise:
victory.setMessage("you won " + victory.getContext().getString(R.string.medal01));
Try this
getResources().getString(R.string.medal01);
- What you are referring to using R.string.medal01 is an public static final integer value in R.java file.
- Use below instead:
victory.setMessage("you won " + getString(R.string.medal01));