The problem with this line:
String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
is that operations are performed left to right, so it first sums h + three (which evaluates to an int) and then one and then ten. Up to that point you have a numerical value (an int) that then will be "summed" to a String. Try something like this:
String output = "" + h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
In this second case your expression will start with a String object, evaluating the rest of the operations as Strings.
You of course could use "" at the beginning or any other value that evaluates to String, like String.valueOf(h). In this last case you wouldn't need to use String.valueOf() for the other operands, as the first one is already a String.
The problem with this line:
String output = h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
is that operations are performed left to right, so it first sums h + three (which evaluates to an int) and then one and then ten. Up to that point you have a numerical value (an int) that then will be "summed" to a String. Try something like this:
String output = "" + h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t;
In this second case your expression will start with a String object, evaluating the rest of the operations as Strings.
You of course could use "" at the beginning or any other value that evaluates to String, like String.valueOf(h). In this last case you wouldn't need to use String.valueOf() for the other operands, as the first one is already a String.
You can either convert your numbers into a string using the toString or valueOf methods of the wrapper classes (guess you are not there yet), or just stuff all your primitives into the printline without the String output.
system.out.println(h + three + one + ten + " " + "w" + zero + "r" + won + "d " + two + " " + t);
All you need to look for is that there is a String in the printline statement. Meaning if you only want to print our number based datatype you can use system.out.println("" + youNumberVariable).
There would also be the option to add an empty string at the beginning of your declaration of output output = "" + theRest; to force all following values into the string like it does in the printline statement.
Most of it is not very pretty coding but will completly suffice for the learning process.
Java print string and int on same line - Stack Overflow
java - How do I combine strings and integers in a print statement? - Stack Overflow
Printing 2 variables, same line without them adding together
concatenating string and numbers Java - Stack Overflow
Videos
Try this:
System.out.printf("M%d%n", number+1);
Where %n is a newline
Add a bracket around your sum, to enforce the sum to happen first. That way, your bracket having the highest precedence will be evaluated first, and then the concatenation will take place.
System.out.println("M"+(number+1));
You need to use the printf method and not println.
println is used to print primitive types, Strings and objects as they are. Also, println takes only one argument as input. That is the reason you are getting an error when you pass multiple arguments to it in your code.
printf on the other hand is used to format and then print the formatted string to the Standard output / error. This is what you should use in your code above for formatting the output.
Here's a reference to the tutorials.
Hope this helps!
Try:
int x = 3;
System.out.println("This is my number" + x);
The output should be:
This is my number 3
Using Java, format is on mobile, only using “1. and 2.... so it page breaks)
The output is suppose to be: 1.Chen 2. 5050
Instructions:
-
create string called Chen, I did this
-
Create int variable age=50, did this
-
Create int variable iq=age, did this
-
Print value of name
-
Print the value of age without skipping a new line
-
Print the value of iq
My output is
-
Chen
-
100
It’s adding age and iq making it 100 instead of 5050
My code:
-
class Main {
-
public static void main(String[] ages) {
-
String name=“Chen”;
-
Int age=50;
-
Int iq=age;
-
System.out.println(name);
-
System.out.println(age+iq);
When I run this it’s giving me:
-
Chen
-
100 (it’s adding age and iq) But when I check how it’s suppose to print out it should be:
-
Chen
-
5050
Its the BODMAS Rule
I am showing the Order of precedence below from Higher to Low:
B - Bracket
O - Power
DM - Division and Multiplication
AS - Addition and Substraction
This works from Left to Right if the Operators are of Same precedence
Now
System.out.println("printing: " + x + y);
"printing: " : Is a String"
"+" : Is the only overloaded operator in Java which will concatenate Number to String.
As we have 2 "+" operator here, and x+y falls after the "printing:" + as already taken place, Its considering x and y as Strings too.
So the output is 2010.
System.out.println("printing: " + x * y);
Here the
"*": Has higher precedence than +
So its x*y first then printing: +
So the output is 200
Do it like this if you want 200 as output in first case:
System.out.println("printing: "+ (x+y));
The Order of precedence of Bracket is higher to Addition.
Basic math tells you that adding numbers is done each at a time.
So "printing: " + x is computed first. As it s a string + int the result is "printing: 20". Then you add y so "printing: 20" + y equals "printing: 2010".
In the second case, multiplying is prioritary. So first x * y is calculated and equals 200. Then "printing: " + 200 equals "printing: 200".
i'm a Java beginner and just recently picked up my Java book again. so, i'm trying to make a simple program. the code is this:
class MyAwesomeProgram {
public static void main(String [] args) {
String test = "bro";
int test2 = "1234";
if (test.equals ("bro")){
System.out.println("hallo!");
}
else {
System.out.println("test successful.your computer will be h");
}
System.out.println(test2);
}
}
when i try to run this with Java, i get the error error: incompatible types: String cannot be converted to int
i'm not trying to convert anything, i'm just trying to use something other than a string! i will provide more info if needed
EDIT: i'm an idiot. in my original post, i tried assigning a LETTER to an int, which can only handle numbers. i know this but i just didn't notice it before i hit post. the same error happens when assigning a number to the int. also happens with any other variable type like boolean or byte.
EDIT 2: solved! i had to remove the quotation marks on 1234
Actually it's not println() implementation who's causing that, this is Java way to treat the + operator when dealing with Strings.
In fact the operation is treated from left to right so:
- If the
stringcomes beforeint(or any other type) in the operationStringconversion is used and all the rest of the operands will be treated asstrings, and it consists only of aStringconcatenation operation. - If
intcomes first in the operation it will be treated asint, thusadditionoperation is used.
That's why a + b +" " gives 7 because Stringis in the end of the operation, and for other expressions a + b +" "+ a+b or ""+ a+b, the variables a and b will be treated as strings if the come after a String in the expression.
For further details you can check String Concatenation Operator + in Java Specs.
It has nothing to do with the implementation of println. The value of the expression passed to println is evaluated before println is executed.
It is evaluated left to right. As long as both operands of + are numeric, addition will be performed. Once at least one of the operands is a String, String concatenation will be performed.
System.out.println(a + b + " ");
// int + int int + String
// addition, concatenation
System.out.println(a + b + " " + a + b);
// int + int int + String Str+Str Str+Str
// addition, concat, concat, concat
System.out.println("" + a + b);
// String+int String+int
// concat, concat