Turn on thread page Beta
    • Thread Starter
    Offline

    2
    ReputationRep:
    Hey guys.
    So my programme is supposed to read characthers from a text file, but how do I end it? So my sequence of numbers ends with 0 and I want that to be enough to end the reading. But the programme prints "end of file while reading from file "data.txt""



    Additionally, I have a few negative numbers in my text file. I expect that the counter will also count the - as a character, how can i stop it from doing that?

    thanks!

    do
    {
    x = inName.readChar();
    counter = counter + 1;
    } while ( x != 0);


    System.out.println (counter);
    }
    }
    Offline

    15
    ReputationRep:
    For one thing, you're reading in the character as a char, but your while loop looks to compare it to an int rather than a char.
    • Thread Starter
    Offline

    2
    ReputationRep:
    (Original post by estel)
    For one thing, you're reading in the character as a char, but your while loop looks to compare it to an int rather than a char.
    but I am counting the number of characters so aren't I supposed to do that?
    Offline

    0
    ReputationRep:
    I don't program in java, but estel is completely right. Assuming that a character evaluates to TRUE, and whatever is used to mark the end of a string is FALSE (as it is with most of the languages I use), you can fix the problem by changing while( x != 0 ) to while (x). You should make sure to initialise counter to 0, too.
    • Thread Starter
    Offline

    2
    ReputationRep:
    (Original post by Luke_C)
    I don't program in java, but estel is completely right. Assuming that a character evaluates to TRUE, and whatever is used to mark the end of a string is FALSE (as it is with most of the languages I use), you can fix the problem by changing while( x != 0 ) to while (x). You should make sure to initialise counter to 0, too.
    I didn't copy the whole programme but the counter is indeed at 0 Only done this for a couple of weeks and I wasn't expecting estel to be wrong, just got my thinking wrong and hopefully it can be corrected. Anyway what you said makes sense but putting only while (x) is not enough as it must be a boolean
    Offline

    0
    ReputationRep:
    That makes it more complicated. If there's an EOF (Test for end of file) function in java, I'd use that. If not, convert x to an integer, while testing for errors. You should get errors, since you won't be able to convert '-' to a whole number. The syntax for converting to an integer is likely to be "int(x)" (function) or "(int) x" (cast).
    Offline

    15
    ReputationRep:
    The "safest" EOF character to use in Java is probably -1; but in some circumstances it might be null or just throw an EOFException.
    Offline

    2
    ReputationRep:
    My advice would be to use something like a Scanner object, which handles EOF for you, and can deal with minus signs for numbers.

    Have a read of the documentation - there are some simple examples at the top.
    • Thread Starter
    Offline

    2
    ReputationRep:
    Thanks to all of you! I managed to solve it in the end; i was a bit silly to say the least haha
 
 
 
Poll
Black Friday: Yay or Nay?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.