Java 程序保持 运行,没有编译器错误

Java program keep running, no compiler's error

我正在尝试编写用于从 txt 文件中选择功能的代码。 即大小 = 1.4356474
物种 = fw, wevb, wrg , gwe ....

这是我到目前为止写的代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.concurrent.ExecutionException;




public class Metodi {



    public static void main (String[] args) {
        String volume = findVolume();
        System.out.println(volume);

    }







    public static String readSpecification() {
        String spec = "";
        // trying to read from file the specification...
        try {
            BufferedReader reader = new BufferedReader(new FileReader("Gemcitabine.txt"));
            String line = reader.readLine();
            while(line!=null) {
                spec += line + "\n";
                line = reader.readLine();
            }        
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return spec;
    }

    public static String findVolume () {

        String res = "";
        String vol = "volume";

    try {
        BufferedReader reader1 = new BufferedReader(new FileReader("Sample.txt"));
        String line1 = reader1.readLine();
        while(line1!=null) {
            if(line1.toLowerCase().indexOf(vol) != -1) {
                String[] str = line1.split("=");
                res = str[1].split(" ")[0];
            }
        }
         } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return res;
    }

}

它没有给我任何编译器错误,但是当我启动它时,它会保持 运行 并且不会结束。 有帮助吗?

findVolume() 中,您在 while 条件中检查 line1 != null。 您永远不会在循环中更改 line1 。因此,它永远不会等于 null 并且循环不会终止。

你的循环不是逐行读取,它需要在每次迭代时调用 read line,它应该是:

String line1 =;
    while((line1 = reader1.readLine()) != null) {
        if(line1.toLowerCase().indexOf(vol) != -1) {
            String[] str = line1.split("=");
            res = str[1].split(" ")[0];
        }
    }