Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Little Error checking help (http://www.programmingforums.org/showthread.php?t=13104)

BungalowBill May 3rd, 2007 8:31 AM

Little Error checking help
 
I've got two methods which take in information from the user. One which takes in Strings:

:

public static String getNames(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        String name = null;
       
        try{
            name = br.readLine();       
        } catch (IOException ioe){
            System.out.println("Error trying to read input");
            System.exit(1);
        }
       
        return name;
    }


and one which takes in Integers:

:

public int getNums()
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        String input = null;
        int num = 0;
       
        try{
          input = br.readLine();
          num = Integer.parseInt( input );
        } catch (IOException ioe){
            System.out.println("Error trying to read input");
            System.exit(1);
        }
       
        return num;
    }


The problem is that if the user enters a string when the system expects an integer, there is an error and the system crashes.

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "r"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)


Any idea on what to write to ensure the user enters an integer when asked?

kruptof May 3rd, 2007 9:00 AM

"Please enter a number from [min] to [max]....if you don't enter a number this program will crash":D

DaWei May 3rd, 2007 10:20 AM

While that seems like a joke, there is a large measure of truth involved. YOU DO NOT HAVE CONTROL OF YOUR USER. Instead, you use a function that tells you whether or not the user complied with your requirements. Then you check what the function had to say. If your function doesn't have that ability, the function was written by a schlock. If your function has that ability, but you don't use it, you are either uninformed, or a schlock.

ReggaetonKing May 3rd, 2007 10:33 AM

Just have another catch statement to catch the NumberFormatException!
:

public int getNums()
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        String input = null;
        int num = 0;
       
        try
        {
          input = br.readLine();
          num = Integer.parseInt( input );
        }
        catch (IOException ioe)
        {
            System.out.println("Error trying to read input");
            System.exit(1);
        }
        catch(NumberFormatException nfe)
        {
          System.out.println("enter an integer value!!");
          System.out(1);
        }
       
        return num;
    }



All times are GMT -5. The time now is 2:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC