20

I'm writing a program that uses an Event class, which has in it an instance of a calendar, and a description of type String. The method to create an event uses a Scanner to take in a month, day, year, hour, minute, and a description. The problem I'm having is that the Scanner.next() method only returns the first word before a space. So if the input is "My Birthday", the description of that instance of an Event is simply "My".

I did some research and found that people used Scanner.nextLine() for this issue, but when I try this, it just skips past where the input should go. Here is what a section of my code looks like:

System.out.print("Please enter the event description: ");
String input = scan.nextLine();
e.setDescription(input);
System.out.println("Event description" + e.description);
e.time.set(year, month-1, day, hour, min);
addEvent(e);
System.out.println("Event: "+ e.time.getTime());    

And this is the output I get:

Please enter the event description: Event description
Event: Thu Mar 22 11:11:48 EDT 2012

It skips past the space to input the description String, and as a result, the description (which is initially set to a blank space - " "), is never changed.

How can I fix this?

4
  • You're not printing the description anyway, so how do you know it's not reading properly? Commented May 11, 2011 at 15:27
  • Can you include an input example? Commented May 11, 2011 at 15:45
  • I accidentally omitted the line of code printing the description, but I have put it in now. @Michael, I don't know to tell you about an input example, because I never get a prompt to input a description. Commented May 11, 2011 at 18:32
  • How are you entering the time information? Commented May 11, 2011 at 18:48

5 Answers 5

24

When you read in the year month day hour minutes with something like nextInt() it leaves rest of the line in the parser/buffer (even if it is blank) so when you call nextLine() you are reading the rest of this first line.

I suggest you call scan.nextLine() before you print your next prompt to discard the rest of the line.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm also having same trouble. I also added scan.nextLine(); in place of input.nextLine(); but it gives me warning cannot find symbol scan and to create scan class.
@ZacharyDale you can only use variables you have defined. Use the Scanner you have defined.
@PeterLawrey here is my issue http://stackoverflow.com/questions/41882552/two-steps-running-at-once-in-java?noredirect=1#comment70947891_41882552
@ZacharyDale You need to use nextLine () to consume what remains of the line after the words or numbers, even though you expect it to be empty. Then you need to can call nextLine () again to get the line after.
2
    Scanner ss = new Scanner(System.in);
    System.out.print("Enter the your Name : ");
    // Below Statement used for getting String including sentence
    String s = ss.nextLine(); 
   // Below Statement used for return the first word in the sentence
    String s = ss.next();

Comments

2

If you use the nextLine() method immediately following the nextInt() method, nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty). So we read can read the empty space to another string might work. Check below code.

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int i = scan.nextInt();
        Double d = scan.nextDouble();
        String f = scan.nextLine();
        String s = scan.nextLine();


        // Write your code here.

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
         System.out.println("Int: " + i);
    }
}

Comments

1
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int i = scan.nextInt();
        Double d = scan.nextDouble();
        scan.nextLine();
        String s = scan.nextLine();
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

Comments

0

use this to clear the previous keyboard buffer before scanning the string it will solve your problem scanner.nextLine();//this is to clear the keyboard buffer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.