This question already has answers here:
Java Scanner class reading strings
(5 answers)
Closed 8 years ago.
I got the following code:
int nnames;
String names[];
System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
names = new String[nnames];
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.next();
}
System.out.println(names[0]);
When I run this code, the scanner will only pick up the first name and not the last name. And it will sometimes skip a line when trying to enter a name, it will show up as if I had left the name blank and skip to the next name. I don't know what's causing this.
I hope someone can help me!
EDIT: I have tried in.nextLine(); it fixes the complete names but it still keeps a line, here is an example of the output:
How many names are you going to save: 3
Type a name: Type a name: John Doe
Type a name: John Lennon
Instead of:
in.next();
Use:
in.nextLine();
nextLine() reads the characters until it finds a new line character '\n'
After your initial nextInt(), there's still an empty newline in your input. So just add a nextLine() after your nextInt(), and then go into your loop:
...
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
in.nextLine(); // gets rid of the newline after number-of-names
names = new String[nnames];
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.nextLine();
}
...
Scanner.next stops reading when it encounters a delimiter, which is a whitespace. Use the nextLine method instead.
Try using:
System.out.println()
Instead of:
System.out.print()
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was working with array and i wanted to make a list that user can set a value for its length and then also user can add some String values duo to it's length that user set up.I wanted the program to show me the list that user entered to user (just practicing) and i found out a different use of next() and nextLine() that nextLine() method wouldn't work properly since i wanted to add as many String as i wanted to my Array.here is what i am thinking:
System.out.println("Welcome to addingCourse wizard :)");
System.out.println("Please enter the amount of your course: ");
int z = input.nextInt();
String[] course = new String[z];
for (int i = 0; i < course.length ; i++) {
course[i] = input.nextLine();
}
System.out.println("Ok. Here is your list...");
for (int i = 0; i < course.length; i++) {
System.out.println(course[i]);
}
problem is nextLine() wouldn't allow us to add items as we wanted in array.for example if you want your list to be 100, it will allow you to just enter 99 items although next() method allow course.length items and print all items
Take a look at these lines
int z = input.nextInt();
String[] course = new String[z];
for (int i = 0; i < course.length ; i++) {
course[i] = input.nextLine();
}
Whenever you hit the enter key, this creates a new line /n character. As soon as you read your integer on the first line, nextInt isnt consuming said char. Take a good look at the output after your lines are being printed, the first line is an empty line.
To consume this new line, add another nextLine() right after you finish reading your int.
int z = input.nextInt();
input.nextLine(); //Consumes the new line character
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 7 years ago.
I tried to get inputs via scanner and in the past, I use enter to get to the next set of inputs.
For ex.
Input 1 <enter>
Input 2 <enter>
However this time, it only accepts in the same line , taking spaces as delimiter.
Scanner in = new Scanner(System.in);
int a,b,n,t;
String input_line;
String inputs[]= new String[3];
t = in.nextInt();
in.reset(); //Tried resetting Scanner to see if this works
input_line = in.nextLine();
inputs = input_line.split(" ");
for(String s:inputs)
System.out.println(s);
For instance, I expect to take the variable t in first line and then move on to the second line for input_line scanning. But if I hit enter after entering t, the program ends.
What am I missing here?
(Merging with another question was suggested but , let me explain, the Scanner does not skip any inputs).
Without any testing I would think you would need something like this
Scanner in = new Scanner(System.in);
int a,b,n,t;
String input_line;
String[] input_numbers = new String[3];
t = in.nextInt();
in.nextLine();
input_line = in.nextLine();
while(!input_line.equals("")){
input_numbers = input_line.split(" ");
// do what you want with numbers here for instance parse to make each string variable into int or create new scanner to do so
input_line = in.nextLine();
}
}
What actually happens here ?
Why can't I store a String with spaces in between as name ?
I tried the delimiter thing, but didn't worked. Is there a way that will produce the desired output ?
I know .next() works but we might need to store a string with space. Just curious ...
Scanner input=new Scanner(System.in);
System.out.print("Enter number of Students:");
double [] scores = new double[input.nextInt()];
String [] names=new String[scores.length];
for(int i=0;i<names.length;i++){
System.out.println("Enter the students name: ");
names[i] = input.nextLine();
System.out.println("Enter the student scores : ");
scores[i]=input.nextDouble();
}
when you call input.nextInt(), it doesn't consume the new line character on that line, so the following call to input.nextLine(); will consume the newline, and return the empty string. nextDouble() will function properly.
one way to fix this is to call input.nextLine(); immediately before the for loop to consume the extra new line character
Edit:
String [] names=new String[scores.length];
input.nextLine(); //literally add the call right here
for(int i=0;i<names.length;i++){
According to the Javadoc:
A simple text scanner which can parse primitive types and strings using regular expressions.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
What if you replace the default delimiter with "\n" like this:
Scanner s = new Scanner(input).useDelimiter("\\\\n");
This is how it works ! Thanks for concern guys! :)
for(int i=0;i<names.length;i++){
if(input.nextLine().equals("")){
System.out.println("Enter students name : ");
names[i] = input.nextLine();
}
System.out.println("Enter the student scores : ");
scores[i]=input.nextDouble();
}
So this question is kinda simple, but I was wondering why the following code, after running perfectly the first time, jumps straight to "Enter the page number: ", instead of asking for the title the second loop around. I've read somewhere that there is something up with Java that won't allow for a reuse of a Scanner. Is this the problem that I'm facing? I've tried to use kbReader.next() and that works, but the code only works with strings without spaces.
String title = "";
int page = 0;
int i = 0;
Scanner kbReader = new Scanner(System.in);
for (i=1;i<=3;i++){
System.out.print("Enter the title: ");
title = kbReader.nextLine();
System.out.print("Enter the page number: ");
page = kbReader.nextInt();
System.out.print(title);
System.out.println(page);
}
Much help appreciated!
You should add a kbReader.nextLine(); after kbReader.nextInt();. On the second loop, your kbReader.nextInt(); is just reading the end-of-line in the stream, left out by kbReader.nextInt();.
Does anybody know how i could make scanner ignore space? I wanna type a first and second name, but scanner wont let me, i want to save the full name
String name;
System.out.print("Enter name: ");
name = scan.next(); //Ex: John Smith
System.out.println(name);
Edit:
New problem.. While using nextLine in my extended program, nextLine just ignores the whole question and moves on without a chance to scan the name.
Scanner#next() splits lines around whitespace. Scanner.nextLine() does not, therefore leaving spaces in.
name = scan.nextLine(); //Ex: John Smith
Well, first your System.out.print(); call is flawed. Everything inside must be inside quotations
System.out.print("Enter name: ");
scan.next() gets the next character in the stream, whereas scan.nextLine() gets the next line (terminated by an EOL character), which may be more helpful to you.
After that, you can create an array of words, like
String[] broken = name.split(" ");
which will place into broken all of the words that you've typed in delimited by spaces.
Then you can go something like
for(int i = 0; i < broken.size; i++)
{
System.out.print(broken[i] + " ");
}
System.out.println();
Scanner.next delimits using whitespaces, to read a full line you can use:
name = scan.nextLine();
use scanner.nextLine() which reads full line, instead of scan.next();
Example:
name = scan.nextLine();
Read oracle documentation for Scanner class for available methods.
sounds like you want to read the entire line (minus the line ending). if someone enters, "helen r. smith", you can read the line in with:
name = scan.nextLine();
YOU CAN DO LIKE THIS
import java.util.*;
class scanner2
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
System.out.println("enter the name");
String name= in.nextLine();//for name with spaces with more than one word or for one word.
System.out.println("enter single word");
String rl= in.next();//single word name
System.out.println("name is "+name+" rl is "+rl);
}
}
Execute it you will get your answer.