How to finish scanning when input is complete?

public static void main(String[] args) { Scanner scan = new Scanner(System.in); try { while (scan.hasNextLine()){ String line = scan.nextLine().toLowerCase(); System.out.println(line); } } finally { scan.close(); } } 

Just wondering how can I finish the program after I finish entering the input? Since the scanner will continue to work after a few "Enter", assuming that I will continue to enter data ... I tried:

 if (scan.nextLine() == null) System.exit(0); 

and

 if (scan.nextLine() == "") System.exit(0); 

They didn’t work ... The program continues and mixes with the original intention,

+8
source share
6 answers

The problem is that the program (like yours) does not know that the user has completed data entry, unless the user ... somehow ... does not say so.

The user can do this in two ways:

  • Enter the “end of file” marker. On UNIX and Mac OS, this is (usually) CTRL + D and on Windows CTRL + Z. This will cause hasNextLine() false .

  • Enter some special input that the program recognizes as meaning "I am done." For example, it can be an empty string or some special value, for example, "exit". The program must verify this specifically.

(You can also use a timer and assume that the user has completed work if he does not enter the input within N seconds or N minutes. But this is not a user-friendly way to do this.)


The reason your current version crashes is because you use == to check for an empty string. You should use equals or isEmpty methods. (See How do I compare strings in Java? )

Other things to consider are case sensitivity (for example, “exit” versus “exit”) and the effect of leading or trailing spaces (for example, “exit” versus “exit”).

+23
source

String comparisons are done using .equals() , not == .

So try scan.nextLine().equals("") .

+4
source

You will need to find a specific pattern that indicates that the end of your input says, for example, "##"

 // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); try { while (scan.hasNextLine()){ String line = scan.nextLine().toLowerCase(); System.out.println(line); if (line.equals("##")) { System.exit(0); scan.close(); } } } finally { if (scan != null) scan.close(); } 
+1
source

With this approach, you must explicitly create an exit command or exit condition. For example:

 String str = ""; while(scan.hasNextLine() && !((str = scan.nextLine()).equals("exit")) { //Handle string } 

In addition, you should handle strings equal to the cases with .equals() not == . == compares the addresses of two strings, which, if they are not actually the same object, will never be true.

0
source

In this case, I recommend using do, while instead of while.

  Scanner sc = new Scanner(System.in); String input = ""; do{ input = sc.nextLine(); System.out.println(input); } while(!input.equals("exit")); sc.close(); 

To exit the program, you just need to assign a title to the line, for example. Exit. If the input is equal to the output, the program will exit. In addition, users can press the control + c button to exit the program.

0
source

You can check the next line of input from the console and check your completion record (if any).

Suppose your entry is completed with "quit", then you should try this code: -

 Scanner scanner = new Scanner(System.in); try { while (scanner.hasNextLine()){ // do your task here if (scanner.nextLine().equals("quit")) { scanner.close(); } } }catch(Exception e){ System.out.println("Error ::"+e.getMessage()); e.printStackTrace(); }finally { if (scanner!= null) scanner.close(); } 

Try this code. Your ending line should be entered by you when you want to close / close the scanner.

0
source

All Articles