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

[2685 byte] By [SandraPandraa] at [2007-11-27 5:48:02]
# 1

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.

Hippolytea at 2007-7-12 15:33:05 > top of Java-index,Java Essentials,Java Programming...
# 2

hi! sorry it took some time to answer. yes what i would like to do is finding the words as you described:

Alpha Bravo Charlie

Bravo Charlie Delta

Charlie Delta Echo

Delta Echo Foxtrot

Echo Foxtrot Golf

Foxtrot Golf Hotel

Golf Hotel India

the reason for finding read() appealing was that, first of all it seemed so easy to just change readLine() into read (hasn't turned out to be as easy as i thought though) and as i understood it read() enables me to continue even when there is a page break or a line break. maybe Scanner does this too, i need to read more about it.

SandraPandraa at 2007-7-12 15:33:05 > top of Java-index,Java Essentials,Java Programming...
# 3

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());

}

}

Hippolytea at 2007-7-12 15:33:05 > top of Java-index,Java Essentials,Java Programming...
# 4
i'll look more into it, it's just that changing readLine() into just read() and BufferedReader to FileReader seems to be similar to what i've already done except it wouldn't break at each new line. but if scanner does this, it's definitely worth learning. thanks.
SandraPandraa at 2007-7-12 15:33:05 > top of Java-index,Java Essentials,Java Programming...
# 5
BufferedReader has the same read() methods that FileReader has: there's no need to 'change the BufferedReader to a FileReader'.
ejpa at 2007-7-12 15:33:05 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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.

uncle_alicea at 2007-7-12 15:33:05 > top of Java-index,Java Essentials,Java Programming...