-3

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?

Pino
  • 619
  • 1
  • 8
  • 25
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Feb 27 '16 at 21:44

2 Answers2

2

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];
        }
    }
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31
0

In findVolume(), you check line1 != null in your while-condition. You never change line1 within the loop. Thus, it will never be equal to null and the loop won't terminate.

jp-jee
  • 1,502
  • 3
  • 16
  • 21