permaculture playing cards*
The moose likes Beginning Java and the fly likes I Need help with file handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Need help with file handling" Watch "I Need help with file handling" New topic
Author

I Need help with file handling

kristian jarvi
Greenhorn

Joined: Jan 19, 2002
Posts: 9
How can i read every line from my text file ("eka.txt")
now it only reads the first line and prints it out.does while loop need some improving?
How can I handle NullPointerException that it gives. I've tried
try/catch but don't know so well how to use it
import java.io.*;
import java.util.*;
class Testi{
public static void main(String []args)throws IOException {
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

File inputFile = new File ("eka.txt");
FileReader fis =new FileReader(inputFile);
BufferedReader bis = new BufferedReader(fis);
String test=bis.readLine();
String tmp= "";
while((bis.readLine().trim() != null)) {
int spacefound=0;
int l=test.indexOf(" ");
for(int i=0;i<test.length();i++){
char c=test.charAt(i);
if(c!=' ') tmp+=""+c;
if(c==' ' && (spacefound<1) && !(tmp.equals(""))){
tmp+=""+c;
spacefound++;
}
if(tmp.length()==l) {
System.out.println(tmp);
tmp="";
spacefound=0;
}
}
if(tmp.length()<l){
for(int i=0;i<=(l-tmp.length());i++)
tmp+=""+' ';
}
}
System.out.println(tmp);
}
}
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
Hi Kristian,
There are a few problems here but you're almost there.
The first thing is - you can catch a null pointer exception but really you shouldn't be trying to catch it - you should be trying to prevent it from arising. The problem in this case is the while clause. It reads every line until it reaches the end of the file when readLine returns null - but then you try to trim that null. If you want to trim the string you should do it within the while clause.
Which leads to the second problem. You read each line of the file but the only line you process is the first line, which is stored in the String variable test. You want another String variable which holds the values of the other lines as you read them. So part of your revised code might look something like this:-

I'm not quite sure what you want to do with the lines as you display them. Do you really need to read the first line before you enter the loop? Are you trying to strip the spaces from each line or are you trying to display the first word of each line?
There is a very good Sun Java tutorial on IO with lots of helpful examples if you're still stuck.
Hope this helps!
Kathy
[ January 23, 2002: Message edited by: Kathy Rogers ]
kristian jarvi
Greenhorn

Joined: Jan 19, 2002
Posts: 9
Originally posted by Kathy Rogers:
Hi Kristian,
There are a few problems here but you're almost there.
The first thing is - you can catch a null pointer exception but really you shouldn't be trying to catch it - you should be trying to prevent it from arising. The problem in this case is the while clause. It reads every line until it reaches the end of the file when readLine returns null - but then you try to trim that null. If you want to trim the string you should do it within the while clause.
Which leads to the second problem. You read each line of the file but the only line you process is the first line, which is stored in the String variable test. You want another String variable which holds the values of the other lines as you read them. So part of your revised code might look something like this:-

I'm not quite sure what you want to do with the lines as you display them. Do you really need to read the first line before you enter the loop? Are you trying to strip the spaces from each line or are you trying to display the first word of each line?
There is a very good Sun Java tutorial on IO with lots of helpful examples if you're still stuck.
Hope this helps!
Kathy
[ January 23, 2002: Message edited by: Kathy Rogers ]
kristian jarvi
Greenhorn

Joined: Jan 19, 2002
Posts: 9
Originally posted by Kathy Rogers:
Thank's Kathy I'll try that.
I'm trying to parse text file into equal length
peaces..and put one space to separate words if this text is in the file "eka.txt"
hello
kathy
howare
you doing
the outcome should be
hello
kathy
how a
re yo
u doi
ng
 
GeeCON Prague 2014
 
subject: I Need help with file handling