Search code examples
javajava-6

Java program keep running, no compiler's error


I'm trying to write a code for selecting features from a txt file. i.e. size = 1.4356474
species = fw, wevb, wrg , gwe ....

this is the code I wrote so far:

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;
    }

}

It doesn't give me any compiler's error, but when I launch it, it keeps running and doesn't end. Any help?


Solution

  • Your loop is not reading line after line, it needs to call read line on each iteration, it should be :

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