Results 1 to 3 of 3
- 11-26-2010, 11:52 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
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 ++ ; }
- 11-27-2010, 01:04 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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?
- 11-27-2010, 01:09 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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
-
Testing arguments
By and0rsk in forum New To JavaReplies: 6Last Post: 10-10-2010, 12:17 PM -
Framework for testing
By wizard in forum Advanced JavaReplies: 0Last Post: 02-26-2010, 10:49 AM -
Testing EMF models.
By manik_jforum in forum EclipseReplies: 0Last Post: 12-10-2008, 09:59 AM -
testing program
By chrisbremen in forum New To JavaReplies: 6Last Post: 11-08-2008, 06:23 PM
Bookmarks