bpiche
asked on
java anagram program will not compile
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,j ava.lang.S tring)
location: class Anagram
at AnagramDriver.main(Anagram Driver.jav a:14)
Java Result: 1
************************** ********** ********** ********** ********** ********** ********** ********** ********** ********** ***
here's the code. what do you think is wrong?
the program compiles fine, but im getting a runtime error:
**************************
Exception in thread "main" java.lang.RuntimeException
symbol : constructor Anagram(java.lang.String,j
location: class Anagram
at AnagramDriver.main(Anagram
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.");
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.");
}
}
}
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;