CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Sep 2009
    Posts
    13

    Question file reading and regular expression

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured