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 3 of 3

Thread: If statements not working as planned

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default If statements not working as planned

    I currently have a program that builds a table based on some file information. The table has four columns, red, yellow, blue, and green. If a certain file has the word green in it, then the file's name goes in the green column. Same goes for red, yellow etc. For some reason, it's putting everything in the green column.

    The way my code is laid out, it appears that my comparison statements are ineffective. My else statement points to the green column, so perhaps it's not picking up on the other if statements.

    I tried putting a print statement right before the if else block, and the colors are printing out as they should as strings. "red" should equal "red", but for some reason it's not recognizing that. The if else block is below, and I have attached more of the program if it may help.


    if (colorName == "red"){
     
    			// column 0 is where red files go
            		int columnNum= 0;
     
    			//getEmptyCell searches for the first empty cell in a column
            		int rowNum=getEmptyCell(data, columnNum);
            		data[rowNum][columnNum] = s.substring(66);
            	}
            	else if(colorName == "yellow"){
     
    			// column 1 is where yellow files go.
            		int columnNum= 1;
            		int rowNum=getEmptyCell(data, columnNum);
            		data[rowNum][columnNum] = s.substring(66);
            	}
            	else if (colorName == "blue"){
     
    			//...
            		int columnNum= 2;
            		int rowNum=getEmptyCell(data, columnNum);
            		data[rowNum][columnNum] = s.substring(66);
            	}
            	else {
            		int columnNum= 3;
            		int rowNum=getEmptyCell(data, columnNum);
            		data[rowNum][columnNum] = s.substring(66);
            	}
    Attached Files Attached Files


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: If statements not working as planned

    Hello davidvee!
    I guess colorName is a String. And you should use the String's equals() method instead the == operator when comparing Strings.
    Hope this helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    davidvee (July 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: If statements not working as planned

    Quote Originally Posted by andreas90 View Post
    Hello davidvee!
    I guess colorName is a String. And you should use the String's equals() method instead the == operator when comparing Strings.
    Hope this helps.
    That fixed it thank you.

Similar Threads

  1. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  2. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  3. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  4. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM