mark602
mark602

Reputation: 11

How to continue looping through DataInputStream?

I am reading a binary file and I have to parse through the headers in this file.

I have a DataInputStream set up and I need to get it to continue looping through the file until the end. I am new to Java so not sure how I would carry this over from my C# experience. At the moment I have the following code:

while (is.position != is.length) {                              
    if ( card == Staff) {
        System.out.print(card);
    } else if ( card == Student ) {
        System.out.print(card);
    } else if (card == Admin) {
        System.out.print(card);
    } else {
        System.out.print("Incorrect");
    }
}

is is the input stream I created, the only error I have is in the first line where the while loop starts under position and length it says they cannot be resolved or is not a field.

Upvotes: 1

Views: 206

Answers (1)

Edd
Edd

Reputation: 3822

Looking at the docs it doesn't look like DataInputStream has a position field. You could possibly use one of the other methods to check whether there is more data available, but it's not clear from your code sample what you're trying to do.

At present there are a number of issues I can see with your code:

  1. If card, Staff, Student and Admin are of type String, then you need to compare them using the equals(String s) method, not the == reference equality (ie. card.equals(Staff) rather than card == Staff)
  2. You don't seem to iterate in your loop. If you don't do anything to change the value of is.position (I know this doesn't actually exist, but hypothetically speaking...) then if you can enter the loop you'll never leave it.
  3. You don't change the value of card. If you do iterate some fixed number of times, you're going to just have identical output printed over and over again, which probably isn't what you intended.

Upvotes: 1

Related Questions