Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: help me up plz

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy help me up plz

    import java.util.*;
    class MyComp implements Comparator
    {
    public int compare(Object o1 ,Object o2)
    {
    Temp t1 = (Temp)o1;
    Temp t2 = (Temp)o2;
    if(t1.salary>t2.salary)
    return 1;
    elseif(t1.salary<t2.salary)
    return -1;
    else
    return 0;
    }
    public static void main(String...s)
    {
    Comparator m = new MyComp();
    TreeSet ts = new TreeSet(m);
    ts.add(new Temp(10));
    ts.add(new Temp(20));
    ts.add(new Temp(30));
    Iterator i = ts.iterator();
    while(i.hasNext())
    {
    Temp t = (Temp)i.next();
    System.out.println(t.salary);
    }
    }
    }
    class Temp
    {
    int salary;
    Temp(int salary)
    {
    this.salary = salary;
    }
    }


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me up plz

    i am beginner in java
    if there is any silly mistake please forgive me..
    and if possible then plz tell me
    " when we should make new classes in multiclasses program " !!

  3. #3
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: help me up plz

    help me plz

    to understand you problem.Please specify your problem.
    How to ask Questions Smartly
    Last edited by DanBrown; February 5th, 2011 at 07:57 AM.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: help me up plz

    I'm astonished that you actually expected good quality answers by just pasting your code without an ounce of detail specifying what your issue is.

    For anyone willing to investigate further, here's the code in a readable format:
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.TreeSet;
     
    public class MyComp implements Comparator {
     
        public int compare(Object o1, Object o2) {
            Temp t1 = (Temp) o1;
            Temp t2 = (Temp) o2;
            if (t1.salary > t2.salary) {
                return 1;
            } else if (t1.salary < t2.salary) {
                return -1;
            } else {
                return 0;
            }
        }
     
        public static void main(String... s) {
            Comparator m = new MyComp();
            TreeSet ts = new TreeSet(m);
            ts.add(new Temp(10));
            ts.add(new Temp(20));
            ts.add(new Temp(30));
            Iterator i = ts.iterator();
            while (i.hasNext()) {
                Temp t = (Temp) i.next();
                System.out.println(t.salary);
            }
        }
    }
     
    class Temp {
        int salary;
        Temp(int salary) {
            this.salary = salary;
        }
    }

    And for the record, Code I just pasted is working, by adding parenthesis and adding a space to else if.
    Last edited by newbie; February 5th, 2011 at 08:06 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me up plz

    i am sorry for not explaining my question .. i posted in hurry ..btw i got solution
    thnx newbie .......//

Tags for this Thread