Results 1 to 5 of 5
  1. #1
    uncanny's Avatar
    uncanny is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default [SOLVED] if else conditions not working. T_T

    Hello, I am new at this forum.

    I have this problem and I dunno what's wrong with my code.

    This is a part of my "compiler" project using the java language. This is a method to determine correct declarations of the variables.

    I am still testing the "int" data type.

    but the problem is... it won't go inside the if(cnemPos1=="int") condition, it goes to the else, even if I cnemPos1 has a value "int". I wonder what's wrong. I hope you can help me out. Thanks a plenty. ^___^

    here is my code.


    Java Code:
    		if(items.contains("=")== true){
    			int state1 = 0;
    			int cPos1 = items.indexOf("=");
    			String cnemPosn = (String)(items.get(cPos1-1));
    			String cnemPos1 = (String)(items.get(cPos1-2));
    			String cnemVal = (String)(items.get(cPos1+1));
    			items.remove(cPos1);
    
    			boolean chk;
    
    			MenuGui.resultArea.append(cnemPos1);
    			MenuGui.resultArea.append("\n");
    			MenuGui.resultArea.append(cnemPosn);
    			MenuGui.resultArea.append("\n");
    			MenuGui.resultArea.append(cnemVal);
    			MenuGui.resultArea.append("\n");
    			MenuGui.resultArea.append("\n");
    
    			if(cnemPos1=="int"){
    				boolean wat = checkDT(cnemPos1,cnemVal);
    				if(wat== true){
    					symDTypeNem.add(cnemPosn);
    					symDTypeL.add(cnemPos1);
    					symDTypeVal.add(cnemVal);
    					MenuGui.resultArea.append("correct");
    					MenuGui.resultArea.append("\n");
    					state1 = 5; //to be changed to state number going to next token.
    				}
    				else if(ifExbalpar(cnemVal)==true){
    					MenuGui.resultArea.append("balanced expression");
    					MenuGui.resultArea.append("\n");
    					state1 = 5;// to be changed
    				}
    				else if(ifExbalpar(cnemVal)==false){
    					MenuGui.resultArea.append("warning! error unbalanced expression");
    					MenuGui.resultArea.append("\n");
    					state1 = 5;// to be changed
    				}
    				else{
    					MenuGui.resultArea.append("incompatible data type, int expected");
    					MenuGui.resultArea.append("\n");
    				}
    			}
    			else{
    				MenuGui.resultArea.append("value of cnemPos1 is no int");
    				MenuGui.resultArea.append("\n");
    			}

  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

    Comparing strings using == is wrong, actually not a best practice. Basically it comparing bit patterns, not meaningfully comparing. So use equals() to compare strings.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Try debugging your code by using println()s For example add a println("cnemPos1+ " cnemPos1):

    before the if test to see what its value is.

    if(cnemPos1.equals("int"))

    A comment. The == true is redundant/not needed below:
    if ((A == 1) == true) ...
    The boolean expression is sufficient.

  4. #4
    uncanny's Avatar
    uncanny is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks a lot, I forgot about equals().

    Now I can move on. :D

  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

Similar Threads

  1. I am working on java!
    By pawankumarom in forum New To Java
    Replies: 33
    Last Post: 03-06-2009, 11:24 AM
  2. Replies: 9
    Last Post: 09-30-2008, 07:30 AM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  4. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 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
  •