> Help with my java code? I'm writing a program reads a txt file and outputs the number of pieces of "good"

Help with my java code? I'm writing a program reads a txt file and outputs the number of pieces of "good"

Posted at: 2014-12-18 
Did you leave something out here?

// Consume badData

badData++;

Because incrementing badData most definitely does not consume the bad data.

Suppose your file contains: 1 2 FOO

You will read the 1 and 2, but FOO will throw the InputMismatchException. But it will NOT advance the input pointer. So when you loop around again, and try to read the next int, it will see FOO again and throw another exception. And so on....infinite loop.

You need to add the code that goes with the comment:

// Consume badData

The program program the reads a txt file and outputs the number of pieces of "good" and "bad" data with good data being any non-negative integer number and bad data being anything else My code should be all right but do anything see wrong with my code, also I need to create a txt file and put a bunch of number in there, does it matter where I put the txt file in? do I need to link in?

import java.io.*;

import java.util.*;

public class DemoEx {

public static void main(String[] args) throws IOException, BPIllegalValueException {

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

int badData = 0;

int goodData = 0;

while (in.hasNext()) {

try {

int value = in.nextInt();

if (value < 0)

throw new BPIllegalValueException("value cannot be less than zero");

else goodData++;

} catch (InputMismatchException ex) {

// Consume badData

badData++;

}

}

System.out.println("gooddata" + goodData);

System.out.println("baddata" + badData);

}

private static class BPIllegalValueException extends Exception {

public BPIllegalValueException() {

}

private BPIllegalValueException(String value_cannot_be_less_than_zero) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

}

}

}