I'm trying to parse a text file into equal length peaces..and put one space to separate words. if the next 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 How can i read every line from my text file ("eka.txt") now it only reads the first line and prints it out like this hello kathy here is the code! import java.io.*; import java.util.*; class Testi{ public static void main(String []args)throws IOException { BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
import java.util.*; import java.io.*; public class Test { private int SEPERATOR = 5;
public Test() { try { this.makeTrim(); } catch(Exception Exp) { System.out.println("Excpeiton raised" + Exp); } } public static void main(String[] args) { new Test(); } public void makeTrim() throws Exception { FileReader fr = new FileReader("c:\\tmp.txt"); BufferedReader br = new BufferedReader(fr); String next_line = null; String curr_line = br.readLine(); while (curr_line != null) { int start_index = 0; int last_index = SEPERATOR; int length = curr_line.length();
// if line length >= 5 if (length >= SEPERATOR) { int repeat = length/SEPERATOR; for (int i = 0; i < repeat; i++) { // cut 5 chars String line = curr_line.substring(start_index, last_index); System.out.println(line); start_index = last_index; last_index = start_index + SEPERATOR; } // store remaining chars in next_line next_line = curr_line.substring(start_index, length); } else { // store chars in next_line next_line = curr_line; }
String temp = null; if ((temp = br.readLine()) == null) { break; } curr_line = next_line + temp; } // while } public void log(String msg) { System.out.println(msg); } }
kristian jarvi
Greenhorn
Joined: Jan 19, 2002
Posts: 9
posted
0
Thank's Napa I'll try that!
Chantal Ackermann
Ranch Hand
Joined: Sep 28, 2000
Posts: 508
posted
0
hello Kristian, it might be quite late for an answer, but I think you've just mixed up your variables "String test" and "String nextLine". "test" holds your first line while "nextLine" is replaced with each following line.