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));
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
posted
0
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
posted
0
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