I am writing a very simple program to input a string with space and then output it, my problem is, it not printed out fully as I expected.
Here is my code, as you can see, very simple
import java.util.Scanner;
public class Testjapanese {
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.next();
System.out.println(x);
}
}
For example, it print "Add a string" but when I input a string "Today is very", it gave me "Today", not "Today is very".
I search and they said to me that I should use input.nextLine(), but I do not know how to use it. May be I must use public java.lang.String nextLine() first ?
Sorry if my question is easy to solve. Thank you for your answer.
CodePudding user response:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}
}
CodePudding user response:
You should just replace next with nextLine as below
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}