-2
import java.util.Scanner;

public class Solution {
    
    static boolean isAnagram(String a, String b) {
        char aa[] = a.toLowerCase().toCharArray();
        char bb[] = b.toLowerCase().toCharArray();
        
        if(a.length() != b.length()) {
            return false;
        }else{
            java.util.Arrays.sort(aa);
            java.util.Arrays.sort(bb);
            return java.util.Arrays.equals(aa,bb);
            
        }
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();
        boolean ret = isAnagram(a, b);
        System.out.println(aa);
        System.out.println( (ret) ? "Anagrams" : "Not Anagrams");
    }

}

Pls help me how to print the variables in isAnagram function to main(). I tried System.out.println(aa); in main() but got error:

Solution.java:35: error: cannot find symbol
        System.out.println(aa);
                           ^
  symbol:   variable aa
  location: class Solution
1 error
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • Isn't the software you're using for the code alarming you that the variable "aa" is not defined in main? Anyways, one quick way to do it is you could print it in the method "isAnagram" where it's defined. – SA.93 Dec 12 '20 at 11:54

3 Answers3

1

You can't access variables from isAnagram function in main() because of their scopes. If you want to print them in console, write print statement inside isAnagram method itself

static boolean isAnagram(String a, String b) {
    char aa[] = a.toLowerCase().toCharArray();
    char bb[] = b.toLowerCase().toCharArray();

    System.out.println(Arrays.toString(aa));
    System.out.println(Arrays.toString(bb));

    if(a.length() != b.length()) {
        return false;
    }else{
        java.util.Arrays.sort(aa);
        java.util.Arrays.sort(bb);
        return java.util.Arrays.equals(aa,bb);
        
    }
}

0

You could prepare the char array aa in main method if you need to print the anagram there:

static boolean isAnagram(char[] aa, char[] bb) {
    if(aa.length != bb.length) {
        return false;
    }

    java.util.Arrays.sort(aa); // `aa` will be sorted outside isAnagram
    java.util.Arrays.sort(bb); // so will 'bb'
    return java.util.Arrays.equals(aa, bb);
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String a  = scan.next();
    String b  = scan.next();
    char[] aa = a.toLowerCase().toCharArray();
    char[] bb = b.toLowerCase().toCharArray();
    boolean ret = isAnagram(aa, bb);
    System.out.println(new String(aa));
    System.out.println( (ret) ? "Anagrams" : "Not Anagrams");
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
0

You can define the character array('aa') as a static instance variable and you will be able to access 'aa' in any method.

Try this way;

import java.util.Scanner;
public class Solution{

  static char aa[];

  static boolean isAnagram(String a, String b) {
    aa = a.toLowerCase().toCharArray();
    char bb[] = b.toLowerCase().toCharArray();

    if(a.length() != b.length()) {
        return false;
    }else{
        java.util.Arrays.sort(aa);
        java.util.Arrays.sort(bb);
        return java.util.Arrays.equals(aa,bb);
        
    }
  }

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    String a = scan.next();
    String b = scan.next();
    scan.close();
    boolean ret = isAnagram(a, b);
    System.out.println(aa);
    System.out.println( (ret) ? "Anagrams" : "Not Anagrams");
  }
}