This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
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();
 
Sheriff
Posts: 9613
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

 
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.
 
Marshal
Posts: 60199
188
  • 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: 60199
188
  • 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: 9613
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: 9613
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)

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!