Results 1 to 2 of 2
  1. #1
    btgrant is offline Member
    Join Date
    Oct 2011
    Rep Power

    Default Scanner "nextLine" Command [Homework]

    Hey, I'm doing a homework assignment for my java class and my assignment requires me to
    have the user enter their first and last name so I entered the lines:

    Java Code:
            System.out.print   ("Please enter your first and last name: ");
            String name = sc.nextLine();
    I have all the scan stuff configured and those lines works fine on their own, but I'm required toput them in between other scanner commands (in particular, and sc.nextDouble).
    But when I try to, the cursor prints the line but doesn't ask for input from the user... How do
    I fix that?

    Below is the rest of my program if you want to see it.
    Java Code:
    import java.util.Scanner;
    class Test { 
        public static void main (String[] args) {
            Scanner sc = new Scanner(;
            System.out.println ("Getting Input:");
            System.out.print   ("Please enter an integer: ");
            int anyInt = sc.nextInt();
            System.out.print   ("Please enter a single word: ");
            String word =;
            System.out.print   ("Please enter your first and last name: ");
            String name = sc.nextLine();
            System.out.print   ("Please enter a real number: ");
            double realNum = sc.nextDouble();
            int intNum = (int) realNum;
            System.out.println ("");
            System.out.println ("Posting Output:");
            System.out.println ("Hello, " + name);
            System.out.println ("The word you entered was: " + word);
            System.out.println ("The integer you entered was: " + anyInt);
            System.out.print   ("The real number entered was " + realNum);
            System.out.println (" which typecasted to an integer is now a " + intNum);
            int division = intNum / anyInt;
            System.out.println (intNum + " divided by " + anyInt + " = " + division);
            int mod = intNum % anyInt;
            System.out.println (intNum + " mod " + anyInt + " = " + mod);
    Any help would be much appreciated :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Scanner "nextLine" Command [Homework]

    You likely are having problems dealing with the end of line token, a token that gets swallowed by Scanner's nextLine() method but not by its next(), nextInt(), nextDouble() ... methods. One solution is to call sc.nextLine() after every nextInt(), nextDouble(), ...

    Another solution is to avoid calling nextLine() anywhere in your code.

Similar Threads

  1. -jGRASP wedge2 error: command "javac" not found.
    By ncneeds in forum New To Java
    Replies: 0
    Last Post: 09-28-2011, 10:21 PM
  2. trouble with Scanner(new File("input"));
    By ronyosi in forum New To Java
    Replies: 9
    Last Post: 10-28-2010, 12:34 AM
  3. Replies: 3
    Last Post: 08-25-2010, 11:21 AM
  4. Replies: 1
    Last Post: 06-30-2009, 03:32 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08: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