Java Android, listing multiple stored values in settext - java

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

Related

Change variable in println

Can I calculate and print sum in string println without creating new variable? Something like this
System.out.println("Hello world " + "value+1");
Yes you can, but you need to wrap the value and the added number in to parentheses, otherwise it will concatenate them like they were string characters. Loose the quotetions around value.
System.out.println("Hello world" + (value +1));
if you actually want quotetions around the value, you need to escape the extra quotetions with the \ char.
System.out.println("Hello world" + "\"(value +1)\"");;
And if you want to add numbers without concatenation you can use the String.format
System.out.println(String.format("Hello world %d", value +1));
Yep.
System.out.println("Hello world " + (variable + 1));

How do you format and print multiple data types in single statement using `println`? [duplicate]

This question already has answers here:
How can I print this 2 Variables in the same println "System.out.println"
(7 answers)
Closed 3 years ago.
So basically I'm trying to complete a school problem in which I have to declare two variables:
I want to print them in a single statement, but I don't know how.
I have tried using commas, and even a plus sign to separate the different variables but I'm always getting an error.
I did look online, but I guess you can only combine strings and ints?
Boolean isTrue = false;
Double money = 99999.99;
System.out.println(money + isTrue);
I expected it to print:
99999.99 false
Try this.
Inside of a System.out.println() method each variable calls its string representation to print. But if its a primitive datatype--> then we need to call its to string method to print its exact value.
You can concatenate with .toString() method
public static void main(String[] args) {
Boolean isTrue = false;
Double money = 99999.99;
System.out.println(money + isTrue.toString());
}
This is because println will automatically convert its input to a string for printing. Any logic inside of the "()" will be performed first, before this type conversion. Since you have 2 different value types that can't normally be added together, this logic fails. You will need to convert all of your values to a string to be able to use the "+" as a concatenation.
Alternatively, consider looking at using StringBuilder or StringFormat
Try this way:
System.out.println(money + " " + (isTrue ? "True" : "False"));
or simply:
System.out.println(money + " " + isTrue);
Concatenate them with string:
Boolean isTrue = false;
Double money = 99999.99;
System.out.println(money + " " + isTrue);

Java System.out.println syntax help for multiple variables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm new to java and trying to print the values of multiple variables.But the quotes inside the System.out.println confusing me.Can anybody explain the following syntax?
Why "+ b1.cc" is outside the quotes ?
My code:
System.out.println("Bike data " + b1.brand + " " + b1.color + " " + b1.cc);
Let's say you have:
String one = "1";
String two = "2";
String three = "3";
System.out.println("one: " + stringOne + " and two: " + stringTwo + " and also a three: " + stringThree);
Will print
one: 1 and two: 2 and also a three: 3
It is called concatenation. I.e. you "create a new String".
Look at this answer too for mor information.
In you actual code " " will just add a white space between the values of your variables.
I think you need to learn about string concatenation in Java. You can call a method to concatenate (join together) two strings, but you can also use the + operator.
The String class includes a method for concatenating two strings:
string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end.
You can also use the concat() method with string literals, as in:
"My name is ".concat("Rumplestiltskin");
Strings are more commonly concatenated with the + operator, as in
"Hello," + " world" + "!"
which results in
"Hello, world!"
The + operator is widely used in print statements. For example:
String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");
which prints
Dot saw I was Tod
Such a concatenation can be a mixture of any objects. For each object that is not a String, its toString() method is called to convert it to a String.
You have presented an example of String concatenation, equally valid would be building a String reference separately like,
String str = "Bike data " + b1.brand + " " + b1.color + " " + b1.cc;
System.out.println(str);
Java also supports formatted printing. Assuming those fields are all String(s) you could use
System.out.printf("Bike data %s %s %s", b1.brand, b1.color, b1.cc);
or String.format()
String str = String.format("Bike data %s %s %s", b1.brand, b1.color, b1.cc);
The quotes create a String object for the JVM to use. The variables:
b1.brand
b1.color
b1.cc
will return a String object already so the quotes aren't necessary. If, for instance, b1.color was in quotes, it would print specifically b1.color and not what the variable holds.

Eclipse: change description of variable in debug mode

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

referencing a string variable is giving me a string of numbers

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));

Categories