If statements with characters in Java

Discussion in 'OT Technology' started by RetroEvolute, Apr 21, 2010.

  1. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    This is killing me. I can't see anything wrong with my code, but obviously something I'm doing isn't working as none of my if statements comparing characters are working.

    Assuming c and g are characters, I'm doing it like:

    Code:
    if (c == g) {
       blahblahblah
    }
    else {
    System.out.print("lol");
    }
    
    They should be the same, yet I'm getting lol. Any ideas? Here's my actual code (snippet):

    Code:
    public int[] getPos(char c) {
    		int[] pos = new int[2];
    		//System.out.println(c);
    		for (int i = 0; i < 5; i++) {
    			for (int j = 0; j < 5; j++) {
    				if (matrix[i][j] == c) {
    					pos[0] = i;
    					pos[1] = j;
    				}
    				else {
    					System.out.print("lol");
    				}
    			}
    		}
    		return pos;
    	}
    
    And yes, matrix is an array of chars.
     
  2. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    Nevermind. I figured it out. I forgot to turn the users input from earlier into caps, so my program's internals were comparing lowercase to uppercase, which obviously is not the same. It's all working now.
     

Share This Page