Results 1 to 7 of 7
  1. #1
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default Scanner problem.

    Hello, I am working on a terminal based game and have a problem with the scanner.

    The below code is part of the "players turn". The first time the method is called for the game, it prints once and works perfectly. Then during the second calling & onwards, it prints the "Message..." and "type a command:..." lines twice before it offers me to input a string into the terminal.

    Java Code:
    if(!notComputerTurn) {
                System.out.println("Message...");
                System.out.println("type a command: play, move, exit");
                String keyWord = scan.nextLine();
            
          if(keyWord.contains("play")) {
                  int number scan.nextInt();
              ...
            }
            ...
    }
    I have been trying to fix this for hours... Im working in netbeans but have also taken it to bluej and used that debugged to trace, it simply skips over it the first time (after the second time its called).

    Whats wrong?
    Thank you.
    Last edited by keo; 04-07-2011 at 06:49 AM.

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

    Default

    Do you call nextInt or nextDouble anywhere in the code you omitted?

  3. #3
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    Oops, I wrote my code wrong, but yes, there is a nextInt is that the problem? Thanks for helping.

    Wrong:
    Java Code:
          if(keyWord.contains("play")) {
                            String keyWord = scan.nextLine();
                              ...
                   }
    Correct:
    Java Code:
                  if(keyWord.contains("play")) {
                          int number scan.nextInt();
                          ...
                    }
    ...

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

    Default

    Yes nextInt is notorious for causing headaches. If user enter 9[enter] the nextInt method reads the 9 but not the enter. The next time you call nextLine it reads the empty String between the 9 and enter and carries on as if user had entered something. Solution: after the call to nextInt make a call to nextLine and throw it away.

  5. #5
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    Thank you so much Junky! :):):):):)

    I have spent so many hours just trying to fix this bug, and I would have never of fixed it without asking :rolleyes:.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    I would have never of fixed it without asking
    Learn to search the net intelligently. Google gives a truckload of useful results when searching "nextint nextline problem"

    db

  7. #7
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Learn to search the net intelligently. Google gives a truckload of useful results when searching "nextint nextline problem"

    db
    But I had no idea that nextInt() was causing the problem.

Similar Threads

  1. Scanner Class Problem
    By jgg in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 01:07 PM
  2. Scanner problem
    By mkay in forum New To Java
    Replies: 2
    Last Post: 10-28-2010, 12:09 AM
  3. Problem with scanner
    By JavaJ in forum New To Java
    Replies: 6
    Last Post: 03-16-2010, 08:51 PM
  4. Problem with scanner
    By Kangaroo128 in forum New To Java
    Replies: 11
    Last Post: 09-01-2009, 08:07 AM
  5. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 12:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •