Nate
Nate

Reputation: 412

Using a try/catch block to catch exceptions with input from a file

I'm pretty sure my problem is existent in my try block somewhere, but I can't seem to pinpoint it. What I'm doing is reading from a file, and checking the input. If the input is >0, I'd like to increment goodData, and if not, it should either throw BPIllegalValueException for values <0, or InputMismatchException for things that aren't integers. I was hoping you guys could help out.

EDIT: Professors Instructions:

In your program, you need two exceptions to handle two kinds of bad data. For the non-integer reading, you can use nextInt() method to read an integer from the file, if the data is not an integer, InputMismatchException will be thrown. Your program must catch this exception. To handle the negative reading, you may want to declare your own Exception class: BPIllegalValueException (you are expected to write BPIllegalValueException.java that contains the class), and threw this exception when a negative value is read. Note: your program should not terminate when a bad data is read. Therefore, you must use try/catch block to handle the bad data. Your program is expected to print out (on the screen) the number of good BP readings and the number of bad data.

My code is as follows:

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

public class DemoEx {

public static void main(String[] args) throws IOException
{
    FileReader fr = new FileReader("BP.txt");
    BufferedReader br = new BufferedReader(fr);
    Scanner in = new Scanner(new FileReader("BP.txt"));
    int badData = 0;
    int goodData = 0;
    int data;
    String str = "";

    while((str = br.readLine()) != null)
    {   
        try
        {
            data = in.nextInt();
            if(data>0)
                goodData++;
            else if(data<0)
                throw new BPIllegalValueException(data);
            else
                throw new InputMismatchException();
        }

        catch(InputMismatchException ex)
        {
            badData++;
        }

        catch(BPIllegalValueException ex)
        {
            badData++;
        }
    }

    System.out.println("The number of good data: " + goodData);
    System.out.println("The number of bad data: " + badData);

}

}

Upvotes: 0

Views: 3745

Answers (3)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78639

You do not need to iterate over two different streams. The scanner should suffice.

    BufferedReader br = new BufferedReader(new FileReader("BP.txt"));
    Scanner in = new Scanner(br);

Also, you should not use exceptions for control flow, use if/else statements instead.

        if(data > 0){
            goodData++;
        }else { //if (data <= 0)
            badData++;
        }

The loop should be controlled by the scanner, which is the one looking for the numbers:

    while(in.hasNext()){ ... }

Finally, if your file contains other tokens different than numbers, make sure you iterate over them, or you will program an endless loop:

        if(in.hasNextInt()){
            data = in.nextInt();
        }else{
            in.next(); //makes sure you move to the next token if it is not an int.
        }

In the overall, something like this:

    BufferedReader br = new BufferedReader(new FileReader("BP.txt"));
    Scanner in = new Scanner(br);
    int badData = 0;
    int goodData = 0;
    int data = 0; //this must be initialized

    while(in.hasNext()){
        if(in.hasNextInt()){
            data = in.nextInt();
            if(data > 0){
                goodData++;
            }else if (data <= 0){
                badData++;
            }
        }else{
            in.next(); //move to the next token when it-s not an int.
        }

    }

System.out.println("The number of good data: " + goodData);
System.out.println("The number of bad data: " + badData);

If you file BP.txt contained something like: 1 2 A B C 4 5 6 -1 -2 C B

It would output:

The number of good data: 8
The number of bad data: 4

Which assume corresponds to the output your are expecting.

Upvotes: 0

AwDogsGo2Heaven
AwDogsGo2Heaven

Reputation: 972

Based on your example, why are you even using Exceptions? What you are doing is not handling an error, it looks like you are performing logic. Errors should be show stoppers, not being used to perform logic in your code. Just increment bad data instead of using exceptions. Exceptions can also slow down your code.

Upvotes: 3

rapadura
rapadura

Reputation: 5300

Why do you throw exceptions when you just want to increase a counter?

It looks like you want,

if (data > 0) 
    goodData++ 
 else 
     badData++;

Now, before you do this make sure data is an integer, which you will get if you do .nextInt if there are any, and all other non-int values will be skipped. So it depends which way you read the data, why a scanner? Why not a buffered reader and readLine then split it do whatever you like?

Upvotes: 3

Related Questions