Results 21 to 40 of 50
Thread: dictionary
- 12-31-2010, 07:24 AM #21
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 12-31-2010, 07:34 AM #22
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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--
- 12-31-2010, 09:07 AM #23
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
ok
i read them but i still dont know how to use them?
- 12-31-2010, 10:59 AM #24
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 12-31-2010, 11:19 AM #25
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
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
- 12-31-2010, 11:27 AM #26
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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:
You should be able to handle it from here.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);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 11:59 AM #27
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
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
- 12-31-2010, 12:19 PM #28
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 12-31-2010, 01:34 PM #29
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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); } }
- 12-31-2010, 02:14 PM #30
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
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
i am aiming to getJava 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 } }
but what im getting is[bag, sun, day, moon, Sunday, Monday, airbag, Moonbag]
what could be the problem with my code?[sun, day, moon, Sunday, Monday, airbag, Moonbag, null]Last edited by aizen92; 12-31-2010 at 02:18 PM.
- 12-31-2010, 02:20 PM #31
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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?
you should show what you files look like.But the thing is, i got my code work half of it, but im having a problem here, here is my code
- 12-31-2010, 02:32 PM #32
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
yeh, and i got this working by this code in which i assumed i have an array and did the checking
but now im trying to get the input from the file and putting them in an array to do the above codeJava 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); } }
my input file is the following
bag
sun
day
moon
Sunday
Monday
airbag
Moonbag
----
straw
black
blue
berry
raspberry
strawberry
blueberry
blackberry
cranberry
Halleberry
+++
- 12-31-2010, 02:50 PM #33
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 03:02 PM #34
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
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
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 oneJava 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(); } }Last edited by aizen92; 12-31-2010 at 03:04 PM.
- 12-31-2010, 03:28 PM #35
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 03:36 PM #36
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
- 12-31-2010, 03:40 PM #37
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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
- 12-31-2010, 05:34 PM #38
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
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
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 workingJava 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); } }
- 12-31-2010, 06:45 PM #39
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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:
... and see if you've read them correctly. This is elementary debugging and you should learn to do that.Java Code:System.out.println(Arrays.toString(yourArray));
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 06:59 PM #40
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
Similar Threads
-
Need plugin for creating dictionary project in eclipse
By cbklp in forum EclipseReplies: 2Last Post: 12-27-2010, 04:28 AM -
Phrases in Lucene dictionary?
By TheShar in forum LuceneReplies: 0Last Post: 05-27-2010, 02:42 PM -
add dictionary
By monir6464 in forum New To JavaReplies: 2Last Post: 04-07-2008, 06:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks