+ 8
Comparing char.at () with string?
how can i compare char at with string. below program it gives error can anyone help public class Program { public static void main(String[] args) { string x = "adeel"; if (x.charAt(1).equals("a") ) { System.out.println("test passed") ; } else { System.out.println("test failed") ; } } }
4 odpowiedzi
+ 10
this still gives error
public class Program
{
public static void main(String[] args) {
string x = "adeel";
if (x.charAt(1) == 'a') {
System.out.println("test passed") ;
} else
{
System.out.println("test failed") ;
}
}
}
+ 10
thanks buddy it worked
+ 6
you have problem in if statement.
solution:
if(x.charAt(0) == 'a') // print test passed
if(x.charAt(1) == 'a') // print test failed
0
you need to use the .equals method