Results 1 to 4 of 4
Thread: Assistance With The Scanner
- 02-10-2013, 11:31 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
Assistance With The Scanner
So i'm trying to make a basic text based resource game. I'm using the console scanner as the means of user input. I've added (or rather am trying to add) some manner of error checking to evaluate if the scanner input is invalid meaning the input is either a string or an integer that is greater than what is permitted (in my code the int is called t_available).
while(!scanner.hasNextInt() || scanner.nextInt() > t_available){
System.out.println("Invalid Selection");
System.out.println("Currently You Have Materials To Build " + h_temp + " Hovels");
System.out.println("And " + t_available + " Tradesmen Available");
System.out.println("How Many Hovels Would You Like To Build?");
scanner.next();
}
h_temp2 = scanner.nextInt();
If I remove the (scanner.nextInt() > t_available) portion it works as expected, but so long as its in there it doesn't appear the while loop breaks if a valid value (meaning < t_available) in input. It does flag an invalid value however (meaning > t_available). Can't figure this out for the life of me. Is there a better way to approach this? Any insight would be greatly appreciated.
- 02-11-2013, 12:47 AM #2
Re: Assistance With The Scanner
That statement reads an int value, compares its value and throws the value away. Is that what you intend the code to do? If you want to save the value read, you need to read it into a variable.scanner.nextInt() > t_availableIf you don't understand my response, don't ignore it, ask a question.
- 02-11-2013, 03:06 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
Re: Assistance With The Scanner
Hi Norm, thanks for giving this a look. The idea is if the scanner value is invalid (a string or greater than t_available) you should be prompted to enter a valid value. If the value is invalid it should be thrown away, but if it is valid the while loop shouldn't activate and the code should proceed to the next line (h_temp2 = scanner.nextInt();). Does that make sense? Thanks in advance.
- 02-11-2013, 05:00 AM #4
Similar Threads
-
In need of assistance!
By Arete in forum New To JavaReplies: 4Last Post: 01-26-2012, 01:40 AM -
Assistance about this.
By Syfer in forum Advanced JavaReplies: 38Last Post: 06-21-2010, 05:59 PM -
Looking for assistance
By s_dawg101 in forum New To JavaReplies: 32Last Post: 11-04-2009, 02:49 AM -
New to the forum + assistance :)
By quemadissimo in forum New To JavaReplies: 4Last Post: 10-31-2009, 06:41 AM -
In need of some assistance
By Boer84 in forum New To JavaReplies: 2Last Post: 07-08-2008, 04:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks