Results 1 to 14 of 14

Thread: End of file

  1. #1
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default End of file

    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""

    any idea whats going on?

    edit: I have a few negative numbers, but 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);
    }
    }
    Last edited by Reine; 09-28-2011 at 11:55 PM.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: End of file

    What class are you using for inName? If it's a Scanner, for example, you can do:
    Java Code:
    while (inName.hasNext() && inName.nextInt != 0) { ... }

  3. #3
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: End of file

    oh i'm not using scanner, just the regular public class. could you write it in that form by any chance?

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: End of file

    Which regular public class? Look at the API for whichever class it is that inName is an instance of.

  5. #5
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: End of file

    Only been doing this for a few weeks, but I think it's an I/O class :)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: End of file

    Could you post the statement where the definition for the inName variable and also the all the import statements from the front of your program.?

  7. #7
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: End of file

    Hope this is what you're looking for!
    import java.io.*;
    import java.util.*;


    public class TextFile {

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: End of file

    That's the import statements. Now the statement where inName is defined.

  9. #9
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: End of file

    Could this be it?

    TextFile inName = new TextFile (true, "data.txt");

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: End of file

    Yes, that is it.
    It looks like the code for the readChar() method is in your program. You should be able to look at it to see where
    the programme prints "end of file while reading from file "data.txt""
    Use your editor's Find or Search to look for "end of file while".

  11. #11
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: End of file

    Ah right I deleted it and it worked, Is that OK? Also, would you happen to know how to skip the - character for the negative numbers in my text file?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: End of file

    Is that OK?
    Where did you get the code? Why are you using it instead of using one of the Java SE classes?
    how to skip the - character for the negative numbers
    What is the problem? Putting a - in front of a number is the way to indicate that the number is negative.

  13. #13
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: End of file

    Well my teacher gave us a file with all these codes and he never explained to us why we're not using other classes. But the problem lies in the fact that my program is supposed to state the number of negative numbers (all single digit) there are in my text file. My counter, however, counts the - sign as a character. If finding a way to skip that is beyond beginner's level I'll just assume it's ok to divide the number of characters by 2

  14. #14
    Reine is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: End of file

    Nevermind, I found a solution. Thanks for all your help tho :)

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 07:50 PM
  2. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  3. Replies: 1
    Last Post: 08-04-2010, 03:24 PM
  4. Replies: 0
    Last Post: 07-15-2010, 01:23 PM
  5. Replies: 0
    Last Post: 02-11-2009, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •