I'm trying to write a simple program to identify if two phrases are anagrams of one another. I'm having trouble with sorting an array of characters and defining xarray and yarray.

public class Anagram1{
  
  private String x;        
  private String y;
  private char[] xarray;  //?
  private char[] yarray;  //?
  
  public Anagram1(String phrase1, String phrase2){          
    x = phrase1;
    y = phrase2;
  }
  
  public void xlettersort(){            
    x= x.toLowerCase();
    xarray = x.toCharArray();
    xarray.sort();
  }
  
    public void ylettersort(){            
    y= y.toLowerCase();
    yarray = y.toCharArray();  //?
    yarray.sort();   //?
  }
  
  public void testAnagram(){    
    xlettersort();
    ylettersort();
    if(xarray == yarray){
      System.out.println("Yes the two phrases is an anagram");
    }
    else{
      System.out.println("No there is no anagram!");
    }
  }
   
}

Can anyone find a logical error in this?

public class Anagram1{
  
  private String x;        
  private String y;
  private char xarray[];  
  private char yarray[];  
  private String a;        
  private String b;
  
  public Anagram1(String phrase1, String phrase2){          
    x = phrase1;
    y = phrase2;
  }
  
  public void xlettersort(){            
    x= x.toLowerCase();
    xarray = x.toCharArray();
    java.util.Arrays.sort(xarray);
    a = java.util.Arrays.toString(xarray);
    
  }
  
    public void ylettersort(){            
    y= y.toLowerCase();
    yarray = y.toCharArray();  
    java.util.Arrays.sort(yarray);
    b = java.util.Arrays.toString(yarray);
    
  }
  
  public void testAnagram(){    
    xlettersort();
    ylettersort();
    if(a == b){
      System.out.println("Yes");
    }
    else{
      System.out.println("No!");
    }
  }
   
}

I've tried this and it works

public class Anagram1{

  private String x;        
  private String y;
  private char[] xarray;  //?
  private char[] yarray; //?

  public Anagram1(String phrase1, String phrase2){  

     x = phrase1;
     y = phrase2;
  }

  public void xlettersort(){            
    x= x.toLowerCase();
    xarray = x.toCharArray();
    java.util.Arrays.sort(xarray);   
  }

    public void ylettersort(){            
    y= y.toLowerCase();
    yarray = y.toCharArray();  
    java.util.Arrays.sort(yarray);    
  }

  public void testAnagram(){    
    xlettersort();
    ylettersort();
    x = new String(xarray);
    y = new String(yarray);
    if(x.equals(y)){
      System.out.println("Yes the two phrases is an anagram");
    }
    else{
      System.out.println("No there is no anagram!");
    }
  }

}

Edited 3 Years Ago by Dani: Formatting fixed

P.S. How do you remove spaces in between words in a string using the replaceAll Method?

ie: how are you / howareyou

anyone?

Anyone what?
If you're talking about your "driver class" question, just write your own if you really want to (though I can't see why for the life of me). There is only one constructor and one method of any external consequence.

import java.util.Scanner;

public class AnagramDriver{
  
  //AnagramDriver reads two words or phrases through scanner
  
  public static void main(String[] args){
    System.out.println("Enter two words or phrase:");
    Scanner s = new Scanner(System.in);
    String q = s.next();
    System.out.println("Enter a second statement:");
    String w = s.next();
    Anagram t = new Anagram(q, w);
    t.testAnagram();
    
  //Input values are entered into testAnagram
  }
}
This question has already been answered. Start a new discussion instead.