Continue reading the file after the capture exception

advertisements

Ive got a data.txt file which consist of the numbers
12 45 345 500 45.67684 33

once readData method is executed it should only print the values which are integers and print element not valid for any other type then continue like this

readData("data.txt") will print out the following: 12 45 345 500 element not valid 33

My problem is that once the element not valid statement is printed my code does not go on to print 33 it just stops at 12 45 345 500 element not valid

import java.io.*;
import java.util.*;

public class Ex7 {

    public static void readData(String nameFile){

       try{
        BufferedReader br = new BufferedReader(new FileReader(nameFile));
        String line = br.readLine();
        int i = 0; 

         while((line != null)){
            try{
            String[] lines = line.split(" ");
            int[] result = new int[lines.length];
            result[i] = Integer.parseInt(lines[i]);

            if(result[i] ==(int)result[i]){
               System.out.print(result[i] + " ");
            }
            i++;
        }

       }
        }catch(NumberFormatException e){
            System.out.println("element not valid " );
         }catch(IOException e){
          System.out.println("IO error");
          System.exit(0);
        }
    }

    public static void main (String[] args){
        readData("data.txt");
    }
}


The reason for your question is that you do not understand the flow of control that happens.

It is pretty simple: you have a loop that reads from a file. But your catch is outside of that loop. So, when something throws, and you "catch" outside of the loop, that loop is "over"; there is no way getting back.

So, immediate answer is: move the try/catch for the NumberFormatException ... to the one place where it actually can occur.:

try {
  result[i] = Integer.parseInt(lines[i]);
 } catch (NumberFormatException ...

But of course ... that directly leads to the next problem with your code: you are using an array to store your "valid" inputs ... arrays have a fixed size. How do you know in advance how many members will be valid? You don't. Thus: you have to change from using an array to a dynamic list, like ArrayList. Now you can just add "valid" lines. But well, that doesn't really matter anyway. Because your method is not returning the collected values. But if data isn't used; there is no point in collecting it in the first place.

And the answer beyond the code changes you have to make: turn back to the books; and learn how the constructs/concepts that you are using actually work. There is no point in blindly typing down try/catch, just because they show up somewhere, or because your IDE tells you that somehow need to do something about exceptions. Meaning: when you write down code ... make sure that you really really understand what this code is doing. You know, like that other statement if (result[i] == (int) result[i]) ... that will always be true; and simply doesn't make any sense at all.