Click to See Complete Forum and Search --> : file reading and regular expression


ras_pari
October 1st, 2009, 02:25 AM
hi all..

here is a program.......

import java.util.regex.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;

public class demo{
public static void main(String[] args) throws IOException{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name for search: ");
String filename = in.readLine();
File file = new File(filename);
if(!filename.endsWith(".txt")){
System.out.println("This is not a text file.");
System.out.println("Please enter file name withe \".txt\" extension.");
System.exit(0);
}
else if(!file.exists()){
System.out.println("File not found.");
System.exit(0);
}

FileChannel fileChannel = new FileInputStream(filename).getChannel();
CharBuffer charBuffer = Charset.forName("8859_1").newDecoder().decode(
fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)fileChannel.size()));
Pattern pattern = Pattern.compile("\\s[0-9]?[0-9]\\s");
Matcher matcher = pattern.matcher(charBuffer);
String b="5";
int a = 0;
while(matcher.find()){
a++;
String matchStrings = matcher.group();
String []splits = matchStrings.split(" ");
for(int i=0; i< splits.length ;i++){

System.out.println(splits[i]);
}


for(int j=0; j< splits.length ;j++){

if (splits[j].trim().equals(b)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}


}




}
System.out.println("Total occurance of the word in the file is: " + a);
}
}

IN this program First I have read a file that I have attach ..i.e "prob.txt" and parse the file according to regular expression and extract 1,2,3...........50 number on prob.txt...and now I want to extract two value given in the bracket (3.84) and [6.64] corresponding to number that we have previosly extract.for example if we have number 1 come then ,extact two value i.e (3.84) and [6.64] corresponding to number 1.other example if we have number 2 come then ,extact two value i.e (5.99) and [9.21] corresponding to number 2.I hope u all understand.plz solve this problem.Thanx in advance..

Xeel
October 5th, 2009, 10:07 AM
1. Do not start several threads about one problem
2. Please use [code] tags when pasting code
3. The answer is here (http://www.codeguru.com/forum/showthread.php?t=485795)