Java problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

15th Jul 2021, 7:45 PM
Mohammed Amine
Mohammed Amine - avatar
7 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
15th Jul 2021, 7:50 PM
Jakko Jak
Jakko Jak - avatar
+ 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.
15th Jul 2021, 7:54 PM
Jakko Jak
Jakko Jak - avatar
+ 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 , ??
15th Jul 2021, 8:27 PM
Mohammed Amine
Mohammed Amine - avatar
+ 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.
15th Jul 2021, 8:57 PM
Jakko Jak
Jakko Jak - avatar
+ 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 );
15th Jul 2021, 9:11 PM
zemiak
0
It keeps showing me Exception in thread "main" At java base
15th Jul 2021, 7:52 PM
Mohammed Amine
Mohammed Amine - avatar
0
Your are used Double num1 = in.nextDouble(); Double num2 = in.nextDouble(); The value shoud be double every time
17th Jul 2021, 6:13 PM
‎Abd Almonim D Alshreef
‎Abd Almonim D Alshreef - avatar