+ 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") ; } } }

11th Feb 2017, 11:23 AM
adeel salim
adeel salim - avatar
4 Réponses
+ 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") ; } } }
11th Feb 2017, 12:34 PM
adeel salim
adeel salim - avatar
+ 10
thanks buddy it worked
11th Feb 2017, 12:37 PM
adeel salim
adeel salim - avatar
+ 6
you have problem in if statement. solution: if(x.charAt(0) == 'a') // print test passed if(x.charAt(1) == 'a') // print test failed
11th Feb 2017, 11:33 AM
Samuel Kurnas
0
you need to use the .equals method
11th Feb 2017, 1:29 PM
Rakin Degiya
Rakin Degiya - avatar