Results 1 to 3 of 3
  1. #1
    chkontog is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default regular expressions in java

    Hello,
    i would like to split words in comma, space and punctuations.I have write the above code


    Java Code:
    import java.io.BufferedReader;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.ArrayList;
    import java.util.StringTokenizer;
         
         
        // main class
        public class Main {
         
            // for saving word objects
            private ArrayList<Word> words;
         
            public static void main(String[] args) throws IOException {
         
                Main main = new Main();
                main.start();//start
         
            }
         
            private void start() throws IOException {
         
                readFile(); // read file
                printWords(); // print results
            }
         
            private void readFile()throws IOException{
         
                BufferedReader input = new BufferedReader(new FileReader("a.txt"));
                words = new ArrayList<Word>();
         
                int lineNum = 1; // we read first line in start
         
                // delimeters of line in this example only "space"
                String delim1, delim2;
                char [] parse1 = {' '};
                char [] parse2 = {','};
                
                delim1 = new String(parse1);
                delim2 = new String(parse2);
                String line = input.readLine();
                String [] lineWords;
         
                // read line while end of file
                while(line != null){
         
                    
                        lineWords = line.split("[^a-zA-Z]+");
                        //lineWords = line.[\\s,;\n+\\d\\W\t\r\\s+]+
                    
         
                    // split the words and create word object
                    for (int i = 0; i < lineWords.length; i++) {
                        
                        if(lineWords[i]!=null){
                        System.out.print("i:"+i);
                        //if(lineWords[i].compareTo(" ")!=0);
                            System.out.println(lineWords[i]);
                        Word w = new Word(lineNum,i+1,lineWords[i]); // lineNum -> line number i+1--> words index in line
                        words.add(w);                       // lineWords[i] is word  
                        }
                    }
                    lineNum++;    // pass the next line
         
                    line = input.readLine();
         
                }
         
            }
         
            // print out the results
            private void printWords() {
         
                System.out.println("WORD\t\tLINE\t\tINDEX");
                System.out.println();
         
                for (int i = 0; i < words.size(); i++) {
                    Word w = words.get(i);
                    if(w.name.compareTo("\\n")!=0)
                    System.out.println(w.name + "\t\t" + w.lineNum + "\t\t" + w.index);
                }
         
            }
         
            // class word for model of every word object
         
            class Word{
                private int lineNum;
                private int index;
                private String name;
         
                public Word(int lineNum, int index, String name) {
                    this.lineNum = lineNum;
                    this.index = index;
                    this.name = name;
                }
         
                public int getLineNum() {
                    return lineNum;
                }
                public int getIndex() {
                    return index;
                }
                public String getName() {
                    return name;
                }    
            }    
        }
    but when in the text i have something like these "322 abb",

    the array linewords[], it returns a space at the first element (i=0) and then "abb".

    Why it returns a space element?

  2. #2
    chkontog is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: regular expressions in java

    upppp

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: regular expressions in java

    Are you trying to include or exclude the 322? numbers are not word characters so they will be ignored. Also I don't see that lines 35 through 40 do anything. You could always just add in a check for empty entries and skip those, just look for a 0 length string!

Similar Threads

  1. Java Regular Expressions: Comma Seperated List
    By sgtblitz in forum New To Java
    Replies: 3
    Last Post: 04-18-2011, 10:17 PM
  2. Regular Expressions Help
    By Death Sickle in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 05:21 AM
  3. regular expressions
    By sozeee in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 10:58 PM
  4. How to create regular expressions in Java
    By maz09 in forum New To Java
    Replies: 12
    Last Post: 04-02-2010, 06:13 PM
  5. Regular Expressions in java
    By blue404 in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2008, 04:43 AM

Posting Permissions

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