• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading every line from a file to string

 
kristian jarvi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));

File inputFile = new File ("eka.txt");
FileReader fis =new FileReader(inputFile);
BufferedReader bis = new BufferedReader(fis);
String test=bis.readLine();
String tmp= "";
String nextLine;
while((nextLine=bis.readLine()!= null)) {
nextLine=nextLine.trim();
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);
}
}
 
Napa Sreedhar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
space is treated as a character.

Try this program

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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank's Napa I'll try that!
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!