-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
  • 28,382
  • 11
  • 53
  • 80

3 Answers3

2

you can say

if(z.equalsIgnoreCase("y")){
    //code...
}
Jeeter
  • 5,424
  • 3
  • 43
  • 66
2

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

if (z.equalsIgnoreCase("Y")){//do your job
Pshemo
  • 118,400
  • 24
  • 176
  • 257
1

use equals().

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

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

Trying
  • 13,590
  • 8
  • 67
  • 109