• Post Reply Bookmark Topic Watch Topic
  • New Topic

check variable.nextDouble() is a number not characters  RSS feed

 
Cheikh Sow
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing a program that should act as a calculator; I have the following in it now how do I check if the value of x is an integer and NOT some random character. I hope I am being clear thanks

// initialize first input1 number
Scanner input1 = new Scanner(System.in);
// prompt for the first number
System.out.println("Please enter the first number: ");
// declare the x variable and assign it the value of the first input1 number
double x = input1.nextDouble();
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just make one change to your program as I have shown

 
V R Aravind
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nextDouble() method never accepts characters. if it is anything other than double, there will be an exception namely InputMismatchException. You may catch the exception to inform the user that they didn't enter 'double' datatype.

Also if you want only integers you may use nextInt() method of Scanner class.
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ankit's solution with the hasNextDouble() method is better. You will get much better performance if you can replace an Exception with an if or a boolean test.
 
Cheikh Sow
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks your code is good but it puts an infinite loop in the code it is better to do
// check if second number read in is valid
while (!input1.hasNextDouble()) {
System.out.println("You did not enter a valid number");
System.exit(0); // exit program
}
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still agree with Ankit Garg. That isn't an infinite loop, and using System.exit() can be a bit vicious; if you are in a multi-threaded application it may stop other threads in an "incomplete" state.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheikh as Campbell also said, the code that I gave will not result in an infinite loop. I think what you are trying to say is that if the user enters a non-double number, then he will be asked infinitely for a double number. The solution for that would be you give the user max 3 chances or so like this



[edit: made some changes to the program ]
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheikh you were right. My program is incorrect. I suddenly realized it while I was going home. So here's the correct code (I don't know whether you will read this now or not)

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!