Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Assistance With The Scanner

    scanner.nextInt() > t_available
    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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Assistance With The Scanner

    It seems confusing to have the user enter two values and only use the second one if the first one is valid.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. In need of assistance!
    By Arete in forum New To Java
    Replies: 4
    Last Post: 01-26-2012, 02:40 AM
  2. Assistance about this.
    By Syfer in forum Advanced Java
    Replies: 38
    Last Post: 06-21-2010, 06:59 PM
  3. Looking for assistance
    By s_dawg101 in forum New To Java
    Replies: 32
    Last Post: 11-04-2009, 03:49 AM
  4. New to the forum + assistance :)
    By quemadissimo in forum New To Java
    Replies: 4
    Last Post: 10-31-2009, 07:41 AM
  5. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 05:14 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
  •