Results 1 to 3 of 3
  1. #1
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default 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 11:00 PM. Reason: code tag added

  2. #2
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default Re: lotto program using sets

    I have figured it out by using removeAll instead

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

    Default 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?

    Java Code:
    if (x.getticketNumbers().contains(d)) {
        System.out.println("you have at least one number");
    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.)

    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

  1. Maximizing efficiency with sets and lists
    By salmontres in forum New To Java
    Replies: 2
    Last Post: 12-18-2011, 04:51 AM
  2. Maps and Sets
    By darkblue24 in forum New To Java
    Replies: 19
    Last Post: 03-25-2010, 07:13 PM
  3. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 10:36 AM
  4. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 05:07 PM
  5. No duplicates allowed in Sets
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:33 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
  •