Results 1 to 5 of 5
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default The Simple, Infamous Line-Skipping ".nextLine()" Method

    Evening, everyone. I'm sure experienced programmers are familiar with this frustrating issue, and I've searched the web for the solution but couldn't find any correction for what I was working with..

    I'm working on a program that asks the user to enter the names of students in an array.. It's also in a for loop. Everything is working but when I get to the part where it asks for their names, it skips it.. Here's the code

    Java Code:
    import java.util.*;
    
    public class ClassList
    {
      public static void main (String[]args)
      {
        int numStudents;
        Scanner input = new Scanner(System.in);
        
        System.out.print ("Enter number of students:");
        numStudents = input.nextInt();
        
        String[] name = new String[numStudents];
        String[] number = new String[numStudents];
        
        Student s = new Student(name, number);
        
        for (int i = 0; i < numStudents; i++)
        {
          System.out.print ("Enter student name:");
          name[i] = input.nextLine();
          System.out.print ("Enter student number:");
          number[i] = input.nextLine();
          
        }
        
        System.out.println ("\n\t   STUDENT ROSTER\n");
        System.out.println ("NAME \t\t\t STUDENT NUMBER");
        
        for (int i = 0; i < numStudents; i++)
        {
          System.out.println(s.getName()[i] + "\t\t" + s.getNumber()[i]);
        }
        
      }
    }
    The problem is on line 21. While running the program, it will completely skip "Enter Name" and go straight to "Enter number". This happens the first time of the loop, the 2nd and onward times it works fine. Anyone know why this is happening and have a solution to this curse?
    Last edited by CuppaCoffee; 01-30-2012 at 04:12 AM.

  2. #2
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Re: The Simple, Infamous Line-Skipping ".nextLine()" Method

    I just posted basically the same question, but I'm kind of a newbie so I don't know.

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

    Default Re: The Simple, Infamous Line-Skipping ".nextLine()" Method

    Every time you call input.nextInt() follow it with input.nextLine() so that the end of line token is swallowed.

    For example, change this:
    Java Code:
        System.out.print ("Enter number of students:");
        numStudents = input.nextInt();
    to this:

    Java Code:
        System.out.print ("Enter number of students:");
        numStudents = input.nextInt();
        input.nextLine();

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

    Default Re: The Simple, Infamous Line-Skipping ".nextLine()" Method

    Quote Originally Posted by atac57 View Post
    I just posted basically the same question, but I'm kind of a newbie so I don't know.
    Same for you, but you would call scan.nextLine(); after every call to scan.nextDouble() for the same reason.

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: The Simple, Infamous Line-Skipping ".nextLine()" Method

    Ah, you're right, it works fine. I wonder why that glitch occurs, but I'm glad the solution works. Thanks, Fubarable.
    Last edited by CuppaCoffee; 01-30-2012 at 02:36 PM. Reason: Misread previous post~

Similar Threads

  1. Simple "Refresh" and "Forward" buttons
    By needSolution in forum AWT / Swing
    Replies: 3
    Last Post: 12-12-2011, 01:15 AM
  2. Scanner "nextLine" Command [Homework]
    By btgrant in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 02:45 AM
  3. Replies: 3
    Last Post: 08-25-2010, 10:21 AM
  4. Convert " ü " into simple " u "
    By nitinb4u in forum New To Java
    Replies: 4
    Last Post: 02-23-2009, 08:35 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •