简体   繁体   English

为什么不能让我的程序一遍又一遍地(while循环)运行我的方法?

[英]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(); 我的问题是,只有我拥有Pages()时,它只能运行一次; in the main method, so I tried this loop. 在main方法中,所以我尝试了这个循环。 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 . 您当前不是,这就是为什么您遇到NoSuchElementException的原因。

You do this via Scanner 's has* methods. 您可以通过Scannerhas*方法执行此操作。

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. 而且,我注意到您的main方法中有一个循环,您在该循环中调用Pages() If you close write , you also close System.in ; 如果您关闭write ,那么您还将关闭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. 因此,如果您下次尝试从System.in读取内容,则在下次调用Pages() ,流已关闭,因此没有任何要读取的内容。

Simply don't call write.close() . 只是不要调用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. 并且您没有打开System.in (JVM在启动时就打开了),因此请不要关闭它。

You want to be using a while loop like this: 您想使用如下的while循环:

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 . 其中doneWriting()是一种方法(由您编写!),该方法检查用户是否键入enter

Here is the documentation for the next() method of Scanner. 这是 Scanner的next()方法的文档 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() . 如果您想要更多一点随意的解释, 这是先前询问的关于next()nextLine()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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