This question already has answers here:
Scanner issue when using nextLine after nextXXX [duplicate]
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 9 years ago.
I am writing a basic code which will be taking the number of question as input. After that I am using a for loop to Read each question. However, It is not asking for the first question and directly moving to the second question. Here is the code :
import java.util.Scanner;
class quiz
{
static Scanner in = new Scanner(System.in);
public static void main(String args[])
{
int numberofquestion,i;
String[] question;
question = new String[200];
System.out.print("Enter the number of Question : ");
numberofquestion = in.nextInt();
System.out.println("The number of question you entered is : "+numberofquestion);
for(i=0;i<=numberofquestion-1;i++)
{
System.out.print("Enter the Question : ");
question[i] = in.nextLine();
}
for(i=0;i<numberofquestion;i++)
{
System.out.print("Question = "+question[i]);
}
}
}
One solution is to add
in.nextLine();
after the in.nextInt();
Why? Because nextInt() doesn't read the "newline" character in the input, so your nextLine() in the loop was consuming it.
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Whenever I use sc.nextLine in java I get this error. It skips one line. Anybody knows how to fix it. sc.nextLine fails for multiple test cases as takes skips one line and then take input. Anybody knows another way to take sentence input in java?
For Input:
2
geeksforgeeks
geeks for geeks
your output is:
geeksforgeeks
Code:
class GFG {
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}
//code
}
}
you have to read a line after int testcases=sc.nextInt(); because reading the Int from scanner is not reading a line Termination chars....
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
sc.nextLine(); //<--HERE!
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Close a Scanner linked to System.in
(5 answers)
Closed 4 years ago.
I had setup a method to check if the input matches the type int and doesn't return until it does.
It worked fine for a couple of projects so far but when I used it in my current one I got this kind of error when going into the method for the second time around.
Exception in thread "main" java.util.NoSuchElementException
Here is the method:
private static int inputHandler() {
Scanner sc = new Scanner(System.in);
int num = 0;
try{
System.out.println("Enter a number:");
num = sc.nextInt();
} catch(InputMismatchException e){
System.out.println("Input must match type int");
num = inputHandler();
}
sc.close();
return num;
}
If anyone knows how to fix this issue I would appreciate the help.
This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
how to take more inputs from user using Nextline() in java
if i use scn.Nextline() after reading one input it will work but giving a null in answer.
public class Mapdemo {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
System.out.println("enter the number of phoneaddress you want to store");
int n=scn.nextInt();
int j=0;
System.out.println("enter the phonenumber first and then name of the person phoneaddress");
Map<String, Long> m1=new HashMap<String,Long>();
for(int i=0;i<n;i++)
{
Long l=scn.nextLong();
String s=scn.nextLine();
m1.put(s,l);
}
System.out.println("enter the name to be checked");
String s2=scn.next();
System.out.println(m1.get(s2));
After using .nextInt() for the first time do:
scanner.nextLine();
after that to clear the line, nextLine() stops at the newline character (OS dependant).
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
I wrote some code off my Pseudo post and it compiles, but after entering the item name and cost the first time, then the name again it throws an error with input mis match exception. I'm not sure why.
import java.util.Scanner;
public class NewAssignment
{
public static void main(String args[]){
use names[count] = keyboard.next(); instead od names[count] = keyboard.nextLine(); it will work.
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I know a lot of people have asked this but everyones answer is to use scanner.nextLine() however this does not work for me. I have this code:
if (option == 1) {
System.out.println("What is the name of the goal the task is under?: ");
String goalName = scanner.next();
}
When I use .next() I can only get one word, however when I use .nextLine() the command line does not allow for the user to input anything so it asks "What is the name of the goal the task is under?:" and then continues on the program without letting input occur.
The following test code works:
package asdfsadf;
import java.util.Scanner;
public class Asdfsadf {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("What is the name of the goal the task is under?: ");
String goalName = scanner.nextLine();
System.out.println("You entered: ");
System.out.print(goalName);
}
}
So the only problem would be if you used scanner before this say you had scanner.nextInt() and that would leave an empty "end of the line" in the scanner. So the nextLine would take the end of the line from the previous call. To fix this, just create a new scanner object.