Why Strings Are Not Equal in My Case

Ensuring two string are not equal and ignoring case

while(input.!equalsIgnoreCase("W")) should be

while(!(input.equalsIgnoreCase("W")))

If you are not handling null for input then there can be chance of NullPointerException so I woul do while(!("W".equalsIgnoreCase(input)))

As per the updated code,the solution is

while(!(input.equalsIgnoreCase("W")) && !(input.equalsIgnoreCase("A")) && !(input.equalsIgnoreCase("S")) && !input.equalsIgnoreCase("D")))
{
//Do something
}

Java Does Not Equal (!=) Not Working?

if (!"success".equals(statusCheck))

String is not equal to itself

You are sneaky! The second I is not a lower case latin small i. I hexdumped it:

hexdump -C check
00000000 69 66 20 28 27 69 27 20 3d 3d 20 27 d1 96 27 29 |if ('i' == '..')|
00000010 0a 20 20 20 20 65 63 68 6f 20 27 67 6f 6f 64 27 |. echo 'good'|
00000020 3b 0a 65 6c 73 65 0a 20 20 20 20 65 63 68 6f 20 |;.else. echo |
00000030 27 62 61 64 27 3b 20 20 0a 0a |'bad'; ..|
0000003a

I'll let you look up D1 96 :-) Awesome tricksy riddle. +1

String is not equal to string?

Strings are objects. The == compares objects by reference, not by their internal value.

There are 2 solutions:

  1. Use String#equals() method instead to compare the value of two String objects.

    if (letters[x].equals(cord[1]))
  2. Use char instead of String. It's a primitive, so == will work.

    char[] letters  = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'L'};

    Scanner inp = new Scanner(System.in);
    String input = (inp.nextLine());
    char[] cord = input.toCharArray();

    for (int x = 0; x < 10; x++)
    if (letters[x] == cord[1])
    System.out.println("Fk yeah!");


Related Topics



Leave a reply



Submit