-
October 11th, 2004, 04:51 PM
#1
Senior Member
readLine(), skipping a line
Hello guys!!!
The following the code for reading, and printing the lines if the file "fil1.txt".
---------------------------------------------------------
import java.io.*;
class ReadOne
{
public static void main(String[] args)throws IOException
{
FileReader f1 = new FileReader("c:\\test\\fil1.txt");
BufferedReader file = new BufferedReader(f1);
String text;
int i=0;
while(file.readLine()!= null)
{
text=file.readLine();
i++;
System.out.println(text);
}
System.out.println("\n The value of I is: "+i);
file.close();
}
}
---------------------------------------
The problem is that the file "fil1.txt" has 12 lines and this code is only showing 6 lines. i.e. it is skipping every alternate line.
the contents of the file are the followiing, so that u can try and guide me, about the problem.
----------------------------------------------------------------------
1 we we we f sd s d s d
2 da f sd fs df dsf ds f dsf ds f sf
3 dfsdf fs f d f dsf s df sd fs
4 fdgrsre ff ef fe f ffef a fdsf
5 fr erew325 4aw3234 wer 43r 2
6 rwr43rwerr 23r 3r 2r2 r d s et uyj yu
7 tyryttru u76iyuy jyju yjty
8 errt45ft45tw4e g54t 45g 45y 5y rewyt 65y wy y
9 243t45645 t yhgre y 65 y 4t 45yt 4w t4
10 35 wr43 t 4 4t 45 4 gt45 t
11 34tr3 r4 t45 t45 t 54y 45
12 er 43f3 g4 4 g
-----------------------------------------------------------------
Any sort of help will be appreciated.
U get What U pay for.
-
October 11th, 2004, 05:23 PM
#2
I know nothing about java, but possible the line
System.out.println("\n The value of I is: "+i);
and the +i is causing the counter to add again? Im really not sure thats just a guess and I would assume that it would be i++ to add again.
i2c
-
October 11th, 2004, 05:34 PM
#3
import java.io.*;
public class projectMain {
public static void main(String[] args) throws IOException {
String line;
FileReader f1 = new FileReader(new File("C:\\Documents and Settings\\Owner\\.netbeans\\3.6\\sampledir\\projects\\inputLimiter\\fil1.txt"));
BufferedReader read = new BufferedReader(f1);
while((line = read.readLine()) != null) {
System.out.println(line);
}
read.close();
}
}
You need to change the path, your loop was a bit screwed up so its fixed. =)
-
October 11th, 2004, 06:34 PM
#4
Senior Member
U get What U pay for.
-
October 11th, 2004, 07:20 PM
#5
If you didn't understand what ;TT did, basically you were reading from the file twice before you assigned it to your string. everytime you do a readLine(), it reads a line and goes to the next waiting for you to read again. so when you did...
while(file.readLine()!= null)
{
text=file.readLine();
you were reading a line and checking to see it wasn't null, then you were reading another line. That's bad cause you skip a line, and you could still get null because you're reading another line after you check for null, which isn't checked.
-
October 11th, 2004, 09:00 PM
#6
Member
Another option, standard academic style while (test boolean) structure with boolean that is set when your conditions are met.
boolean loop = true;
while (loop) {
String line = file.readLine();
System.out.println(line);
loop = (line == null);
}
or the same thing, just in a manner that doesn't hit the conditional on the loop when you explicit condition is met, you kick out of the loop and go about your business.
while (true) {
String line = file.readLine();
System.out.println(line);
if (line == null) {
break;
}
}
The one above is a little cleaner, because it uses fewer variables and doesn't have the break.
--BigDick
\"When in Rome, eat Rome!\" -Godzilla
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|