Home java Java Character Comparison

Java Character Comparison

Author

Date

Category

The code should compare the 3rd character of the string with the number 3. But why is the condition wrong?
I’ve tried replacing == with .equals , but the result is the same.

import java.lang.Character. *;

public class Main
  {
    public static void main (String args []) {
     String s = "123456";
    Character result1 = s.charAt (2);
    Character condition = 3;
   if (result1 == condition)
   {
   System.out.println (result1);
   }
   else {
    System.out.println ("There was no comparison");
      }
    }
  }

Answer 1, authority 100%

Thanks for the comment, everything works.

import java.lang.Character. *;
public class Main
  {
    public static void main (String args []) {
     String s = "123456";
    Character result1 = s.charAt (2);
    Character condition = '3';
   if (result1 == condition)
   {
   System.out.println (result1);
   }
   else {
    System.out.println ("There was no comparison");
      }
    }
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions