Results 1 to 14 of 14
Thread: End of file
- 09-28-2011, 11:43 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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.
- 09-28-2011, 11:57 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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) { ... }
- 09-29-2011, 12:01 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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?
- 09-29-2011, 12:06 AM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: End of file
Which regular public class? Look at the API for whichever class it is that inName is an instance of.
- 09-29-2011, 12:11 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: End of file
Only been doing this for a few weeks, but I think it's an I/O class :)
- 09-29-2011, 12:20 AM #6
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.?
- 09-29-2011, 12:34 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: End of file
Hope this is what you're looking for!
import java.io.*;
import java.util.*;
public class TextFile {
- 09-29-2011, 12:40 AM #8
Re: End of file
That's the import statements. Now the statement where inName is defined.
- 09-29-2011, 12:45 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: End of file
Could this be it?
TextFile inName = new TextFile (true, "data.txt");
- 09-29-2011, 12:50 AM #10
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".
- 09-29-2011, 12:56 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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?
- 09-29-2011, 01:14 AM #12
Re: End of file
Where did you get the code? Why are you using it instead of using one of the Java SE classes?Is that OK?
What is the problem? Putting a - in front of a number is the way to indicate that the number is negative.how to skip the - character for the negative numbers
- 09-29-2011, 01:23 AM #13
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
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
- 09-29-2011, 01:37 AM #14
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
the mp3 file is not working..java.io.IOException: File Not Found......javax.media.NoP
By shruts in forum Java AppletsReplies: 1Last Post: 07-28-2011, 07:50 PM -
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 05:31 AM -
pack200 .gz file unpack and create .jar file in browser(speed test).
By maheshsardar in forum Java AppletsReplies: 1Last Post: 08-04-2010, 03:24 PM -
Log4J : logs of a specific category to a file, but only errors to the file AND stdout
By msegmx in forum Advanced JavaReplies: 0Last Post: 07-15-2010, 01:23 PM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks