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!");
    }
  }

}

Thank you for the help!

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

ie: how are you / howareyou

replaceAll(" ","")

what is the driver class for this program?

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
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.