I'm trying to read a txt file into string
and then cut the string into peaces,but it's
not quite working.
if the file looks like this:
this
is
atest
folks
String should be something like this: this is atestfolks
and after parsing
this
is a
test
folk
s
now it's putting all the lines together and i can't get lengt of the
firs word and parsing doesn't work right.
import java.io.*;
import java.util.*;
import java.lang.*;
class New{
public static void main(String []args)throws IOException {
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
File inputFile = new File ("help.txt"
FileReader fis =new FileReader(inputFile);
BufferedReader bis = new BufferedReader(fis);
for(String line=bis.readLine();line != null;line=bis.readLine()) {
sb.append(rivi);
line=line.trim();
}
String test=sb.toString();
Here i'll try to cut the string into equal length peaces
length is the length of the firs word in file. if i have to combine two words then add one space.
String tmp= "";
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
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+=""+' ';
}//if
System.out.println(tmp);
}
}
and then cut the string into peaces,but it's
not quite working.
if the file looks like this:
this
is
atest
folks
String should be something like this: this is atestfolks
and after parsing
this
is a
test
folk
s
now it's putting all the lines together and i can't get lengt of the
firs word and parsing doesn't work right.
import java.io.*;
import java.util.*;
import java.lang.*;
class New{
public static void main(String []args)throws IOException {
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
File inputFile = new File ("help.txt"
FileReader fis =new FileReader(inputFile);
BufferedReader bis = new BufferedReader(fis);
for(String line=bis.readLine();line != null;line=bis.readLine()) {
sb.append(rivi);
line=line.trim();
}
String test=sb.toString();
Here i'll try to cut the string into equal length peaces
length is the length of the firs word in file. if i have to combine two words then add one space.
String tmp= "";
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
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+=""+' ';
}//if
System.out.println(tmp);
}
}