This question has already been answered:

I want to close my Scanner if I enter the word Stop

 Scanner sc = new Scanner (System.in); String[] words1 = new String[25]; for (int i = 0; i < 25; i++) { if (sc.nextLine().equals("Stop")) { System.exit(0); } else { words1[i]=sc.nextLine(); } } for (int i = 0; i < words1.length; i++) { System.out.print(words1[i]+" "); } 

But it does not work. Data entry continues if I enter Stop. + array is not displayed.

Reported as a duplicate by Sergey Gornostaev java May 10 '18 at 11:50 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • same. The input continues - Oleg Khegay
  • one
    Can not reproduce. Input stops at “Stop”. Describe in more detail: how to launch what exactly you enter. And the fact that the array is not printed is because you are closing the virtual machine on “Stop”. You need to exit the loop instead ( break; ) - default locale

1 answer 1

The problem is most likely that you use == to compare words, and Java by == links are compared, use sc.nextLine().equals("Stop")

Here is a fully working code

  Scanner sc = new Scanner(System.in); String[] words1 = new String[25]; for (int i = 0; i < 25 && sc.hasNextLine(); i++) { String nextLine = sc.nextLine(); if (nextLine.equalsIgnoreCase("stop")) { break; } words1[i] = nextLine; } for (String aWords1 : words1) { System.out.print(aWords1 + " "); } 

The first problem you had was that you did the iterations, even though the lines were already gone. Corrected by adding && sc.hasNextLine() .

The second problem was that you wrote this

  if (sc.nextLine().equals("Stop")) { System.exit(0); } else{ words1[i]=sc.nextLine(); 

You called nextLine , the first time the line was, and the second time it did not, because the nextLine does not always return the same value in one iteration, this is not an idempotent method.

Well, another problem was that you called System.exit(0) , if you met the word stop , and this completes jvm execution, and you would never get to a loop that displays the values ​​of the array

Additionally. Scanner cannot call the close method when you pass it System.in , because it closes the stream that was passed to it and so you can accidentally close System.in , and then look for an error for a long time

  • yes it turned out. but when I try to output an array after this (through a loop) the same problem happens on the screen - input continues + the array is not displayed. Scanner sc = new Scanner (System.in); String[] words1 = new String[25] ; for (int i = 0; i < 25; i++) { if (sc.nextLine().equals("Stop")) { System.exit(0); } else{ words1[i]=sc.nextLine(); }} for (int i = 0; i < words1.length; i++) { System.out.print(words1[i]+" "); } Scanner sc = new Scanner (System.in); String[] words1 = new String[25] ; for (int i = 0; i < 25; i++) { if (sc.nextLine().equals("Stop")) { System.exit(0); } else{ words1[i]=sc.nextLine(); }} for (int i = 0; i < words1.length; i++) { System.out.print(words1[i]+" "); } - Oleg Khegay
  • Show me how to output along with the code for filling the array, but I don’t quite understand - Vyacheslav Gusser
  • corrected the code in the main question - Oleg Khegay
  • Now I will launch your code - Vyacheslav Gusser
  • I updated the answer, you can try to run the code - Vyacheslav Gusser