问题
I'm writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say only 'Archbishop' from 'Archbishop Street'. How do I fix this?
import java.util.*;
class MyStudentDetails{
public static void main (String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter Your Name: ");
String name = s.next();
System.out.println("Enter Your Age: ");
int age = s.nextInt();
System.out.println("Enter Your E-mail: ");
String email = s.next();
System.out.println("Enter Your Address: ");
String address = s.next();
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("E-mail: "+email);
System.out.println("Address: "+address);
}
}
回答1:
This approach is working, but I don't how, can anyone explain, how does it works..
String s = sc.next();
s += sc.nextLine();
回答2:
Initialize the Scanner this way so that it delimits input using a new line character.
Scanner sc = new Scanner(System.in).useDelimiter("\\n");
Refer the JavaDoc for more details
Use sc.next() to get the whole line in a String
回答3:
I would use Scanner#nextLine opposed to next
for your address
attribute.
This method returns the rest of the current line, excluding any line separator at the end.
Since this returns the entire line, delimited by a line separator, it will allow the user to enter any address without any constraint.
回答4:
Default delimiter of Scanner is whitespace. Check javadoc for how to change this.
回答5:
String s="Hi";
String s1="";
//For Reading Line by hasNext() of scanner
while(scan.hasNext()){
s1 = scan.nextLine();
}
System.out.println(s+s1);
/*This Worked Fine for me for reading Entire Line using Scanner*/
回答6:
Instead of using System.in
and System.out
directly, use the Console class - it allows you to display a prompt and read an entire line (thereby fixing your problem) of input in one call:
String address = System.console().readLine("Enter your Address: ");
回答7:
We can easily done using scan.nextLine .It will read the rest of the input till the end. Then assign it to your variable. Entire sentence can be printed easily . Here is the example for your better understanding.
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
String s2;
scan.nextLine(); // read the rest of the line of input (newline character after the double token).
s2 = scan.nextLine();
/* Concatenate and print the String variables on a new line integer variables on a new line;
System.out.println(s + s2);
scan.close();
} }
回答8:
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 s="";
while(scan.hasNext())
{
s=scan.nextLine();
}
System.out.println("String: " +s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
回答9:
I tried following code but this is not stopping as there is no break condition so it always waiting for input , may be you could add a condition for break
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s="";
while(scan.hasNext())
{
s=scan.nextLine();
}
System.out.println("String: " +s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
回答10:
next()
can read the input only till the space. It can't read two words separated by space. Also, next()
places the cursor in the same line after reading the input.
nextLine()
reads input including space between the words (that is, it reads till the end of line \n
). Once the input is read, nextLine()
positions the cursor in the next line.
for reading the entire line you can use nextLine()
回答11:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
s += scan.nextLine();
System.out.println("String: " + s);
}
}
回答12:
To get the whole adress add
s.nextLine();//read the rest of the line as input
before
System.out.println("Enter Your Address: ");
String address = s.next();
回答13:
I set the token delimiter to be a newline pattern, read the next token, and then put the delimiter back to whatever it was.
public static String readLine(Scanner scanner) {
Pattern oldDelimiter = scanner.delimiter();
scanner.useDelimiter("\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029]");
String r = scanner.next();
scanner.useDelimiter(oldDelimiter);
return r;
}
回答14:
"it only prints a word lets say only 'Archbishop' from 'Archbishop Street"
It is only printing the word because Scanner functions such as next(), nextInt(), etc. read only a token at time. Thus this function reads and returns the next token.
For example if you have: x y z
s.next() will return x
s.nextLine() y z
Going back to your code if you want to read the whole line "Archbishop Street"
String address = s.next(); // s = "Archbishop"
Then address += s.nextLine(); // s = s + " Street"
回答15:
Below is the sample code to read a line in Standard input using Java Scanner class.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println(s);
scan.close();
}
}
Input:
Hello World
Output:
Hello World
回答16:
the below piece of code should do what yoiu are looking for:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println(s);
scan.close();
}
回答17:
java.util.Scanner; util - package, Scanner - Class
next()
reads the string before the space. it cannot read anything after it gets the first space.nextLine()
reads the whole line. Read until the end of the line or "/n". Note: Not The Next line
(Example)
My mission in life is not merely to survive, but to thrive;
and to do so with some passion, some compassion, some humor.
(Output)
My
My mission in life is not merely to survive, but to thrive;
Tricks:
If you want to read the next line Check Java has
method.
while (scanner.hasNext()) {
scan.next();
}
while (scanner.hasNext()) {
scan.nextLine();
}
回答18:
I had the same question. This should work for you:
s.nextLine();
回答19:
You can also use
String address = s.nextLine();
address = s.nextLine();
回答20:
Use nextLine()
instead of next()
for taking sentence as string input.
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
回答21:
You can do this:
class MyStudentDetails{
public static void main (String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter Your Name: ");
String name = s.nextLine();
System.out.println("Enter Your Age: ");
String age = s.nextLine();
System.out.println("Enter Your E-mail: ");
String email = s.nextLine();
System.out.println("Enter Your Address: ");
String address = s.nextLine();
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("E-mail: "+email);
System.out.println("Address: "+address);
}
}
来源:https://stackoverflow.com/questions/4058912/scanner-doesnt-read-whole-sentence-difference-between-next-and-nextline-o