Results 1 to 7 of 7
  1. #1
    jazzo is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default validating int input

    HI chaps, as part of a larger project I am trying to validate input. I have created a very small and simple program that attempts to do just that, but I am not entirely sure how to do it. I have done a bit of reading online of course before posting, but being pretty new to java, I failed to understand how that is meant to be done. I noticed some people take a string and then check that the string can be converted to an int, some other uses methods like hasNextInt(), but I couldn't get it to work. Here's the code:

    Java Code:
    /*test.java*/
    import java.util.Scanner;
    public class ValidateNumbers{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		int number;
    		System.out.println( "\ninsert number: " );
    		number = input.nextInt();
    		
    			
    		/* while( !input.hasNextInt()){
    			System.out.print("This is not an int, please insert an int now");
    			
    			input.nextInt();
    		} */
    		
    		System.out.println( "\nThe number inserted is " + number );
    		
    	}
    }
    I commented out the bits that don't work of course. SO here I asked the user for an int input and then I thought I would try with hasNextInt() hence the commented out bit but it doestn' work: if I input an integer nothing happens, the curson still flashes and I have to input a second integer. If I input a character the program breaks and produces errors. I also tried to have a look at the api, but I mist admit I got completely lost in there. COuld anybody kindly explain me the process of checking for int validation please? One of the things I am wondering is, can I validate an int if I use nextInt() to get the input as I am doing above? ANy help will be much appreciated, thanks!

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: validating int input

    Take the input as a String, the do:
    try{

    Integer.parseInt(input);
    }
    catch (Excecption e) {

    System.out.print("Not an int try again");

    }

    edit this to suit you

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: validating int input

    Using the java.util.Scanner you simply ask for an int and check it

    Java Code:
    do {
    			try {
    				System.out.print("Enter an integer: ");
    				int number = input.nextInt();
    				continueInput = false;
    			} catch (InputMismatchException e) {
    			     System.out.println("An integer is required!");
    			     input.nextLine(); // Discard input
    			}
    		} while (continueInput);
    	 }
     }

  4. #4
    jazzo is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: validating int input

    right, thanks, I would like to keep the input as an int if I can. Now, I must admit I have never used a try catch statement, so I really don't know much about that except the fact it allows you to gracefully handle errors. Is it not possible to enclose that into a while statement as opposed to a do while? That's for two reasons:
    1) simply because in my larger program where the validation will be easier to implement a while statement;
    2)a do while statements executes once regardless of whether its condition is satisfied or not, and I think that could be a problem in my program
    thanks

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: validating int input

    yeah, of course it can be coded with a while loop, but... you would have to validate the data twice.

    one before the loop and one inside it. A do - while is much better in this case imo, but if it is a problem for you, then do it in your way.

    Also, you have to handle the InputMismatchException, if you want to validate that the input is integer so a try-catch is inevitable.

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: validating int input

    ye do while is the best option, i just gave a sample for a simple solution.

    you can still have the input as an int,

    String x = input.nextLine();
    int y = Integer.parseInt(x);

    this would allow you to keep it as an integer, just reference y.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: validating int input

    Java Code:
    number = input.nextInt();  // 1
    
    while( !input.hasNextInt()){
        System.out.print("This is not an int, please insert an int now");
        input.nextInt();  // 2
    }
    1. You call nextInt before you check if it is an int. If user enters a String then this will throw an exception.
    2. The loop will enter if input is not an int then you call nextInt which of course will again throw an exception.

Similar Threads

  1. Validating log in using database
    By mDennis10 in forum New To Java
    Replies: 6
    Last Post: 03-11-2012, 12:17 AM
  2. Validating user input
    By dfarni in forum New To Java
    Replies: 1
    Last Post: 08-28-2011, 04:42 AM
  3. Help with validating input into an array
    By kcoltharp in forum New To Java
    Replies: 1
    Last Post: 02-25-2011, 09:05 AM
  4. SDF validating date input
    By getshum in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 08:37 AM
  5. Validating Jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 02:54 PM

Posting Permissions

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