+ 6

cant find error source from scanner char input

//opens/closes scanner, uppercases user input, and returns it as an int public static int seeker() { Scanner kb = new Scanner (System.in); char guess = Character.toUpperCase(kb.next().charAt(0)); kb.close(); return guess; } I keep getting these messages: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) helppp!!! <3

24th Jul 2018, 12:29 AM
Jeff
Jeff - avatar
7 Réponses
+ 4
Jeff Well, the idea is that you shouldn't be taking input in the function. If possible, create a Scanner object in the main function, reuse the object and close it before the program ends. This way, you solve your problems and also avoid creating new Scanner objects every time the function is called.
24th Jul 2018, 1:33 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Each time your function is entered you allocate a new scanner. Only the first time has access to the input.
24th Jul 2018, 12:56 AM
John Wells
John Wells - avatar
+ 3
My guess is your input is the issue. Link your code by hitting the circle/plus icon so we can run the same code and gives us the input you entered at the prompt. However, if you allocated two Scanner classes, the second will not get any input.
24th Jul 2018, 12:42 AM
John Wells
John Wells - avatar
+ 3
Jeff Have you tried not closing the Scanner object? As I'm pretty sure that is the case, with the input source being closed along with the Scanner.
24th Jul 2018, 12:54 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
The function as it is presented here works just fine. I wasn't able to recreate the errors with just this: import java.util.Scanner; public class Program { public static int seeker() { Scanner kb = new Scanner (System.in); char guess = Character.toUpperCase(kb.next().charAt(0)); kb.close(); return guess; } public static void main(String[] args) { System.out.print(seeker()); } } However, it is possible that you continued to use scanners after you closed the scanner object. Closing the scanner object will also close System.in. https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input
24th Jul 2018, 12:37 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
So here's the full code, its basically a random letter generator that checks against a char input : import java.util.Random; import java.util.Scanner; public class Jeff_McCann_CIS106_HYB1_GuessALetter { //returns random uppercase char public static char pickLetter() { Random rnd = new Random(); char letter = Character.toUpperCase((char) (rnd.nextInt(26) + 'a')); return letter; } //opens/closes scanner, uppercases user input, and returns it as an int public static int seeker() { Scanner kb = new Scanner (System.in); char guess = Character.toUpperCase(kb.next().charAt(0)); kb.close(); return guess; } public static void main(String[] args) { char mystery = pickLetter(); //random picked constant variable boolean found = false; System.out.println("Let's play guess the letter!"); do { System.out.println(); System.out.print("Enter a letter (upper or lower case): "); int x = mystery; //A=65-Z=90 int y = seeker(); if (x == y) { System.out.println("You guessed it!"); System.out.println("The letter was " + mystery); found = true; } else if (x < y){ System.out.println("Your letter comes before the secret letter"); } else { System.out.println("Your letter comes after the secret letter"); } }while(!found); } }
24th Jul 2018, 12:49 AM
Jeff
Jeff - avatar
0
The errors are resolved by leaving the scanner unclosed; but i hate seeing that flag and I know its bad practice? where should I close it?
24th Jul 2018, 1:07 AM
Jeff
Jeff - avatar