0

I'm trying to read all integers from a file into an ArrayList in the @BeforeClass of a java JUnit test. For testing purposes, I am then simply trying to print all values of the arraylist to the screen. Nothing is being output however. Any input would be greatly appreciated.

public class CalcAverageTest
{

static List<Integer> intList = new ArrayList<Integer>();

@BeforeClass
public static void testPrep() {
    try {
        Scanner scanner = new Scanner(new File("gradebook.txt"));
        while (scanner.hasNextInt()) {
            intList.add(scanner.nextInt());

        }
        for (int i=0;i<intList.size();i++) {
            System.out.println(intList.get(i));
        }
    } catch (IOException e) {
        e.printStackTrace();   
    } catch (NumberFormatException ex) {
        ex.printStackTrace();
    }            
 }
}
7
  • 2
    Have you tried using a debugger? Commented Oct 30, 2018 at 0:08
  • If the file is empty, or doesn’t start with an int, nothing will be printed, just as you are experiencing. Commented Oct 30, 2018 at 0:16
  • where is your 'gradebook.txt' file located? Is it on the classpath / in the resources directory? this might be helpful : programmergate.com/java-read-files-classpath Commented Oct 30, 2018 at 0:27
  • @Fabian Yes it's in the same directory. Commented Oct 30, 2018 at 0:33
  • @AJNeufeld you may have hit the nail on the head with your comment. I have some strings to skip over before the integers. Commented Oct 30, 2018 at 0:33

2 Answers 2

1

(promoting a comment to an answer)

If gradebook.txt is an empty file, or starts with something that does not parse as an int, such as text or comments at the top of the file, then scanner.hasNextInt() will immediately return false, and intList will remain empty. The for loop will then loop over the empty list zero times, and no output will be generated, as observed.

I have some strings to skip over before the integers.

scanner.readLine() can be used to skip over comment lines before the numbers. If it is not a set number of lines that need skipping, or if there are words on the line before the numbers, we would need to see a sample of the input to advise the best strategy for finding the numbers in the input file.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to iterate over the file till the last line, so you will need to change the condition in the loop and use .hasNextLine() instead of .nextInt()

while (scanner.hasNextLine()) {
    String currLine = scanner.nextLine();
    if (currLine != null && currLine.trim().length() > 0 && currLine.matches("^[0-9]*$"))
        intList.add(Integer.parseInt(currLine));
    }
}

Here, we read each line and store it in currLine. Now only if it contains a numeric value it is added to the intList else it is skipped. ^[0-9]$* is a regex used to match only numeric values.

From the docs, hasNextLine()

Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.

8 Comments

The code compiled just fine. The OP is using hasNextInt() (which returns a boolean) and nextInt(), which would work if the file was composed of white space (including new lines) separated numbers. Based on the comment that there are string at the beginning of the file, your answer would fail with an InputMismatchException in the first nextInt() call.
Yeah, my bad. Edited the answer.
Better, but still assumes the numbers are entered one-per-line. Your code won’t read any line with more than one number. Looping while (hasNext()), and then using if(hasNextInt()) to discriminate between ints (which you would read with nextInt()) and non-ints (which you’d skip with next()) would be better still. No regex and no Integer.parseInt(). Best would be waiting for a text file sample, so the code can be correct, instead of being WAGs.
The currLine != null test is unnecessary given hasNextLine() returned true.
Agreed. We can tweak that accordingly. Let's wait for the input file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.