• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

try catch stuck in a loop

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to make a loop that checks to make sure a user inputs a number. but when I enter any none number it the catch gets stuck in a loop.
boolean notInt = true;
while(notInt)
{
try
{
//reads the amount of meters the user typed
meters = keyboard.nextInt();
notInt= false;
}
catch(InputMismatchException e)
{
meters = 0;
System.out.println("opps, you did not enter a valid option.");
}
}


I am really hoping some one could tell me what I missed.
 
author
Posts: 23937
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hint: After nextInt() chokes because it encounters the non-number. How do you get rid of the non-number?

Henry
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please see the line 1,


import java.util.InputMismatchException;
import java.util.Scanner;
class AA
{
public static void main(String g[])
{
boolean notInt = true;
int meters=-1;
Scanner keyboard=new Scanner (System.in);
while(notInt)
{
try
{
//reads the amount of meters the user typed
meters = keyboard.nextInt();
notInt= false;
}
catch(InputMismatchException e)
{
meters = 0;
System.out.println("opps, you did not enter a valid option.");
notInt= false; // line 1
}
}
}
}
 
Marshal
Posts: 77920
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be able to sort that out with the hasNextInt method of Scanner. Put that in the test part of your while loop, then you get the error message and "please try again" whenever a non-number is entered. You may need to get rid of the last input, as Henry has suggested; a look at the methods of Scanner will help.
Than you can dispense with the try-catch.

And please use the CODE button; your posts are difficult to read.
 
Daniel Clark jr
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much, I'll try your advice. for now I put it in a string and casted the string in the try catch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic