Ali Folks
Ali Folks

Reputation: 11

reading characters from file

I'm having trouble understanding why my code isn't working properly.Just to test it, I have only five characters in my txt file. I know that characters are being put into array I create, but I am not sure why wouldnt it print them. Thanks!

        catch (IOException exception) {
            System.err.println(exception);
        }// catch

        finally {
            try {
                if (fileInput != null)
                    fileInput.close();
            }// try

            catch (IOException exception) {
                System.err.println("error!" + exception);
            }// catch

        }// finally

    }// main
}// TestCode

Upvotes: 0

Views: 85

Answers (1)

Reimeus
Reimeus

Reputation: 159754

You have a for loop inside your while reader loop. Better to use:

    int index = 0;
    while ((character = fileInput.read()) != -1) {
        inputArray[index] = (char) character;
        index++;
    }

Also ensure that you dont exceed the inputArray size. You may want to consider a using a List type if this data is required to grow.

Upvotes: 2

Related Questions