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