Results 1 to 3 of 3
Thread: lotto program using sets
- 10-03-2012, 09:14 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
lotto program using sets
Hi I am writing a java program that will generate 100 lottery tickets store this data,then you can enter the winning lottery numbers and it will allow you to check if you have won the jackpot and also allow you to check to see if any of your numbers have come up.I have run into a problem trying to write a method to check if one or more numbers have won this is the method I have written but I am getting an error message:
Java Code:Exception in thread "main" java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.Comparable at java.util.TreeMap.getEntry(TreeMap.java:325) at java.util.TreeMap.containsKey(TreeMap.java:209) at java.util.TreeSet.contains(TreeSet.java:217) at practical3_lottery.Practical3_Lottery.checkTicket at practical3_lottery.Practical3_Lottery.main
The method I have written is this any help would be greatly appreciated
Java Code:public static void checkTicket(ArrayList<Ticket> list, Set<Integer> d) { int i = 0; for (Ticket x : list) { if (x.getticketNumbers().contains(d)) { System.out.println("you have at least one number"); }Last edited by pbrockway2; 10-03-2012 at 10:00 PM. Reason: code tag added
- 10-03-2012, 10:04 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
Re: lotto program using sets
I have figured it out by using removeAll instead
- 10-03-2012, 10:14 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: lotto program using sets
Please use the code tags when posting code. BB Code List - Java Programming Forum
It would also help to post a bit more code. Specifically people will read that fragment and wonder: so, what type of thing does getticketNumbers() return? and is that if condition the line of code indicated by the error message?
The message suggests you are searching for a number within the treeset of ticket numbers. But what you send to contains() is a hashset of numbers. The ticket contains numbers not sets of numbers. (The runtime error occurs because sets of numbers are not comparable things, and sorted sets compare things in order to find elements.)Java Code:if (x.getticketNumbers().contains(d)) { System.out.println("you have at least one number");
To find if at least one element of d is also an element of x.getticketNumbers() consider using a for loop and checking number in d. There may be other ways, but this seems to be the most straight forward.
Similar Threads
-
Maximizing efficiency with sets and lists
By salmontres in forum New To JavaReplies: 2Last Post: 12-18-2011, 03:51 AM -
Maps and Sets
By darkblue24 in forum New To JavaReplies: 19Last Post: 03-25-2010, 06:13 PM -
Maps and Sets
By RedKMan in forum New To JavaReplies: 3Last Post: 02-16-2010, 09:36 AM -
Duplicates in more than two sets
By JavaJ in forum New To JavaReplies: 8Last Post: 12-03-2009, 04:07 PM -
No duplicates allowed in Sets
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks