user1299463
user1299463

Reputation:

How to check if three chars equal each other and if they do what do they equal?

H I am a bit new to java, and I am trying to figure out how to determine if three chars equal eachother. And if they do equal eachother, I want to figure out what they equal. How should I do this? I don't have any code that would be helpful to this problem.

Upvotes: 0

Views: 38499

Answers (3)

HUNeater
HUNeater

Reputation: 166

This is not an intelligent/dynamic solution, but works:

char a = 'a';
char b = 'b';
char c = 'a'; 

if(a == c && b == c) {
    System.out.println("All chars are same");
} else {
    if(a == b) System.out.println("a equals b");
    if(a == c) System.out.println("a equals c");
    if(b == a) System.out.println("b equals a");
    if(b == c) System.out.println("b equals c");
    if(c == a) System.out.println("c equals a");
    if(c == b) System.out.println("c equals b");
}

Output:

a equals c
c equals a

The dynamic way:

char[] chars = {'a', 'b', 'a'};

for (int i = 0; i < chars.length; i++) {
    char char1 = chars[i];

    for (int i2 = 0; i2 < chars.length; i2++) {
        char char2 = chars[i2];

        if (i != i2) {
            if (char1 == char2) {
                System.out.println(char1 + " equals " + char2);
            } else {
                System.out.println(char1 + " not equals " + char2);
            }
        }
    }
}

Output:

a not equals b
a equals a
b not equals a
b not equals a
a equals a
a not equals b

Upvotes: 5

Stephen C
Stephen C

Reputation: 718826

You test equality of characters using ==

  char c1 = ...
  char c2 = ...
  if (c1 == c2) {
      System.out.println("they are equal");
  }

and you can extend that to multiple tests using the && and || operators ("and" and "or")

  if (c1 == c2 && c2 == c3) {
      System.out.println("they are all equal");
  }

As for "figuring out what they are equal [to]" ... the most obvious interpretation is that you want to print out the value of the character

  if (c1 == c2 && c2 == c3) {
      System.out.println("All three characters are '" + c1 + "'");
      System.out.println("The Unicode codepoint is " + ((int) c1));
  }

The last line convert the character to an integer and prints it out (in decimal). You might do this if the character you are trying to examine is non-printable. Also, there are some cases where two or more distinct Unicode codepoints1 are indistinguishable when displayed.

(Now if you were asking about 1 character strings ... the answer would be very different. You SHOULD NOT compare Strings of any kind using ==. You should use String.equals ...)


1 - Actually, codepoint is not the right term. A char typically represents a Unicode codepoint, but in some cases a codepoint requires two char values ... a surrogate pair. There is a more accurate term for what a char is, but it escapes me at the moment.

Upvotes: 2

Jon Lin
Jon Lin

Reputation: 143896

char c1 = 'a';
char c2 = 'b';
char c3 = 'c';

// are all 3 equal?
if(c1 == c2 && c2 == c3) {
    // print out what the char is
    System.out.println("The characters all equal and is " + c1);
}

Upvotes: 1

Related Questions