Computer Science, asked by abhinav98324, 1 month ago

No matter what i type in input it always shows the else statement, ie, What to sayyyy???...

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Kira=new Scanner (System.in);
System.out.println("Are you Abhinav???");
String a= Kira.next();
String b= "yes";
String c= "No";
if (a==b)
{ System.out.println ("Welcome...");
}
else if (a==c)
{ System.out.println("Don't dare to open me again...");}
else {
System.out.println ("What to sayyyy???");}
}
}​

Answers

Answered by keshavsharma938
1

Answer:

Civil Rights Act, (1964), comprehensive U.S. legislation intended to end discrimination based on race, colour, religion, or national origin. ... Title II prohibits segregation or discrimination in places of public accommodation involved in interstate commerce.

Answered by anindyaadhikari13
6

Solution:

Your cøde:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner Kira=new Scanner (System.in);

System.out.println("Are you Abhinav???");

String a= Kira.next();

String b= "yes";

String c= "No";

if (a==b)

{ System.out.println ("Welcome...");

}

else if (a==c)

{ System.out.println("Don't dare to open me again...");}

else {

System.out.println ("What to sayyyy???");}

}

}

Corrected Cøde:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner Kira=new Scanner (System.in);

System.out.println("Are you Abhinav???");

String a= Kira.next();

String b= "yes";

String c= "No";

if (a.equalsIgnoreCase(b))

{ System.out.println ("Welcome...");

}

else if (a.equalsIgnoreCase(c))

{ System.out.println("Don't dare to open me again...");}

else {

System.out.println ("What to sayyyy???");}

}

}

Correction:

  • In Java, two or more strings cannot be compared using == operator.
  • So, no matter what the input is, a==b and a==c will always return false.
  • As the condition is false, if block will not execute. Also, else if block will not execute. At last, else block will execute.

Conclusion:

  • To compare two strings, use any of these two methods - (i) equals() or (ii) equalsIgnoreCase()
  • equals(): It compares two strings checking the cases too.
  • equalsIgnoreCase(): Compares two strings irrespective of the cases.

•••♪

Similar questions