1

I'm running a bit of code to read data from a file and then split each line into an array for easier calculating.

However, I've come across a problem where my while loop isn't continuing despite the fact I know there is more data in the text file.

Any help with the problem would be greatly appreciated!

public void test() throws IOException {
    FileReader fr = null;
    Scanner sc = null;

    String file_name = "data.txt";

    try {
        fr = new FileReader(file_name);
        sc = new Scanner(fr);


        while(sc.hasNextLine()){
            int year = 0;
            int month = 0;
            double sales_total = 0;
            String lines[] = sc.nextLine().split(" ");
            for (int i = 0; i < lines.length; i++){
                int value = Integer.valueOf(lines[i]);
                if (value > 2000) {
                   year = value;
                }
                else if (i == 1){
                    month = value;
                }
                else {
                    sales_total += value;
                }
                System.out.println(sales_total);
            }
            System.out.println(year + " - " + month + " - "+sales_total);
        }
    } 
    catch(IOException e) {
        System.out.println(e);
    } 
    finally {
        if (fr != null) { fr.close();}  
        if (sc != null) { sc.close();}
    }
}

data.txt

2016 02 5 18 12
2016 12  14  10   3   5   6   8  20   7  10  
2016 09  17   3  16  18  19  10   6  10  
2016 10   5   3  12   4  12  15  
2016 11  18  19  16   2   
2017 01   5   5   4  11
2017 02  41   3
2017 06   3   6  18 
2017 07  15  18  15  12   9  15
2017 11   9   8   9   4  20  20  19   3 
2018 01  19  19   3  10  20  20  18  14   3   
2017 12   8  13  18   6   
2018 11  10   2  11   8  17   8   6  18  15   1 
5
  • Please provide small sample input file Commented Apr 2, 2019 at 23:10
  • 1
    Can you provide a minimal reproducible example? Right now it's just your word against the JVM's. Commented Apr 2, 2019 at 23:12
  • Apologies! I have now updated the OP with the contents of the sales-data.txt file. Commented Apr 2, 2019 at 23:15
  • Does the code throw any exception? Commented Apr 2, 2019 at 23:22
  • It was throwing the following exception... java.lang.NumberFormatException: For input string: "" I fixed this by checking for multiple spaces or tabs. Commented Apr 3, 2019 at 0:04

1 Answer 1

1

sc.nextLine and sc.hasNextLine are blocking calls by design, so it will remain blocking until new input comes in. see Scanne.hasline() documentation

Could it be that some lines in code are missing CR LF delimiter? I took you sample, opened it with notepad++ and viewed the whitespace (under view->show symbol). and saw the following:

...
2017 11   9   8   9   4  20  20  19   3 CR LF
2018 01  19  19   3  10  20  20  18  14   3 CR LF  
2017 12   8  13  18   6   CR LF
2018 11  10   2  11   8  17   8   6  18  15   1
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that! It was the spacing in between the integers in the data file. I changed the exception format around and it was throwing the following exception java.lang.NumberFormatException: For input string: "" I fixed this by checking for multiple spaces or tabs with the following bit of code. String lines[] = sc.nextLine().split("\\s+");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.