Results 1 to 3 of 3
  1. #1
    Alexander_003 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Testing if it is an Int and if not ask again

    Hello guys ,

    so this what I wanna do , I want tot read 4 integers. But after each of them te program has tot test if the entered value is really an integer if it isn't it should display a error message and it should ask enter it again en so on .

    this is what I have :
    it's a part of a bigger file .

    thanks on beyond .

    Java Code:
    int counter=0;
            while(counter<4){
                System.out.print("Geef een getal tussen 1 en 8 in  :  ");
                if(!in.hasNextInt()){
                    System.out.println(MESSAGE.ERRORGAME);
                    System.out.print("Geef een getal tussen 1 en 8 in  :  ");
                    
                                }
    
              else   {
                poging[counter]=in.nextInt();
                }
                
                
                counter ++ ;
            }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Write a separate method that takes a string to use as a prompt and returns the int that was input. This simplifies the logic: just call the method four times.

    -----

    At the moment you are only printing the error message once if the user enters a non int. Is this what you intend to do?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    for(int ndx = 0; ndx < 4; ndx++) {
        poging[ndx] = readInt("Geef een getal tussen 1 en 8 in  :  ");
    }
    
    
    // ... somewhere else
    
        /**
         * Displays a given prompt and returns the int value entered.
         * TODO: gives the user ??? chances to get it right
         * TODO: what should be returned if the user doesn't enter an int???
         */
    private int readInt(String prompt) {
        // TODO
    }

Similar Threads

  1. Testing arguments
    By and0rsk in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 11:17 AM
  2. Framework for testing
    By wizard in forum Advanced Java
    Replies: 0
    Last Post: 02-26-2010, 09:49 AM
  3. Testing EMF models.
    By manik_jforum in forum Eclipse
    Replies: 0
    Last Post: 12-10-2008, 08:59 AM
  4. testing program
    By chrisbremen in forum New To Java
    Replies: 6
    Last Post: 11-08-2008, 05:23 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
  •