Why is the output 30 and not 31? [duplicate] - java

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 5 years ago.
public class Class1 {
public static void main(String[] args) {
int myFirstNumber =20;
int mySecondNumber=10;
System.out.println(myFirstNumber+++mySecondNumber);
}
}
mySecondNumber should have been incremented to 11, thus making the sum 31

It's the Java parser interpreting
+++
as (myFirstNumber++)+, rather than +(++mySecondNumber)
We use the term greedy to describe that behaviour; i.e. the parser consumes as much of the input as it can in order to form a meaningful expression.
Be assured, that after the println, myFirstNumber will be 21.

Related

How to round up a floating-point number in Java [duplicate]

This question already has answers here:
Java Round up Any Number
(7 answers)
Closed 3 years ago.
public class MyClass {
public static void main(String args[]) {
double x=120.38;
System.out.println(Math.round(x‬));
}
}
Output: 120
But I want an output of 121
Rounding off any decimal values to 1 whole number
Replace Math.round with Math.ceil
Try to use : Math.ceil(x)
It will works for you

How to increment the string value? [duplicate]

This question already has answers here:
How to increment the number in a String by 1?
(7 answers)
Closed 5 years ago.
For example:
public static void main(String[] args)
{
String a="1";
int inc= Integer.parseInt(a+1);
System.out.println(inc);
}
I'm getting 11 but i want to get 2. How can i do it in a very efficient way?
Integer.parseInt(a+1); parses the String that results from concatenating the value of the String a ("1") to the int literal 1, which is "11".
Change it to
int inc = Integer.parseInt(a) + 1;
This way "a" would be parsed to the integer 1 and then 1 would be added to it to give you the value 2.
Since a is a String object this operation is not giving the desired input
Integer.parseInt(a+1);
because will be equivalent to do
Integer.parseInt("1"+"1");
or
Integer.parseInt("11");
you need to parse the string first and then increment
Integer.parseInt(a)+1

Java compiler shows error that “integer is too large” [duplicate]

This question already has answers here:
"Integer too large" for a small compile time constant
(4 answers)
Closed 5 years ago.
when I add integer number it's show me "integer too large" even if I make it double howto solve this
public class Three {
public static void main(String[] args) {
int i = 05955555;
}
Try this instead:
int i = 5955555;
In Java, an integer number starting with 0 is interpreted as being in octal base - and in that base, you can't have the digit 9.

Can you somehow turn a String like "14232*12331-1231" into something Java can Calculate? [duplicate]

This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 8 years ago.
I'd like to know if you can somehow get something like
String test= "3445+100";
double x= test
so I can turn a typed in calculation into a result?
First time asking sorry if I waste your time :/
You split the String between the operators. Then you cast the Number-Strings to int and you can cast the Operators with a distinction of cases because you can have only +,*,- and /.
String test= "3445+100";
String[] numberArray=test.split("\\+");
int a = Integer.parseInt(numberArray[0]);
int b = Integer.parseInt(numberArray[1]);
int solution=a+b;

Why does a char disappear when concatinated with an integer [duplicate]

This question already has answers here:
The concatenation of chars to form a string gives different results
(5 answers)
why does a char + another char = a weird number
(4 answers)
Closed 8 years ago.
We had an odd thing in our logging happen today. Printed is a list of integers and longs separated by commas, like the following code:
public class Main {
public static void main(String[] args) throws InterruptedException {
long l = 10;
System.out.println(l + ';' + "text");
}
}
The problem was that the ; disappeared from the output.
The problem here is caused by the overload of the + operator. It acts in one way when operating on a String and a long, and another when operating on a char and a long. When one of the operands is a string it will try to cast the other operand to a string if it's not already one and then concatenate the two.
But when the operators are numbers, like int and long, the + operator acts as the normal mathematical plus operator. And since char is a number and nothing else, ';' + l is treated as a numerical operation and thus the output of the code in the question is 69text and not 10;text.

Categories