Problems with decimal point in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problems with decimal point in java

I have this simple code again import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); double num = input.nextDouble(); System.out.println(num); } } when my input is 3.7, in eclipse gives me error (in SL don't) Why this happens? I solved the problem with: double num = Double.parseDouble(input.nextLine)); Now I can enter a number with decimal point, why? I don't quite understand.

13th Jul 2017, 1:47 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
4 Answers
+ 3
try entering 3,7. instead of 3.7
13th Jul 2017, 2:11 AM
Luke Thompson
Luke Thompson - avatar
+ 1
This is the error: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(unknown Source) at java.util.Scanner.next(Unknown source) at java.util.Scanner.nextDouble(Unknown source) at Test.main(Test.java:8)
13th Jul 2017, 2:05 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
+ 1
Thats true! I don't have any error If I enter 3,7 instead 3.8... thx!
13th Jul 2017, 2:16 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
//try float for variable; float num= sc.nextFloat(); import java.util.Scanner; //Rbcode org class Compoundint{ public static void main(String[] args) { System.out.println("welcome calculate your compound intrest here..."); double p,t,i; double ci; float r; double temp; Scanner sc =new Scanner(System.in); System.out.println("enter princle amount"); p=sc.nextInt(); System.out.println("please enter rate in decimal"); r = sc.nextFloat(); System.out.println("enter time period"); t=sc.nextInt(); System.out.println("principle amount :"+p+"rate :"+r+"time period :"+t); sc.close(); for (i=1;i<=t ;i++ ) { ci=p*r*i; temp=p+ci; System.out.println("ci : "+temp); p=temp; } } }
5th May 2020, 8:47 AM
Rajesh Badera
Rajesh Badera - avatar