Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 50

Thread: dictionary

  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    can explain it more to me, i didint understand it very well ur method
    what are sets? are they like arrays or what?
    The API documentation for the Set interface is your friend.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by aizen92 View Post
    can explain it more to me, i didn't understand it very well ur method
    what are sets? are they like arrays or what?
    Sets are a type of interface in java. When you store elements in a set, the set interface ensures there is no duplicate element in the set. Before proceeding with your implementation, read the Java API as JosaH mentioned:

    Set (Java 2 Platform SE v1.4.2)
    --user0--

  3. #23
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ok
    i read them but i still dont know how to use them?

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    ok
    i read them but i still dont know how to use them?
    Read it again? It isn't rocket science; if you give up after a first read I can tell you that the software development business is not for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Sorry for asking too much, but is there an alternate for sets?
    something with similar functionality but different than sets, i dont think im getting the hang of it

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    Sorry for asking too much, but is there an alternate for sets?
    something with similar functionality but different than sets, i dont think im getting the hang of it
    If there is something with a similar functionality you'd experience the same difficulties and you wouldn't understand it either. Sets are easy actually:

    Java Code:
    Set<String> words= new HashSet<String>(); // create a new set
    
    words.add("foo"); // add a few words to the set
    words.add("bar");
    words.add("baz");
    words.add("foo"); // this won't be added because it's already in the set
    
    for (String s: words) // print the entire set
       System.out.println(s);
    You should be able to handle it from here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #27
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Well i got it to work with the previous skeleton code u gave me the one with the same array but with 3 for loops and checking, and it worked, now im going to work on having a scanner read from a file and inputing them in an array
    and hope it works

    @Jos: i really tried the set but i couldn't because it was kinda hard to understand, sorry but really thnx for heping me alot

    and also thnx for user0 for helping me too with this problem, anyway ill post my final code when it works

  8. #28
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    @Jos: i really tried the set but i couldn't because it was kinda hard to understand, sorry but really thnx for heping me alot

    and also thnx for user0 for helping me too with this problem, anyway ill post my final code when it works
    Good; you show your code and I'll show you the code using Sets; you compare both versions and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #29
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by aizen92 View Post
    Ok, i think ill be testing a little bit before heading to the whole array thingy from input, im gonna assume i already made the arrays and had them arranged, and made 2 arrays sub and word with each one's element in them, and i want to see how to test for if to count for the word or not.
    this is the code ive come up with

    Java Code:
    public class testing
    {
        public static void main (String[] args)
        {
            String [] sub = {"bag", "sun", "day", "moon"};
            String [] word = {"Sunday", "Monday", "airbag", "Moonbag"};
            
            int count = 0;
            
            for ( int i = 0; i < word.length; i++)
            {
                for ( int j = 0; j < sub.length; j++)
                {
                    for ( int k = 0; k < sub.length; k++)
                    {
                        if ( word[i].equals(sub[j] + sub[k]))
                        {
                            count++;
                        }
                    }
                }
            }
            
            System.out.println(count);
        }
    }
    but the problem is my output is "0", so i think there is something wrong with my code
    any help with this
    thanks in advance
    you should compare them case-insensitive
    Java Code:
    public class testing
    {
        public static void main (String[] args)
        {
            String [] sub = {"bag", "sun", "day", "moon"};
            String [] word = {"Sunday", "Monday", "airbag", "Moonbag"};
            int count = 0;
            for ( int i = 0; i < word.length; i++)
            {
                for ( int j = 0; j < sub.length; j++)
                {
                    for ( int k = 0; k < sub.length; k++)
                    {
                        if ( word[i].toLowerCase().equals(  (sub[j] + sub[k]).toLowerCase()  ) )
                        {
                            count++;
                        }
                    }
                }
            }
    
            System.out.println(count);
        }
    }

  10. #30
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    the problem said to ignore case sensitivity, so it doesnt matter if theyre upper or lower case

    But the thing is, i got my code work half of it, but im having a problem here, here is my code

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.util.Arrays;
    
    public class Problem2
    {
        public static void main (String[] args) throws FileNotFoundException
        {
            Scanner input = new Scanner (new File (args[0])); // this scanner to see how many words in each dictionary
            Scanner input2 = new Scanner (new File (args[0])); // this scanner is to read from input and use them in arrays
            int count = 0; // a count for words that can be divided into 2 sub-words
            int wordC = 0; // a count for how many words in the first dictionary
            int wordC2 = 0; // a count for how many words in the second dictionary
    
            while ( input.hasNextLine()) // to count for the first dictionary
            {
                if ( input.next().contains("-"))
                {
                    break;
                }else{
                    input.nextLine();
                    wordC++;
                }
            }
            
            while ( input.hasNextLine()) // to count for the second dictionary
            {
                if ( input.next().contains("+")) 
                {
                    break;
                }else{
                    input.nextLine();
                    wordC2++;
                }
            }
            
            String [] words = new String [wordC]; // an array that contains all the words in a dictionary that is used for comparison later
            
            while ( input2.hasNextLine() && !input2.next().contains("-")) // i believe the error is here, because im trying to put the words of the first dictionary in the array, and when it reaches something the contains - to stop and continue to the other statements ( that will later be written )
            {
                 for ( int i = 0; i < words.length - 1; i++)
                 {
                    words [i] = input2.next();
                    input2.nextLine();
                 }
            }
            
            System.out.println(Arrays.toString(words)); // to print the array to check if my work is right or wrong
        }
    }
    i am aiming to get

    [bag, sun, day, moon, Sunday, Monday, airbag, Moonbag]
    but what im getting is

    [sun, day, moon, Sunday, Monday, airbag, Moonbag, null]
    what could be the problem with my code?
    Last edited by aizen92; 12-31-2010 at 02:18 PM.

  11. #31
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by aizen92 View Post
    the problem said to ignore case sensitivity, so it doesnt matter if theyre upper or lower case
    Correct, it doesn't matter, that's why when you do equals() , you must convert them to either uppercase or lowercase for comparison. If you don't, then "Bag" will not be equal to "bag", right?

    But the thing is, i got my code work half of it, but im having a problem here, here is my code
    you should show what you files look like.

  12. #32
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Quote Originally Posted by JavaHater View Post
    Correct, it doesn't matter, that's why when you do equals() , you must convert them to either uppercase or lowercase for comparison. If you don't, then "Bag" will not be equal to "bag", right?
    yeh, and i got this working by this code in which i assumed i have an array and did the checking

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class testing
    {
        public static void main (String[] args)
        {
            String [] a = {"straw", "black", "blue", "berry", "raspberry", "strawberry", "blueberry", "blackberry", "cranberry", "Halleberry"};
            
            int count = 0;
            
            for ( int i = 0; i < a.length; i++)
            {
                for ( int j = 0; j < a.length; j++)
                {
                    for ( int k = 0; k < a.length; k++)
                    {
                        if ( a[i].toLowerCase().equals(a[j].toLowerCase() + a[k].toLowerCase()))
                        {
                            count++;
                        }
                    }
                }
            }
            
            System.out.println(count);
        }
    }
    but now im trying to get the input from the file and putting them in an array to do the above code

    my input file is the following

    bag
    sun
    day
    moon
    Sunday
    Monday
    airbag
    Moonbag
    ----
    straw
    black
    blue
    berry
    raspberry
    strawberry
    blueberry
    blackberry
    cranberry
    Halleberry
    +++

  13. #33
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    Java Code:
                        if ( a[i].toLowerCase().equals(a[j].toLowerCase() + a[k].toLowerCase()))
    Don't use the toLowerCase() method, use the equalsIgnoreCase( ... ) method instead (it saves you the creation of a couple of String objects). Your problem is that your file contains all the words, compound words as well as the 'elementary' words. You have to check each word if it is the catenation of two other words in the same file/array. Have a look at my code suggestion again and read my explanation about those Sets again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #34
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ok, i got the equalsIgnoreCase
    now whats left is why my code is only reading from the second words and leaving the one null

    i used this

    Java Code:
    String [] words = new String [wordC + wordC2 + 2];
            
            while ( input2.hasNextLine() && !input2.next().contains("-"))
            {
                 for ( int i = 0; i < wordC + wordC2 + 1; i++)
                 {
                    words [i] = input2.next();
                 }
            }
    where wordC + wordC2 + 2 is the number of all the words in the input inlcuding the ---- and +++, but still all i get is missingn the first one
    Last edited by aizen92; 12-31-2010 at 03:04 PM.

  15. #35
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    where wordC + wordC2 + 2 is the number of all the words in the input inlcuding the ---- and +++, but still all i get is missingn the first one
    What are those '-----' and '+++++' lines doing in that file? I thought you had just a file with words (one word per line)? b.t.w. you're reading the first word and ignore it in the header of your while loop (you're testing it for '-----' and forget about it afterwards.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #36
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    the "----" and "+++" are just there to as endings to each dictionary

    how do u suggest to solve the problem of
    Quote Originally Posted by JosAH View Post
    you're reading the first word and ignore it in the header of your while loop (you're testing it for '-----' and forget about it afterwards

  17. #37
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    you can try Scanner
    Java Code:
                Scanner sc = new Scanner( new File("inputfile") );
                sc.useDelimiter("\\Z");
                String[] data = sc.next().split("\\n"); // data will now contain your strings

  18. #38
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ok, ive got all my code, and my final one, but i have a problem here, all my input is 0 and 0 for both counts, heres my code

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.util.Arrays;
    
    public class Problem2
    {
        public static void main (String[] args) throws FileNotFoundException
        {
            Scanner input = new Scanner (new File (args[0]));
            Scanner input2 = new Scanner (new File (args[0]));
            int count = 0;
            int count2 = 0;
            int wordC = 0;
            int wordC2 = 0;
    
            while ( input.hasNextLine())
            {
                if ( input.next().contains("-"))
                {
                    break;
                }else{
                    input.nextLine();
                    wordC++;
                }
            }
            
            while ( input.hasNextLine())
            {
                if ( input.next().contains("+")) 
                {
                    break;
                }else{
                    input.nextLine();
                    wordC2++;
                }
            }
            
            String [] words = new String [wordC];
            String [] words2 = new String [wordC2];
            
            for ( int i = 0; i < wordC; i++)
            {
                input2.useDelimiter("\n");
                words[i] = input2.next();
            }
            input2.next();
            for ( int i = 0; i < wordC2; i++)
            {
                words2[i] = input2.next();
            }
            
            for ( int i = 0; i < words.length; i++)
            {
                for ( int j = 0; j < words.length; j++)
                {
                    for ( int k = 0; k < words.length; k++)
                    {
                        if ( words[i].equalsIgnoreCase(words[j] + words[k]))
                        {
                            count++;
                        }
                    }
                }
            }
            
            for ( int i = 0; i < words2.length; i++)
            {
                for ( int j = 0; j < words2.length; j++)
                {
                    for ( int k = 0; k < words2.length; k++)
                    {
                        if ( words2[i].equalsIgnoreCase(words2[j] + words2[k]))
                        {
                            count2++;
                        }
                    }
                }
            }
            
            System.out.println(count);
            System.out.println(count2);
        }
    }
    i tried the checking method ( last 2 for loops) using an array created by me and entered the elements manually by me, and it worked, and gave me the correct numbers, but in this code it isnt working

  19. #39
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    i tried the checking method ( last 2 for loops) using an array created by me and entered the elements manually by me, and it worked, and gave me the correct numbers, but in this code it isnt working
    So something's different when you specify those two arrays yourself and when you try to read them from a file. Print the content of both arrays:

    Java Code:
    System.out.println(Arrays.toString(yourArray));
    ... and see if you've read them correctly. This is elementary debugging and you should learn to do that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #40
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    I printed the arrays and checked them and they were correct
    I double checked them more than once and they were correct

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 12-27-2010, 04:28 AM
  2. Phrases in Lucene dictionary?
    By TheShar in forum Lucene
    Replies: 0
    Last Post: 05-27-2010, 02:42 PM
  3. add dictionary
    By monir6464 in forum New To Java
    Replies: 2
    Last Post: 04-07-2008, 06:27 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
  •