简体   繁体   English

使用代码终止Eclipse(Java)

[英]terminating eclipse with using code (in java)

I've been always using while(scanner.hasNext()) {} method to read from the file and then execute the program this whole time. 我一直在使用while(scanner.hasNext()){}方法从文件中读取内容,然后一直执行该程序。 But I've notice that CPU usage was drastically increasing even when the program (Eclipse) finishes the process, and it wont stop entirely until I press this button. 但是我注意到,即使程序(Eclipse)完成了该过程,CPU的使用量仍在急剧增加,并且直到我按下此按钮时,它才会完全停止。 buttonImage 按钮画面

I also used this function, System.exit(0), to stop the program, but I still have to press the button. 我还使用了System.exit(0)这个函数来停止程序,但是我仍然必须按下按钮。

Is there any flaw in my code that makes the program not to stop. 我的代码中是否有任何缺陷使程序无法停止。

public class HW3
{

    /*
      Description
    */
    public static void main(String[] args) throws FileNotFoundException 
    {
        final File file1 = new File(args[0]);
        final File file2 = new File(args[1]);
        final Scanner sc1 = new Scanner(file1);
        HW3 instance = new HW3 ();
        while (sc1.hasNext()) {
            print(instance.splitSentence(sc1.nextLine()));
        }
        sc1.close();
        final Scanner sc2 = new Scanner(file2);
        while (sc2.hasNext()) {

        }
        System.exit(1);
    }
    public void constructTreeNode(String[] stringArray) {

    }
    private String[] splitSentence (String sentence) {
        int wordCount = 1;
        for (int i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(i) == ' ') {
                wordCount++;
            }
        }
        String[] arr = new String[wordCount];
        wordCount = 0;
        String temp = "";
        for (int i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(i) == ' ' || i == sentence.length() - 1) {
                arr[wordCount] = temp;
                wordCount++;
                temp = "";
            } else {
                temp += sentence.charAt(i);
            }
        }
        return arr;
    }
    private static void print (String[] arr) {
        for(String e : arr) {
            System.out.println(e);
        }
    }
}

The issue you are having is with this segment of code. 您遇到的问题是这段代码。

final Scanner sc2 = new Scanner(file2);
while (sc2.hasNext()) {

}
System.exit(1);

You create a scanner and enter a loop until the scanner has no new data; 您创建一个扫描仪并进入循环,直到该扫描仪没有新数据为止。 but you don't read any data from the Scanner. 但您不会从扫描仪读取任何数据。 As long as the File file2 wasn't empty, you would enter an infinite loop, and the System.exit(1); 只要File file2不为空,您将进入无限循环,然后输入System.exit(1);。 code would be unreachable. 代码将无法访问。

This is also the reason why your code never completes, because it is stuck in this loop. 这也是您的代码无法完成的原因,因为它被卡在此循环中。 The code will complete if it reaches the end of the main() method entry point to your program (That is to say a call to System.exit(int n) is not required unless you specifically want to indicate error). 如果代码到达程序的main()方法入口点的末尾,则代码将完成(也就是说,除非您特别想指出错误,否则不需要调用System.exit(int n))。

The fix: 解决方法:

final Scanner sc2 = new Scanner(file2);
while (sc2.hasNext()) {
    String line = sc2.nextLine();
}
System.exit(1);

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

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