[java] Java Scanner String input

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?

This question is related to java string java.util.scanner

The answer is


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);
    }
}

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 to use scan.next() instead of scan.nextLine().


    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();

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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to java.util.scanner

How do I use a delimiter with Scanner.useDelimiter in Java? How to read multiple Integer values from a single line of input in Java? What's the difference between next() and nextLine() methods from Scanner class? Read line with Scanner Rock, Paper, Scissors Game Java Java using scanner enter key pressed Scanner is never closed how to insert a new line character in a string to PrintStream then use a scanner to re-read the file Exception in thread "main" java.util.NoSuchElementException Using a scanner to accept String input and storing in a String Array