This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Scanner only reads first word instead of line
(5 answers)
Closed 4 months ago.
I am running this code to get 3 values: an integer, a string and a boolean from the user and print it in separate lines.
import java.util.*;
public class Practice {
public static void main(String[] args){
int a;
String b;
boolean c;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
b = scanner.nextLine();
c = scanner.nextBoolean();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
I am trying to give input like this:
1
hello world
true
and am getting this error after writing the second line of input
next() can read the input only till the space. It can't read two words separated by a 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.
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
String b = scan.next();
boolean c = scan.nextBoolean();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
so basically you can use next() instead of nextLine() or reorder if you really need to use nextLine()
b = scanner.nextLine();
a = scanner.nextInt();
c = scanner.nextBoolean();
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was trying to take input for the number of names to be stored in an array from user and then using that i was taking names from the user ,first i tried to take names from the user using next() method and all the things were fine but when i tried to take input using nextLine() method the output was as shown below
package learningJava;
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
Output for the nextLine() method
Enter the number of names you are gonna enter
5
Enter the name of friend 1
Enter the name of friend 2
It is not prompting me to enter the name of friend 1 and directly skipping it and coming to the friend 2 line.
I am beginner in Java , i know the basic difference in next and nextLine() that next() doesn't take input after a space but nextLine() takes complete input , So what is happening here ??
just in for loop, just change "println" to "print" because nextLine() consumes new line character.
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
check this answer: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
I have a Java program below. At first, I tried to get input n as this
int n = sc.nextInt();
But the output was different than expected. It runs the first iteration without taking the userName. After changing to
int n = Integer.parseInt(sc.nextLine());
It's working fine. What was wrong with "int n = sc.nextInt();"?
public class StringUserNameChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Number of usernames you want to enter
int n = Integer.parseInt(sc.nextLine());
while (n-- != 0) {
String userName = sc.nextLine();
System.out.println("Your username is " + userName);
}
}
}
When sc.nextLine() is used after sc.nextInt(), it will read a newline character after the integer input.
So, to run your code correctly, you'll have to use sc.nextLine() after sc.nextInt(), like this:
int n = sc.nextInt();
sc.nextLine();
while(n-- > 0) {
String username = sc.nextLine();
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
The code is not scanning the second string form user. It just prints 'Hello' second string is not printed.
package online_questions;
import java.util.Scanner;
public class Add {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int i = 4;
double d = 4.0;
String s = "Hello ";
int a = scan.nextInt();
double b = scan.nextDouble();
String c = scan.nextLine();
System.out.println(i+a);
System.out.println(d+b);
System.out.println(s + c);
}
}
after
double b = scan.nextDouble();
add
scan.nextLine();
The methods nextInt nextDouble etc doens't move to the next line automatically. So you need to call them explicitely.
After reading double b there will be a newline character in the buffer. nexLine() will read upto new line character '\0 so that's why your getting empty string simply addscan.nextLine();after reading your double
You need to call nextLine after nextDouble.
double b = scan.nextDouble();
scan.nextLine();
String c = scan.nextLine();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
When I use scan.nextLine(), input boxes don't work properly. If it's scan.next() ,works perfectly.But Why? Ain't I supposed to use scan.nextLine() for string?
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
System.out.println("p");
String p = scan.nextLine();
System.out.println("q");
String q = scan.next();
System.out.println("m");
String m = scan.next();
}
}
Before using them, try to check the doc's.
Reason :
Scanner.nextLine : The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
While, this is not Applicable to Scanner.nextInt
Hence, the Scanner.nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner.nextLine.
Basic Solution would be to use blank Scanner.nextLine after Scanner.nextInt just to consume rest of that line including newline.
For Example
int myVal1 = input.nextInt();
input.nextLine();
String myStr1 = input.nextLine();
This is the solution to the problem I'd use. The above comment by Tahir Hussain Mirwould likely be the cause of the problem
import java.util.ArrayList;
import java.util.Scanner;
public class app {
public static void main(String[] args) {
// declare scanner
Scanner scan = new Scanner(System.in);
// what ever number you need, it could be calculated
int numberOfInputLines = 3;
// the list of the lines entered
ArrayList<String[]> list = new<String[]> ArrayList();
// add each line to the list
for (int i = 0; i < numberOfInputLines; i++) {
// get entire line as a single string
String input = scan.nextLine();
// split the line into tokens, and store the array in the array list
String[] result = input.split("\\s");
list.add(result);
}
// iterate through each line
for (int i = 0; i < list.size(); i++) {
// iterate through the line
for (int j = 0; j < list.get(i).length; j++) {
if (isInteger(list.get(i)[j]) == true) {
// do what you want if the input is an int
//to show it works
System.out.println("int: " + list.get(i)[j]);
} else {
// do what you want if a the token inputed is a string
//to show it works
System.out.println("String: " + list.get(i)[j]);
}
}
}
}
// greasy way to check if is an int
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
}
You should use nextLine and then convert it to your expected types.
In above scenario read the line then cast it to an integer, because next and nextInt just read the input before a whitespace occurred. So when you are calling nextInt it will just consume the number and leave the newLine character which will be consumed in nextLine.
From the question, it looks like this is how you are going to read inputs input.
First integer.
Second a string line.
Third line will have two words separated by space.
This is what your code should be.
Scanner scan = new Scanner(System.in);
int x = Integer.parseInt(scan.nextLine()); //read line and then cast to integer
System.out.println("p");
String p = scan.nextLine();
System.out.println("q m");
String[] linesParts = scan.nextLine().split(" "); // read the whole line and then split it.
String q = linesParts[0];
String m = linesParts[1];
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
I am trying to add elements to hashset, but it gets an empty element into it.
Initially I tried,
import java.util.*;
public class SetTrial{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
HashSet<String> names = new HashSet<String>();
for(int j=0; j<number;j++)
{
String text = sc.nextLine();
names.add(text);
}
System.out.println(names);
}
}
When I give input as,
5
a
b
c
d
e
It seems to only accept input till d and execute print displaying
[, a, b, c, d]
My guess was that it is accepting a newline at beginning, so I added a sc.next() in the code.
import java.util.*;
public class SetTrial{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
HashSet<String> names = new HashSet<String>();
sc.next();
for(int j=0; j<number;j++)
{
String text = sc.nextLine();
names.add(text);
}
System.out.println(names);
}
}
Although this time it seems to accept all of the input properly, the result is
[, b, c, d, e]
So the problem must be something else. How do I fix this?
Second approach was nearly right.
Just replace sc.next() with sc.nextLine().