more fun questions about FileReader
at risk of being a little repetitive but i would really like to solve this, if it's realistic to do so: i have a code that can open a file, find words 1-3, 2-4, 3-5 etc but it starts from the beginning when it hits a line break. i would like to change "BufferedReader" (within the "try"-statement) to "FileReader" and "readLine()" to "read()" but simply changing them doesn't work for me, even if it seems easy to do. Does anyone know how to do this?
class Testar2{
publicstaticvoid main(String[] args){
String word1;
String word2;
String word3;
String partDesc;
try{FileInputStream fstream =new FileInputStream("Test.txt");
DataInputStream in =new DataInputStream(fstream);
BufferedReader br =new BufferedReader(new InputStreamReader(in));//here's the first one
while ((partDesc = br.readLine()) !=null){//here's the second
do{
Pattern pattern = Pattern.compile("^(\\w+)\\s+(\\w+)\\s+(\\w+)");
Matcher matcher = pattern.matcher(partDesc);
//Find the first word of the part desc
while (matcher.find()){
word1 = matcher.group(1);
word2 = matcher.group(2);
word3 = matcher.group(3);
System.out.println (word1 +" " + word2 +" " + word3);}
partDesc = partDesc.replaceFirst("^(\\w+)\\s+","");}
while ( partDesc.matches("^(\\w+)\\s+.*") ==true);
in.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
thanx in advance
What happened to using Scanner?
Also, let me see if I understand your requirement.
Say you have a file with the word sequence:
Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India
You want to generate the triplets:
Alpha Bravo Charlie
Bravo Charlie Delta
Charlie Delta Echo
Delta Echo Foxtrot
Echo Foxtrot Golf
Foxtrot Golf Hotel
Golf Hotel India
If so, this stuff with replaceFirst is overkill.
-Read input one word at a time
-maintain the last words read (maximum of three)
-when a word is added, and there are already three, the oldest is dropped.
-there are lots of ways of doing this, but none are that difficult.
Scanner rocks for this. By default, the delimiter is whitespace:
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India");
while (scanner.hasNext())
System.out.format("[%s]%n", scanner.next());
}
}
> maybe Scanner does this too, i need to
> read more about it.
Actually, Scanner does a lot more than that. Its findWithinHorizon() methods let you use it as a super-Matcher that can treat a file or a stream as if it were a string, without having to read the whole thing in at once. Combine that fact with a certain lookahead trick that hiwa posted here a while back, and the solution is simple: import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Test
{
public static void main(String... args) throws Exception
{
Scanner sc = new Scanner(new File("test.txt"));
Pattern p = Pattern.compile("\\b(\\w+)\\s+(?=(\\w+)\\s+(\\w+)\\b)");
while (sc.findWithinHorizon(p, 0) != null)
{
MatchResult m = sc.match();
System.out.printf("%s %s %s%n", m.group(1), m.group(2), m.group(3));
}
}
}
Given a "text.txt" file like this: Alpha
Bravo Charlie Delta
Echo Foxtrot Golf Hotel
India
You should see exactly the result you posted in reply #2.