java anagram program will not compile

bpiche
bpiche used Ask the Experts™
on
im writing an anagram program for an introductory java course. the program reads in two Strings from the user. it then converts each String to lower case, puts each lower case String into an array, sorts each array, and then compares the arrays to see if they are equal. if they are, then the program replies that the Strings are indeed anagrams of each other; if not, it replies that they are not.
the program compiles fine, but im getting a runtime error:
***********************************************************************************************************************
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol  : constructor Anagram(java.lang.String,java.lang.String)
location: class Anagram
        at AnagramDriver.main(AnagramDriver.java:14)
Java Result: 1
***********************************************************************************************************************
here's the code. what do you think is wrong?
import java.util.Scanner;
 
public class AnagramDriver {
 
  public static void main(String[] args){
    System.out.println("Welcome to AnagramDriver. This program reads two words or");
    System.out.println("phrases and answers if the input are anagrams of one another.");
    System.out.println("Enter two words or phrases:");
    Scanner s = new Scanner(System.in);
    String input1 = s.next();
    System.out.println("Enter a second statement:");
    String input2 = s.next();
    Anagram t = new Anagram(input1, input2);
    t.isAnagram();
 
  }
}
=============================================================================
public class Anagram{
 
  public String input1;
  public String input2;
  private char array1[];
  private char array2[];
  private String anagram1;
  private String anagram2;
 
 
  public void Anagram1(String a, String b){
    input1 = a;
    input2 = b;
  }
 
  public void input1Sort(){
    input1= input1.toLowerCase();
    array1 = input1.toCharArray();
    java.util.Arrays.sort(array1);
    anagram1 = java.util.Arrays.toString(array1);
 
  }
 
    public void input2Sort(){
    input2= input2.toLowerCase();
    array2 = input2.toCharArray();
    java.util.Arrays.sort(array2);
    anagram2 = java.util.Arrays.toString(array2);
 
  }
 
  public void isAnagram(){
    input1Sort();
    input2Sort();
    if(array1 == array2){
      System.out.println("The input are anagrams of one another.");
    }
    else{
      System.out.println("The input are not anagrams of one another.");
    }
  }
 
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You should link your anagram somehow.

For example, put your class in a package :

package anagram;

(on the beginning of your Anagram class file)

then, in your anagramdriver java file :

import anagram.Anagram;

public void Anagram1(String a, String b){
  input1 = a;
  input2 = b;
}


These lines of code should be the following:


public Anagram(String a, String b){
  input1 = a;
  input2 = b;
}


A constructor...
1) Must have the same name as the class
2) Must not have a return type


I assumed that these two classes were in different files, but if they aren't, then Anagram can't be declared public.  You should also consider putting them in a package (like flob9) said; this is unnecessary, but has become the standard in Java.

I hope this helped.
Oh, I forgot to add the modified code.  All I did was modify that constructor like I said in my previous post.  The code provided below compiled and ran on my system without any errors in one AnagramDriver.java file.
import java.util.Scanner;
 
public class AnagramDriver {
 
  public static void main(String[] args){
    System.out.println("Welcome to AnagramDriver. This program reads two words or");
    System.out.println("phrases and answers if the input are anagrams of one another.");
    System.out.println("Enter two words or phrases:");
    Scanner s = new Scanner(System.in);
    String input1 = s.next();
    System.out.println("Enter a second statement:");
    String input2 = s.next();
    Anagram t = new Anagram(input1, input2);
    t.isAnagram();
 
  }
}
 
//=============================================================================
 
class Anagram{
 
  public String input1;
  public String input2;
  private char array1[];
  private char array2[];
  private String anagram1;
  private String anagram2;
 
 
  public Anagram(String a, String b){
    input1 = a;
    input2 = b;
  }
 
  public void input1Sort(){
    input1= input1.toLowerCase();
    array1 = input1.toCharArray();
    java.util.Arrays.sort(array1);
    anagram1 = java.util.Arrays.toString(array1);
 
  }
 
    public void input2Sort(){
    input2= input2.toLowerCase();
    array2 = input2.toCharArray();
    java.util.Arrays.sort(array2);
    anagram2 = java.util.Arrays.toString(array2);
 
  }
 
  public void isAnagram(){
    input1Sort();
    input2Sort();
    if(array1 == array2){
      System.out.println("The input are anagrams of one another.");
    }
    else{
      System.out.println("The input are not anagrams of one another.");
    }
  }
 
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial