exception in thread 22main 22 java util nosuchelementexception

Solutions on MaxInterview for exception in thread 22main 22 java util nosuchelementexception by the best coders in the world

showing results for - "exception in thread 22main 22 java util nosuchelementexception"
Tomas
26 Jun 2017
1//broken:
2public static void main(String[] args){
3	String firstLine=readLine();
4  	String secondLine=readLine();//will not work
5}
6
7public static String readLine(){
8	Scanner scan=new Scanner(System.in);
9	String line=scan.nextLine();//throws a NoSuchElementException the second time the method is called
10	scan.close();//this also closes System.in
11  	return line;
12}
13//working: reuse the Scanner:
14public static void main(String[] args){
15  	Scanner scan=new Scanner(System.in);
16	String firstLine=readLine(scan);
17  	String secondLine=readLine(scan);//will not work
18 	scan.close();//this also closes System.in	
19}
20
21public static String readLine(Scanner scan){
22	String line=scan.nextLine();//throws a NoSuchElementException the second time the method is called
23  	return line;
24}
25