Results 1 to 8 of 8

Thread: for each prob

  1. #1
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default for each prob

    Hi i do have problem with this code can any body help me out regarding


    public class Main implements Comparable {

    public String drink;

    public int compareTo(Object o) {
    return 0;
    }

    public static void main(String[] a) {


    Main mn1 = new Main();
    Main mn2 = new Main();
    mn1.drink = "tea";
    mn2.drink = "cafe";
    TreeSet tree = new TreeSet();
    tree.add(mn1);
    tree.add(mn2);
    ////////////////////error block/////////
    for(Main m : tree){ types incompatible
    //////////////////eb////////////////////
    System.out.print(m.drink);
    }

    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    For loop definition is wrong. What you really want to do. Give more details about your application.

  3. #3
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    I am just executing the SCJP Qs

    here i want to iterate each set and want to print the name of each drink.

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    I think you may need to change this to:

    Java Code:
    for(String m : tree){
    }
    I haven't tested it though. Let me know what happens.
    Did this post help you? Please me! :cool:

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    I don't think so. Again there is incompatible variables found. Isn't it?
    Like I said I didnt actually test it but your right. After looking at it further this will actually still cause an error.

    'Cannot convert from element type Object to main'
    Did this post help you? Please me! :cool:

  7. #7
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    I've one more q to get clarified,

    Diff btwn

    Integer i = 0;
    int i=0;

    when we do ++i ,i++// in both cases result is diff

    why is that so

  8. #8
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    ++i is pre increment

    and

    i++ is post increment.

    Take a look at this:

    Incremental Java
    Did this post help you? Please me! :cool:

Similar Threads

  1. Need help on a strange file access prob
    By squad in forum Advanced Java
    Replies: 0
    Last Post: 03-21-2008, 10:55 AM
  2. Prob with an exercise
    By jhetfield18 in forum New To Java
    Replies: 4
    Last Post: 02-15-2008, 06:11 PM
  3. Threading prob..
    By banie in forum Java Applets
    Replies: 0
    Last Post: 02-05-2008, 06:30 AM
  4. having prob with Exception
    By eva in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 05:44 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
  •