Comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • m6s
    New Member
    • Aug 2007
    • 55

    #1

    Comparison

    Hello,

    public static void X(){
    ArrayList o = new ArrayList();
    o.add("a1");
    o.add(o);
    for( Object c: o )
    if(c.getClass() .toString() == "java.lang.Stri ng")
    System.out.prin tln("Found one : " + c);
    }

    Why is n't working as expected?
    if it is a string print "a1" in this example?

    Thank you
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by m6s
    Hello,

    public static void X(){
    ArrayList o = new ArrayList();
    o.add("a1");
    o.add(o);
    for( Object c: o )
    if(c.getClass() .toString() == "java.lang.Stri ng")
    System.out.prin tln("Found one : " + c);
    }

    Why is n't working as expected?
    if it is a string print "a1" in this example?

    Thank you
    1.) Use code tags when posting code.
    2.) Use the equals method when comparing Strings in Java
    3.) Check the docs for the getClass().toSt ring(). For a String it returns the String "class java.lang.Strin g"

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      In this example, unless you really need the flexibility of the class name string, I would use instanceof:

      [CODE=Java]import java.util.*;

      public class X {
      public static void main(String[] args) {
      List<Object> list = new ArrayList<Objec t>();
      list.add("a1");
      list.add(list);
      for(Object element: list) {
      if(element instanceof String) {
      System.out.prin tln("Found one : " + element);
      }
      }
      }
      }
      [/CODE]

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by BigDaddyLH
        In this example, unless you really need the flexibility of the class name string, I would use instanceof:

        [CODE=Java]import java.util.*;

        public class X {
        public static void main(String[] args) {
        List<Object> list = new ArrayList<Objec t>();
        list.add("a1");
        list.add(list);
        for(Object element: list) {
        if(element instanceof String) {
        System.out.prin tln("Found one : " + element);
        }
        }
        }
        }
        [/CODE]
        Spoil sport

        Comment

        • m6s
          New Member
          • Aug 2007
          • 55

          #5
          But with debugger the comparisons seem to work.
          But nothing is printed
          I get into the last print of c, but never prints out!

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by m6s
            But with debugger the comparisons seem to work.
            But nothing is printed
            I get into the last print of c, but never prints out!
            You must be misinterpreting what the debugger is doing.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by m6s
              But with debugger the comparisons seem to work.
              But nothing is printed
              I get into the last print of c, but never prints out!
              The if is never entered. See all the comments given above and try to understand/investigate them. You'll learn a lot that way.

              Comment

              • m6s
                New Member
                • Aug 2007
                • 55

                #8
                Code:
                public static void X(){
                	ArrayList o = new ArrayList();
                	o.add("a1");
                	o.add(o);
                	for( Object element: o )
                	    if(element.getClass().equals( java.lang.String.class ) )
                		System.out.println("Found one : " + element);
                    }
                Instead of getClass().toSt ring().equals(" java.lang.Strin g" )
                I put the one I show you in the code, and now the element is printed.
                BUT. I use netbeans and java5, the previous snippet I posted with debugger the debugger gets in the line "Found one:" and nothing was printed! I don't understand it...why...

                Thank you by the way for your help.

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by m6s
                  BUT. I use netbeans and java5, the previous snippet I posted with debugger the debugger gets in the line "Found one:" and nothing was printed! I don't understand it...why...

                  Thank you by the way for your help.
                  Impossible. Beyond the use of == instead of equals, the toString method would return "class java.lang.Strin g" not "java.lang.Stri ng". Whatever you were doing in the debugger, try to reproduce it! I don't think you can.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by m6s
                    Code:
                    public static void X(){
                    	ArrayList o = new ArrayList();
                    	o.add("a1");
                    	o.add(o);
                    	for( Object element: o )
                    	    if(element.getClass().equals( java.lang.String.class ) )
                    		System.out.println("Found one : " + element);
                        }
                    Instead of getClass().toSt ring().equals(" java.lang.Strin g" )
                    I put the one I show you in the code, and now the element is printed.
                    BUT. I use netbeans and java5, the previous snippet I posted with debugger the debugger gets in the line "Found one:" and nothing was printed! I don't understand it...why...

                    Thank you by the way for your help.
                    I wonder if you are reading the responses here. If you really get to understand them you'd convince yourself that the if is never entered.
                    Remove the if and just print c.getClass().to String() for each element in that list.

                    Comment

                    Working...