Results 1 to 6 of 6
  1. #1
    Sinnergy's Avatar
    Sinnergy is offline Member
    Join Date
    Feb 2009
    Location
    Philippines
    Posts
    4
    Rep Power
    0

    Default how to avoid input errors?

    im new at java, i used netbeans and using "run" to see the output of my program, i already know how to use loops, if-else, switch, input data, and some beginner stuffs

    lots of my test programs requires the user to input data and that was the problem, like if the program ask for an "Integer type" data and the user inputs a wrong data like a "character type" data, then my program terminates due to input errors

    what i want to know is how to fix these input errors, like if the user inputs a wrong data, the program will show an error message and letting the user input another data, then continues when the user inputs the right data

    thanks in advance :)
    Last edited by Sinnergy; 02-02-2009 at 11:49 AM.

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default Exception handling might be helpful in such cases

    // For example I am forcing the user to enter a number only in this example
    Java Code:
    import java.io.*;
    
    public class UserInput{
    
    	public static void main(String[] arg){
    		String x="";
    	int userInput=0;
    	while(true){
    		try{
    			System.out.print("Enter a number:-");
    			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    			x=br.readLine();
    			userInput=Integer.parseInt(x);
    			break;
    		}
    		catch(NumberFormatException ne){
    		   System.out.println("You did not enter a number...");
    		}
    		catch(IOException ioe){}
    	}
    
    	System.out.println("You have entered.."+userInput);
      }
    }

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    And there are other ways too.

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    so you know try catch block?
    (Lesson: Exceptions (The Java™ Tutorials > Essential Classes))

    you may try
    declare and initialize a int/Integer "testInt" to -999/null
    use scranner next method to get String type input
    use a do-while loop
    use Integer.parseInt method to get int
    if input is not an int, it will throw a exception, and testInt will remain the initial value...
    if testInt remains initial... loop to ask user input

  5. #5
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    Moreover your logic to validate any user input will completely depend upon the type of validation you wish to perform.

  6. #6
    Sinnergy's Avatar
    Sinnergy is offline Member
    Join Date
    Feb 2009
    Location
    Philippines
    Posts
    4
    Rep Power
    0

    Default

    thnx guys, i always used Scanner instead of BufferedReader, anyways, ill try dswastik's code and i will replace BufferedReader with Scanners

    thnx and more power

Similar Threads

  1. avoid legitimate spamming
    By hervey in forum Suggestions & Feedback
    Replies: 1
    Last Post: 10-29-2008, 09:20 AM
  2. Avoid/Eliminate Duplicate Code
    By kicker in forum New To Java
    Replies: 6
    Last Post: 09-11-2008, 05:14 AM
  3. Replies: 16
    Last Post: 08-05-2008, 12:34 PM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •