Results 1 to 3 of 3
  1. #1
    Bmack is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Question Casting an int from a sorted set

    Hi,
    trying to write a method that will return an int from a sorted set. I get a messasge "Semantic error: java.lang.String cannot be cast to java.lang.Integer". Is there any way in which I can return an int?


    [code]
    public int getLowestValueInSet()
    {
    TreeSet tSet = new TreeSet();
    tSet.add("7")
    tSet.add("3");
    tSet.add("4");
    // System.out.println("Lowest value in Java TreeSet is : " + tSet.first());
    int lowestValue=(Integer)tSet.first();
    return lowestValue;
    }
    [code]

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Maybe this:

    TreeSet<Integer> tSet = new TreeSet<Integer>();
    tSet.add(new Integer(7));
    tSet.add(new Integer(4));
    tSet.add(new Integer(3));

  3. #3
    Bmack is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Thumbs up

    Thanks, that worked a treat. I applied your solution to the method that created the set I was using and it all worked. Thanks for taking the time to reply.

    F.Y.I.

    Java Code:
    public SortedSet makeCalendar(int [] suppliedDays)
    {
       SortedSet<Integer> calendar = new TreeSet<Integer>();
       for (Integer aDay : suppliedDays)
       { 
          calendar.add(new Integer(aDay));
       }
          return calendar;
       }
    }

Similar Threads

  1. Sorted LinkList problem
    By koolaqua16 in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2009, 07:49 AM
  2. Replies: 8
    Last Post: 04-22-2008, 04:21 AM
  3. How to create a Sorted List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:31 PM
  4. Replies: 0
    Last Post: 04-14-2008, 09:39 PM
  5. My doublyLinked list does not get sorted
    By hasani6leap in forum New To Java
    Replies: 0
    Last Post: 01-06-2008, 04:09 PM

Tags for this Thread

Posting Permissions

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