Results 1 to 2 of 2
  1. #1
    Riftara is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default Please Help - TreeSet

    I have the following assignment for my Java class, and, as my professor fully supports reaching out for assistance and advice, I will ask all of you here. This is my second formal class for Java, and I have made a few pointless Android apps, so I'm not a total beginner, but I know I am missing something.

    Create an original single-file program in a file called <YourName> that satisfies this criteria:
    1. Your class includes a constructor that takes a string as a parameter. (Lets call that string the "one_letter" for this discussion.)
    2. Your class implements the Comparable interface such that it compares the "one_letter" given to it via the constructor.

    3. The main method of your class Prints your full name (First and Last)
    4. The main method of your class creates an instance of itself for each letter of your last name and each created instance is stored in a TreeSet.
    5. The main method of your class prints the "one_letter" of each instance stored in the TreeSet by walking the TreeSet via a "Iterator<T>" style iterator, where <T> is your classname.
    and the code I have so far:

    Java Code:
    import java.util.*;
    public class MyNameWeek3Lab implements Comparable {
    	public static String l;
    	public static TreeSet<MyNameWeek3Lab> t1 = new TreeSet<MyNameWeek3Lab>();
    	public static String letter;
    	public MyNameWeek3Lab(String letter){
    		l = letter;
    	public static void printTree(){
    		//Prints TreeSet
    		Iterator<MyNameWeek3Lab> it1 =t1.iterator();
    			MyNameWeek3Lab entry =;
    	public static void main(String[] args) {
    		String name = "My Name";
    		System.out.println("Week 3 Lab, " + name);
    	    name = name.toLowerCase();
    		//add letters to treeset via constructor
    		int i;
    		MyNameWeek3Lab[] x = new MyNameWeek3Lab[0];
    		for(i=0; i < name.length(); i++){
    			x[i] = new MyNameWeek3Lab(Character.toString(name.charAt(i)));
    		//print with iterator
    	public int compareTo(Object o) {
    		// TODO Auto-generated method stub
    		return 0;
    I know that the line "x[i] = new MyNameWeek3Lab(Character.toString(name.charAt(i))) ;" is causing my problems, I had it not using an array, but it was only picking up the last letter ie the "e". I can get the proper output, but not in the way the professor wants. I hate that I can't just do it how it makes sense to me!

    Should I not use an array? Do I just need to write the compareTo first? Could I get some help with the compareTo? I feel it is unnecessary since TreeSet sorts automatically, and I have never used compareTo for anything other than integers, so I'm not sure how to approach it for strings.
    And am I meeting the requirements, anything you can think of to get me closer?
    any and all input is appreciated.

  2. #2
    Onra is offline Member
    Join Date
    Nov 2009
    Rep Power


    First of all object fields should not be static.
    As far as I understand, you are supposed to compare the String field of your object? In that case just call the compareTo() method of String in your own compareTo() method.
    You should also check for o instanceof MyNameWeek3Lab

Similar Threads

  1. Having trouble removing from TreeSet
    By xtrmi in forum New To Java
    Replies: 2
    Last Post: 05-02-2009, 05:01 AM
  2. TreeSet Demonstration
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:34 PM
  3. Working with HashSet and TreeSet
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:34 PM
  4. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 01:20 AM

Posting Permissions

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