vineeth Prabhakaran vineeth Prabhakaran - 5 months ago 10
Java Question

Comparing character and String

I am writing a program to check wether the given sentence is a panagram or not, but I am not able to compare each character with the characters in a string.
Can anyone suggest me a method to get the desired output?

Scanner scan = new Scanner(System.in);
String panagram = scan.nextLine();
String word = panagram.toLowerCase();
System.out.println(word);
String a[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z" };
int count = 1;
System.out.println("a=" + a.length);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < word.length(); j++) {

if ((a[i]).equals(word.charAt(j)))// problem occurs here {

count++;
break;
}
}

}

if (count == 26) {
System.out.println("pangram");
} else {
System.out.println("not pangram");
}

Answer

You can use:

(a[i]).equals(String.valueOf(word.charAt(j)))

or change your a array from String[] to char[] and compare the character using the == operator.