1

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.

Edd
  • 3,724
  • 3
  • 26
  • 33
mark602
  • 11
  • 1
  • What datatype is card? Also, you should probably be using an infinite loop and break when the value read is -1. – Elliott Frisch Oct 08 '14 at 12:44
  • card is a string, staff student and admin are enums. I have other code for processing it(I have changed this a bit for privacy), all I really need to change is the is.Position != is.Length, so I can get to to say while the file is not finished continue – mark602 Oct 08 '14 at 12:48

1 Answers1

1

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.
Edd
  • 3,724
  • 3
  • 26
  • 33