Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Scanner for String in for loop

  1. #1
    CannonVulcan is offline Member
    Join Date
    Oct 2012
    Location
    Trinidad
    Posts
    4
    Rep Power
    0

    Question Scanner for String in for loop

    I need to read the name of a person in a program and then a number amount for sales.

    Essentially I create a scanner object "read" and do this.

    Name = read.nextLine();
    sales = read.nextDouble();

    (I removed most of the other code to avoid confusion). This works fine at first but im running in a for loop which means it then tries to repeat the code. When it tries again it skips the name entry. I know this is something to do with a nextline glitch that happens when you read.nextLine after a double.
    I would usually do this

    Name = read.nextLine();
    Name = read.nextLine();

    That usually solves the problem in procedural but if I do that in this program I have to enter the first person's name twice. Is there a way around this that I can read a string and a double one after the other?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Scanner for String in for loop

    One problem you may be running into is that Scanner#nextDouble() (and nextInt(), next(),...) do not handle the end of line (EOL) token, and so this token is being read by your next call to nextLine(), where you're trying to read in the next name. A solution is to call nextLine() when you need to handle that token. For instance:

    Java Code:
    name = read.nextLine();
    sales = read.nextDouble();
    read.nextLine();  //  **** add this to handle the EOL token ****
    Note that for similar problems, you'll want to post some of the data file so we can see how it is formatted as this can change everything.
    CannonVulcan likes this.

  3. #3
    CannonVulcan is offline Member
    Join Date
    Oct 2012
    Location
    Trinidad
    Posts
    4
    Rep Power
    0

    Default Re: Scanner for String in for loop

    Thank you very much that helped a lot program works perfectly now :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 07:13 PM
  2. Scanner class in my for loop
    By Mode in forum New To Java
    Replies: 1
    Last Post: 12-20-2010, 10:48 AM
  3. Scanner Loop
    By Phenomena in forum New To Java
    Replies: 2
    Last Post: 04-01-2010, 07:47 PM
  4. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 03:15 AM
  5. Replies: 0
    Last Post: 11-20-2007, 05:59 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
  •