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.
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I get an error when i try to input name and num in a loop,
int i = 1;
while(i <3) {
System.out.print("Please enter name: ");
String name = input.nextLine();
System.out.print("Please enter number: ");
int num = input.nextInt();
i++;
}
The error that i am getting is this
on the first iteration the input is normal, however on the second iteration it prints the enter num and name at the same line. Could anyone explain to me why is this happening?
when calling nextInt() you only read the integer, but there is a new line character that should also be read.
so at the end of the loop, call input.nextLine() to read the new line.. and then the rest of the loop should work fine.
https://stackoverflow.com/a/26626204/14951486
Already answered
Refer the link
Error because you are taking String input after int
This question already has answers here:
Java -- Closing Scanner and Resource Leak
(3 answers)
Close a Scanner linked to System.in
(5 answers)
Closed 2 years ago.
I don't know why when I run this part of the code, it doesn't let me type in anything.
import java.util.Scanner;
public class IRA {
public void select() {
Scanner options = new Scanner(System.in);
System.out.println("What do you want to do: \na) Add cash \nb) Invest \nc) Transfer money \nd) Exchange currencies");
System.out.println("Please, enter a, b, c or d: ");
String letter = options.nextLine();
char c = letter.charAt(0);
This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 5 years ago.
When I run this, int x = 0 and 1 is added to x to each time it runs. The program keeps on printing out "Round 1", not allowing any time in between to input anything. This is not about reading input, it's about being able to input something without the program going crazy.
while (!broken) {
int pointcount = 0;
System.out.println("Round " + x);
Scanner jumpOption = new Scanner(System.in);
if (jumpOption.equals("W")) {
pointcount += 1;
Random rand = new Random();
if (rand.nextInt(100) == 0) {
broken = true;
You're creating a Scanner object, but not using it for anything. To get any input from the Scanner you have to use one of its methods, like Scanner.nextLine()
Scanner myScanner = new Scanner(System.in);
// Here's where we get the actual input!
if (myScanner.nextLine().equals("Hello")
You have to actually read from the scanner with the next() method
if (jumpOption.next().equals("W")) {
This question already has answers here:
Scanner throws NoSuchElementException on nextInt
(2 answers)
Closed 6 years ago.
The first time menu() is displayed, I'm able to enter input and runGame() works. The second time the menu is displayed, the program is crashing on the line int answer = scanner.nextInt() with a java.util.NoSuchElementException. It seems that there isn't a 'nextInt' to read in, but I don't ever have the chance to enter it the second time.
public void runGame(){
int userPick = 0;
userPick = menu();
while (userPick != 10){ //user exists with a choice of 10
switch (userPick){
case 1:
System.out.println("User picked 1");
break;
case 2:
...
default:
...
}
userPick = menu();
}
public int menu(){
Scanner scanner = new Scanner(System.in);
System.out.println("Please choose an integer from 0 - 10(quit)");
int answer = scanner.nextInt();
scanner.close();
return answer;
}
Per, Scanner throws NoSuchElementException on nextInt
When you call scanner.close() it closes your underlying stream, which is System.in; once you close System.in the only way to get it back is to restart your program.
Removing the close took care of the issue.
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.