+ 3
Java problem
Scanner in = new Scanner(System.in); Double num1 = in.nextDouble(); Double num2 = in.nextDouble(); double sum; sum=num1 + num2; System.out.println(sum); If num1 & num2 are interger it'd run fine Else num1or num2 are double it'd printout error HELP
8 Answers
+ 3
What error are you getting? Works fine for me.
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Double num1 = in.nextDouble();
Double num2 = in.nextDouble();
double sum;
sum=num1 + num2;
System.out.println(sum);
}
}
:::INPUT:::
1
2.3
:::OUTPUT:
3.3
:::INPUT:::
1.5
3
:::OUTPUT:::
4.5
:::INPUT:::
1
2
::::OUTPUT:::
3.0
+ 2
Can you put it into Code Playground and link me so I can see the error? More than happy to help, just need more information on the error.
+ 2
Martin Taylor
Thank you it's working
My machine isn't compling because i was using . Instead ,
Thank you
but why is the result use . instead , ??
+ 2
If your locale uses a comma (,) instead of a period (.) for decimals, then you can change that setting in your JRE so that it reflects your locale instead. That's how the compiler will know which one to expect.
+ 2
If your system eg Windows has set different separator, because local country habits, installed java expect this sarator instead standard English dot
this shows expected decimal separator
//import java.text.DecimalFormatSymbols;
char dSepar = new DecimalFormatSymbols()
.getDecimalSeparator();
System.out.println( dSepar );
0
It keeps showing me
Exception in thread "main"
At java base
0
Your are used
Double num1 = in.nextDouble();
Double num2 = in.nextDouble();
The value shoud be double every time