how to compare characters to a string java code example

Example 1: how to compare two characters in java

If your input is a character and the characters you are checking against are mostly consecutive you could try this:

if ((symbol >= 'A' && symbol <= 'Z') || symbol == '?') {
    // ...
}

Example 2: how to compare a char to a string java

public class JavaCharacterCompareExample1 {
	public static void main(String[] args) {
		char firstValue = 'A';
		char secondValue = 'B';
		// compare the first char to the second
    	int result = Character.compare(firstValue, secondValue);
    	if (result == 0) {
      		System.out.println("The chars are the same.");
    	} else {
          	System.out.println("The chars are different.");
        }
	}
}

Tags:

Java Example