Results 1 to 10 of 10
  1. #1
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Skipping an Integer

    Hi guys,

    I wonder if you can help me. I am currently writing an assignment program that needs to do one thing if an integer is entered and another if it is not.

    The problem is where i prompt the user for input; if i do enter an integer all is fine but if i don't enter an integer, and just hit enter, the program goes to the next line and hangs until something is entered (an int, and then moves on or a character and then throws an exception).

    Any help would be greatly appreciated, i have looked on the net and in my text books but i cant find anything. I am using Scanner for the input.

    BM

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not quite sure what you want, can you clarify?

  3. #3
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Hi,
    The program prompts the user for input on a range of items, one is an integer that represents a number of days. If the user does not need to enter any extra days, they are to simply leave this blank and hit enter and go to the next item. Its a simple print line and store user input in variable through using Scanner.

    My problem is when i don't enter anything and just hit enter, the program just hangs, if i press enter again, it just hits a new line and hangs, until something is entered.

    BM

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

    Default

    Perhaps you could post some code?

    One thing I would observe is that if you want to know if the user entered an int on a line or something else (where something else includes nothing at all), then using nextInt() is wrong. (See its API documentation)

  5. #5
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    The code is :

    Java Code:
            //prompt user for input
          System.out.print("Enter your assement: ");
          String assesment = input.nextLine();
          System.out.print("Enter class:");
          String studentClass = input.nextLine();
          System.out.print("Enter days needed: ");
          Int daysNeeded = input.nextInt();            // this is where it hangs if an
          if(input.hasNextLine()){                         // integer is not entered
             input.nextLine();
          }
          System.out.print("Enter Teacher: ");
          String teacher = input.nextLine()
    If nothing is entered for a string the program just moves to the next item and null is recorded, but if nothing is entered for the integer, the program just keeps moving to a blank new line, until a value is entered in.

    BM

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

    Default

    If nothing is entered for a string the program just moves to the next item and null is recorded, but if nothing is entered for the integer, the program just keeps moving to a blank new line, until a value is entered in.

    Exactly. That is the documented behaviour for nextLine() and nextInt(). Briefly nextLine() "Advances this scanner past the current line and returns the input that was skipped" ie it returns a (possibly empty) line. On the other hand nextInt() "Scans the next token of the input as an int" ie it keeps ignoring empty lines until it gets a token which it returns as an int.

    Is this the actual code you are using? I ask because it has a typo ("Int" for int) and because it does not even attempt what you stated originally: that the program "needs to do one thing if an integer is entered and another if it is not."

    If you are trying to see if the user entered an int or something else (including entering nothing at all) then don't use nextInt(). Fundementally this is because you are not trying to read an int: rather you are trying to read something that may or may not be an int. Read the whole line instead and decide, from its contents, whether the user entered an int.

    The Integer method parseInt() may help.

  7. #7
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks for that pbrockway2,

    You have answered my question completely, and have pointed me in the right direction, so that i can now look not so much for an int but rather if there is an integer.

    I really appreciate your time in addressing my question.

    BM

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

    Default

    You're welcome.

    If you get stuck obtaining the user's input, deciding whether it's an integer and doing one thing or another based on that, post whatever you come up with. I didn't post "teh codez", but only so you weren't deprived on the fun of writing your own solution, not to close off discussion with you left stuck.

  9. #9
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    I think the best part about this forum, are the people like yourself that help "point you in the right direction". I would much rather have someone give me tips on where to look and why something might be behaving a certain way, then justing pasting code and telling me that this will "fix the situation". At the end of the day its all about learning to do the code yourself, but with a little nudge in the right direction from people with more experience.

    Once again,

    thanks :)

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

    Default

    That's good to hear. Unfortunately you are in the minority. I have seen on several occassions where someone has copied and pasted the spoonfed code directly into their code and were surprised when it didn't work and post back to whine about it. If they bothered to tink about it and actually learn then they would have been able to adapt the code to suit their needs.

Similar Threads

  1. Skipping lines during debugging
    By Taxi in forum New To Java
    Replies: 5
    Last Post: 04-01-2014, 04:03 PM
  2. Skipping Scanner
    By chari_autumn in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 06:29 AM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. Skipping links!!!
    By jvh in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 12:11 AM
  5. skipping input
    By windie_86 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 07:09 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
  •