Answers for "exception in thread "main" java.util.nosuchelementexception"

1

exception in thread "main" java.util.nosuchelementexception

//broken:
public static void main(String[] args){
	String firstLine=readLine();
  	String secondLine=readLine();//will not work
}

public static String readLine(){
	Scanner scan=new Scanner(System.in);
	String line=scan.nextLine();//throws a NoSuchElementException the second time the method is called
	scan.close();//this also closes System.in
  	return line;
}
//working: reuse the Scanner:
public static void main(String[] args){
  	Scanner scan=new Scanner(System.in);
	String firstLine=readLine(scan);
  	String secondLine=readLine(scan);//will not work
 	scan.close();//this also closes System.in	
}

public static String readLine(Scanner scan){
	String line=scan.nextLine();//throws a NoSuchElementException the second time the method is called
  	return line;
}
Posted by: Guest on April-06-2021

Code answers related to "exception in thread "main" java.util.nosuchelementexception"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language