Results 1 to 2 of 2
- 10-08-2011, 02:37 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
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:
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, sc.next and sc.nextDouble).Java Code:System.out.print ("Please enter your first and last name: "); String name = sc.nextLine();
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.
Any help would be much appreciated :)Java Code:import java.util.Scanner; class Test { public static void main (String[] args) { Scanner sc = new Scanner(System.in); 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 = sc.next(); 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); } }
-
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
-
-jGRASP wedge2 error: command "javac" not found.
By ncneeds in forum New To JavaReplies: 0Last Post: 09-28-2011, 09:21 PM -
trouble with Scanner(new File("input"));
By ronyosi in forum New To JavaReplies: 9Last Post: 10-27-2010, 11:34 PM -
"Could not find the main class" for Linux Command Line Compile
By tetelee in forum New To JavaReplies: 3Last Post: 08-25-2010, 10:21 AM -
Compiling/Running Project in Command Line: "Could not find main class" Error
By Yasemin Gokce in forum New To JavaReplies: 1Last Post: 06-30-2009, 02:32 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks