Trying to figure out why code wont work. Its gets 2 errors for
String newS1 = s1.sort;
String newS2 = s2.sort;
I dont know how know whats wrong

public class anagram {
  public static void main(String args[]) {
   
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter the first string: ");
    String first = input.nextLine();

    System.out.print("Enter the second string: ");
    String second = input.nextLine();
    System.out.println(
      first + " and " + second + " is " +
      (isAnagram(first, second) ? "anagram." : "not anagram."));
  }

  public static boolean isAnagram(String s1, String s2) {
      String newS1 = s1.sort;
      String newS2 = s2.sort;
    
    if (newS1.length() != newS2.length()) return false;

    for (int i = 0; i < newS1.length(); i++) {
      if (newS1.charAt(i) != newS2.charAt(i))
        return false;
    }

    return true;
  }
}

Does the Strings s1,s2 have an attribute named: sort ?
You can not just imagine methods and attributes and call them. The String object doesn't have such thing:

s1.[B]sort[/B];

Does the Strings s1,s2 have an attribute named: sort ?
You can not just imagine methods and attributes and call them. The String object doesn't have such thing:

s1.[B]sort[/B];

so your saying I need to make a method sort

No I am saying the String class doesn't have such an attribute or method. Whether you need to create one or not it is up to you.

This article has been dead for over six months. Start a new discussion instead.