java.util.NoSuchElementException on scanner -
I have a scanner called input with this code:
scanner input = new Scanner (System.in); Int cFreq = 0; System.out.println ("Enter the symbol that you want to find the frequency of:"); Char s = 'a'; Symbol loop: while (s == 'a') {try {s = input.next ("."). ToLowerCase (). CharAt (0); } Hold (InputMismatchException e) {System.out.println ("Please enter a valid symbol!"); Input.next (); } Switch (check symbol (s)) {case 0: s = 'a'; break; Case 1: Break Symbol Loop; }} For (int i = 0; i However when it reaches the line, where it reads from the scanner, it ends with this error:
Exceptions in the thread "main "Java.util at Java.util.Scanner.throwFor (Scanner.java:862) at NoSuchElementException java.util.Scanner.next (Scanner.java1485) at java.util.Scanner.next (Scanner.java1418) Home. On the frequency (Home.java:118) Menu.select (Menu.java:58) at Menu.view (Menu.java:17) at Home.main (Home.java.2) (Home class is named) I do not know what this is the reason and would appreciate some help! :) Thanks! Edit: I have also tried with this code: string str = input.nextLine (); In the same method, but it throws the same error.
If the user does not match the data "". " regex (possibly surrounded by delimiter - white space) next (" ") does not thrust NoSuchElementException , not InputMismatchException . < / P>
NoSuchElementException Exception is thrown when the scanner can not expect too much data, in this case it is certain that the next element will not exist in the next element. Only if the scanner knows that the source of the data will not have more elements, which is in the case of the stream When we read the entire contents of the stream (such as in the case of FileInputStream) or it will be closed.
System.in was closed, so read Next and not more data. To avoid such cases, the currents using System.in should be avoided as you will not be able to open it again in your application.
Comments
Post a Comment