Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-16-2007, 03:39 PM
Member
 
Join Date: Nov 2007
Posts: 13
Camden is on a distinguished road
handle wrong input int/null
Lets say we got a program and we want someone to type either 1 or 2 in order to do something. If they type 1) any other integer or if 2) they do not type anything (and just press enter), how can i make the program not crash? I mean e.g. if they type 4 then the program to show again the phrase 'please type either 1 or 2'; and to repeat again and again the prompt until the user types 1 or 2. thank you
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-16-2007, 10:37 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
You can use some if-else statements to test/qualify the iuput.
For example,
Code:
BufferedReader reader = ... int n = -1; do { System.out.println("enter 1 or 2"); String line = reader.readLine(); if(line != null && line.length() > 0) { // User entered something so we may have something to parse. // You may want to check that "line" contains parsable digits // with something like Character.isDigit. // Or, you could use a try/catch for NumberFormatException. n = Integer.parseInt(line); } } while(n != 1 && n != 2);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Handle own exception stevemcc New To Java 3 04-10-2008 05:55 AM
Better way to handle exceptions javaplus Advanced Java 2 01-16-2008 07:47 PM
How to handle socket Exception mayank0512 Networking 13 01-10-2008 01:53 PM
how to handle exceptions paty Advanced Java 2 08-05-2007 05:17 AM
how to take input and verify input in Java programs bilal_ali_java Advanced Java 0 07-21-2007 09:46 AM


All times are GMT +3. The time now is 06:42 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org