Results 1 to 12 of 12
  1. #1
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default Help removing pronouns from text

    hi,

    im trying to remove the pronouns for a string. i have a list of pronouns in a text file. i get those into a tring array and then check my string against the elements in the array..here is my code.

    Java Code:
    String a;
    BufferedReader pronounlist = new BufferedReader(new FileReader("E:/pronounslist.txt"));
      a = new String[100];
      String pronoun = pronounlist.readLine();
      int k = 0;
     while (pronoun != null)
                            {
                            a[k] = pronoun;
                            pronoun = pronounlist.readLine();
                                 System.out.println(a[k]);
                            k++;
                            }
    
                            for(int iFilt = 0; iFilt<a.length; iFilt++) {
                                int nWordLength = a[iFilt].length();
                                int nFoundPos = new1.indexOf(a[iFilt]);
                                while (nFoundPos != -1){
                                        
                                        new1 = new1.substring(0,nFoundPos)+""+new1.substring(nFoundPos+nWordLength);
                                        nFoundPos = new1.indexOf(a[iFilt]);
                                }
                               }
    im getting a null pointer exception in the line,

    int nWordLength = a[iFilt].length();

    can somebody help me with this?

    jessie
    Last edited by Eranga; 11-12-2010 at 10:51 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    When you post code, put [code] at the start and [/code] at the end of the code so that it appears nicely formatted:

    Java Code:
    String a;
    BufferedReader pronounlist = new BufferedReader(new FileReader("E:/pronounslist.txt"));
    a = new String[100];
    String pronoun = pronounlist.readLine();
    int k = 0;
    while (pronoun != null)
    {
        a[k] = pronoun;
        pronoun = pronounlist.readLine();
        System.out.println(a[k]);
        k++;
    }
    
    for(int iFilt = 0; iFilt<a.length; iFilt++) {
        int nWordLength = a[iFilt].length();
        int nFoundPos = new1.indexOf(a[iFilt]);
        while (nFoundPos != -1){
    
            new1 = new1.substring(0,nFoundPos)+""+new1.substring(nFou ndPos+nWordLength);
            nFoundPos = new1.indexOf(a[iFilt]);
        }
    }
    (I have done this so I can read it ;)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    im getting a null pointer exception in the line,

    int nWordLength = a[iFilt].length();

    "NullPointerException" means that a value was null when it should not have been. In this case the two candidates are the array a and the array element a[iFilt]. Neither of these are allowed to be null because you can't access an element if the array is null, and you can't call a method if something is null.

    Since your code says quite straightforwardly

    Java Code:
    a = new String[100];

    we can rule out a being null. That leaves a[iFilt] being null. And you have to wonder why that would happen.

    a is initialised as above and, like all Java arrays, it is never empty! Instead it has one hundred locations and each one of them has a null value. a.length is, and will always be, 100. So when you work along the for loop from index 0 to index 100 you will, almost certainly, hit one of these null values in the array. (the front part of the array was filled with nonnull pronouns, but you made the array "big enough" so there are bound to be nulls at the end of it).

    Anyway you get to one of these nulls and then a[iFilt] has the value null and a[iFilt].length() causes the NPE because you can't call a method on a null thing.

    ------------------------

    Instead of the "argument" to show a[iFilt] is null (rather than a), you can use System.out.println().

    Java Code:
    or(int iFilt = 0; iFilt<a.length; iFilt++) {
        System.out.println("a=" + a);
        System.out.println("a[iFilt]=" + a[iFilt]);
        int nWordLength = a[iFilt].length();
        // ...

    Such code will tell you exactly what it is that is null.
    Last edited by pbrockway2; 11-11-2010 at 06:40 PM.

  4. #4
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default

    hi,

    thnks a lot. now i dont get the null pointer exception after i set the array size to the exact size.. but now, this code does not find the pronouns in the list and remove that from the string. why is that? i cant find anything wrong here. i tried the following code as well. but still i can see the pronouns in the string..

    Java Code:
    for(int iFilt = 0; iFilt<a.length; iFilt++) {
                              
                                while ( new1.indexOf(a[iFilt]) != -1){
                                      
                                        new1 = new1.replace(a[iFilt], "");
    
                                       
                                }
                               }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you check what you've in the array a. You keep all the stuff in an array and removing them. If it's not found in the array then you cannot see any changes to the original.

  6. #6
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default

    the array contains the pronouns... i tested it..but still its not checking it in my string....

  7. #7
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default

    Im iterating the list of words like this and then searching for pronouns in the list using my array this time...still it does not work....

    Java Code:
      for(Iterator f = words1.iterator();f.hasNext();){
                                String anew = f.next().toString();
                                //System.out.println(anew);
                                for(int iFilt = 0; iFilt<a.length; iFilt++) {
                               if(anew.indexOf(a[iFilt]) != -1){
                                      
                                        anew = anew.replaceAll(a[iFilt], "");
                               }
                                                                                    
                               }
                                 System.out.println(anew);
                        }

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Again you can use System.out.println() to see what you are checking for:

    Java Code:
    for(Iterator f = words1.iterator();f.hasNext();){
        String anew = f.next().toString();
        System.out.println("looking at " + anew);
        for(int iFilt = 0; iFilt<a.length; iFilt++) {
            System.out.println("checking for " + a[iFilt]);
            if(anew.indexOf(a[iFilt]) != -1){
                System.out.println("found!")
                anew = anew.replaceAll(a[iFilt], "");
            }
        }
        System.out.println(anew);
    }

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    Again you can use System.out.println() to see what you are checking for:
    @OP, are you clear with this?

  10. #10
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default

    i think there is a mistake in my searching code.. because the list words1 contains a list of words like this for example.

    it
    Belgium
    's IMEC
    a threeyear collaboration agreement
    all
    advanced metallization process

    i want to remove pronouns like, it, all and so on from this list.. but my search code replaces all in the metallization word itself. how can i remove only the pronouns that are in the list as one word/element...

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It's hard to know exactly what you're doing here.

    Originally you had one string and were searching for pronouns within it. In that case you could use replaceAll() but will have to use regular expressions that corresponds to each of these words. (A word is not just a sequence of letters: it is a sequence within a certain context that marks it out as a word.)

    The problem you pose now is quite different.

    how can i remove only the pronouns that are in the list as one word/element

    In that case you would take the list item and work through all the pronouns. If the item is equal to any of the pronouns, remove it from the list.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Removing Items In a GUI
    By 67726e in forum New To Java
    Replies: 3
    Last Post: 08-01-2010, 05:46 PM
  2. Removing Duplicates.
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 01:03 PM
  3. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 11:46 AM
  4. image removing
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-20-2008, 08:27 PM
  5. Replies: 1
    Last Post: 01-11-2008, 10:06 PM

Posting Permissions

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