Urgent Help needed! Match n retrieving
Hi, would appreciate of any sort of help is given in any possible way.
I need it quite urgently!!
I am suppose to read a text file and compare the text on the text file. If it is the same, i am suppose to retrieve the text beside it separated by spaces.
Example:
Apple fruitA
Orange AD1204
Pear Green
if it match "Apple", it would retrieve the word "fruitA".
if it match "orange", it would retrieve the word "AD1204"
My codes as shown below:
import java.io.*;
FileReader fr = new FileReader("pathToFile.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
if ("something".indexof("Apple") > -1) {
retrieve the word beside it.
}
}
Can any kind soul help me out. What should i use to replace the "something" and
how do i retrieve the word beside it.!!
thanks a lot!
[985 byte] By [
lance103] at [2007-11-11 7:36:15]

# 1 Re: Urgent Help needed! Match n retrieving
I'm not going to give you an answer, but here's a snippet to help you:
// just combined your two lines into one
BufferedReader br = new BufferedReader(new FileReader("pathToFile.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(" ");
System.out.println(split[0] + " " + split[1]);
}
destin at 2007-11-11 22:37:48 >

# 2 Re: Urgent Help needed! Match n retrieving
Hi, thanks for your prompt reply.
I understand your code is to spilt by the whitespaces . I joined them back into array but how do i match and retrieve?
shouldn't i use indexof() something to match?
Buti really have no idea how to retrieve? please help.
My codes are shown below:
import java.io.*;
public class Test {
public static void main(String[]args)throws IOException {
BufferedReader br = new BufferedReader(new FileReader("pathToFile.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(" ");
System.out.println(split[0] + split[1]);
}
}
}
# 3 Re: Urgent Help needed! Match n retrieving
Look at the Scanner class.
You can take each line of your file, tokenize it, read the first word, if it matches your criteria then take the next word, otherwise, drop those tokens, read the next line, tokenize it, read the first word, etc. until you've processed your file.
nspils at 2007-11-11 22:39:57 >

# 4 Re: Urgent Help needed! Match n retrieving
Sorry about the lousy response before, i was in a rush to go somewhere. Here, i think this is what you're looking for.
import java.io.*;
public class Test2 {
public static void main(String args[]) {
/** this will be the String we are parsing for */
String firstPart = "Pear";
/** this will hold the rest of the line */
String secondPart = "";
try {
BufferedReader out = new BufferedReader(new FileReader("pathToFile.txt"));
/** stores the line that we're reading */
String line;
/** loop through the file, setting line to the current */
while ((line = out.readLine()) != null) {
/** this line contains firstPart? */
if (line.indexOf(firstPart) != -1) {
/**
* line.indexOf(firstPart) + firstPart.length() will
* get us past the first word, then we trim the string
* get rid of any excess whitespace (before or after)
*/
secondPart = line.substring(line.indexOf(firstPart) + firstPart.length()).trim();
/** we're done looking, so break out of the loop */
break;
}
}
} catch (IOException e) {
/** print the error if we find one */
System.err.println("Error: " + e);
}
/** print out the second part of the line */
System.out.println(secondPart);
}
}
destin at 2007-11-11 22:40:51 >

# 5 Re: Urgent Help needed! Match n retrieving
Hi destin, ya this is what i am looking for. thanks a million.
How do i modify such that i can get a number of the same record?
ENTRY blahbah
Gender M
///
ENTRY aaaaaaaa
Gender F
///
I need get all the values after the ENTRY.
this case would be
blahblah
aaaaaaaaa
Note they are separated by ///
I am thinking of a loop delimited by ///
but had no idea where to intgegrate in your codes.
Please help me out.
thanks.
Would appreciate quick reply.
# 6 Re: Urgent Help needed! Match n retrieving
With Scanner you can use "///" as your delimiter to tokenize your input file, and then you could create a scanner object of each of the "file's" tokens using whitespace as your delimiter to create this object's tokens ...
nspils at 2007-11-11 22:43:00 >

# 7 Re: Urgent Help needed! Match n retrieving
import java.io.*;
import java.util.*;
public class Test2 {
public static void main(String args[]) {
/** this will be the String we are parsing for */
String firstPart = "Orange";
/** this will hold the rest of the line */
ArrayList<String> secondPart = new ArrayList<String>();
try {
BufferedReader out = new BufferedReader(new FileReader("pathToFile.rtf"));
/** stores the line that we're reading */
String line;
while ((line = out.readLine()) != null) {
/** this line contains firstPart? */
if (line.indexOf(firstPart) != -1) {
/**
* line.indexOf(firstPart) + firstPart.length() will
* get us past the first word, then we trim the string
* get rid of any excess whitespace (before or after)
*/
secondPart.add(line.substring(line.indexOf(firstPart) + firstPart.length()).trim());
}
}
} catch (IOException e) {
/** print the error if we find one */
System.err.println("Error: " + e);
}
/** print out the second part of the line */
for (int i = 0; i < secondPart.size(); i++)
System.out.println(secondPart.get(i));
}
}
I don't have time now to explain how to do the "///" because i'm going on vacation tomorrow morning and need sleep. The code above this will show all secondParts with for the given String (firstPart). It will not do the "///" thing.
destin at 2007-11-11 22:43:53 >

# 8 Re: Urgent Help needed! Match n retrieving
Ithink you can use Properties clas.