Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 3rd, 2007, 7:31 AM   #1
BungalowBill
Programmer
 
Join Date: Dec 2005
Posts: 40
Rep Power: 0 BungalowBill is on a distinguished road
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?
BungalowBill is offline   Reply With Quote
Old May 3rd, 2007, 8:00 AM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
"Please enter a number from [min] to [max]....if you don't enter a number this program will crash"
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 3rd, 2007, 9:20 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 3rd, 2007, 9:33 AM   #4
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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;
    }
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:05 PM.

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