-3

I wanna ask you , do you have a solution for this question (if statement) or where is the error ?

String z = input.nextLine() ;  //i want from the user value of z  .
if (z ==( "Y" || "y" )) 
{  
    statement ... 
}     
else if (z==("N" || "n" ))    
{  
    statement ... 
}

How to write a condition for if when the condition of String in Java ?

Esko
  • 29,022
  • 11
  • 55
  • 82

3 Answers3

2

you can say

if(z.equalsIgnoreCase("y")){
    //code...
}
Jeeter
  • 5,887
  • 6
  • 44
  • 67
2

You are probably looking for equalsIgnoreCase. You can use it like

if (z.equalsIgnoreCase("Y")){//do your job
Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

use equals().

== does reference equality which you don't want.

So use equals() as you want to check the content equality.

Trying
  • 14,004
  • 9
  • 70
  • 110