If I enter anagram strings like "act" and "cat" then it gives this error: Exception in thread "main" java.lang.NullPointerException at examples.Anagram.main(Anagram.java:20)
import java.util.Scanner;
public class Anagram {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String 1:");
String str1 = sc.nextLine();
System.out.print("Enter the String 2:");
String str2 = sc.nextLine();
Boolean isAnagram = false;
Boolean[] isVisited = new Boolean[str1.length()];
if(str1.length() == str2.length()){
for(int i=0;i<str1.length();i++){
char c = str1.charAt(i);
isAnagram = false;
for(int j=0;j<str2.length();j++) {
if (c == str2.charAt(j) && !isVisited[j]) {
isAnagram = true;
isVisited[j] = true;
break;
}
if (!isAnagram) {
break;
}
}
}
}
if(isAnagram){
System.out.print("Anagram:)");
}else{
System.out.print("Not a Anagram:(");
}
}
}