简体   繁体   中英

Why can I not let my program run my method over and over (while loop)?

This is a diary program which allows you to write something in your diary (obviously). After typing enter and pressing enter, the page closes and its gonna be safed in a list. My problem is that it only runs once when I have Pages(); in the main method, so I tried this loop. It doesnt work for me and i dont know why. Need some help

import java.util.ArrayList;
import java.util.Scanner;

public class NotizbuchKlasse{
    public static void Pages() {
        System.out.println("day 1 : Write something in your diary.");
        System.out.println("Write enter if you are done writing.");
        ArrayList<String> List = new ArrayList<String>();
        String ListInList;
        Scanner write = new Scanner(System.in);
        do {
            ListInList = write.next();
            List.add(ListInList);
        } while (! ListInList.equals("enter"));
        List.remove(List.size()-1);
        write.close();          
        System.out.println("This is now your page. Your page is gonna be created after writing something new.");
        System.out.println(List);
    }

    public static void main(String[]Args){
        boolean run = true;
        do{
            Pages();
        } while(run);
    } 
}

Error:

This is now your page. Your page is gonna be created after writing something 
new.
Exception in thread "main" [hello]
day 1 : Write something in your diary.
Write enter if you are done writing.
java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at NotizbuchKlasse.Pages(NotizbuchKlasse.java:12)
    at NotizbuchKlasse.main(NotizbuchKlasse.java:24)

You need to check whether there is something to read before you read it. You're not currently, and that's why you're getting a NoSuchElementException .

You do this via Scanner 's has* methods.

For example:

    ArrayList<String> List = new ArrayList<String>();
    Scanner write = new Scanner(System.in);
    while (write.hasNextLine()) {
        String ListInList = write.nextLine();
        if (ListInList.equals("enter")) break;
        List.add(ListInList);
    }
    // No need to remove the last item from the list.

But also, I notice that you have a loop in your main method, where you call Pages() in that loop. If you close write , you also close System.in ; once a stream is closed, you can't re-open it. So if you try to read things from System.in the next time you call Pages() , the stream is already closed, so there's nothing to read.

Simply don't call write.close() . You shouldn't close streams that you didn't open in general; and you didn't open System.in (the JVM did when it started up), so don't close it.

You want to be using a while loop like this:

while (write.hasNextLine()) {
  ListInList = write.nextLine();
  if (doneWriting(ListInList)) { // Check for use of enter.
    break; // Exit the while loop when enter is found.
  }
  List.add(ListInList); // No enter found. Add input to diary entry.
}

where doneWriting() is a method (that you write!) which checks to see if the user has typed enter .

Here is the documentation for the next() method of Scanner. If you read it, you will see that it throws the exception you are getting when it runs out of tokens.

If you want a little bit more a casual explanation here is a question that was asked previously about next() versus nextLine() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM