First off, yeah I know there are many codes out there for this, but how can one learn without doing.
Ok
So we need to write a code to see whether two words are anagrams of one another.
Here is what I have, it compiles but does not work.

The main thing is that when I run the program I want to be able to write in the prompt:
"java Anagram <phrase1> <phrase2>" and then it will tell me.
THank you for any help/suggestions

public class Anagram
{
	private String phrase1;
	private String phrase2;
	private char p1array[];
	private char p2array[];
	
	public void main (String[]arg)
	{
		String phrase1 = arg[0];
		String phrase2 = arg[1];
		
		letterchange(phrase1);
		letterchange(phrase2);
		
		if (isAnagram())
		{
			System.out.println(phrase1+" is an anagram of "+phrase2);
		}
		else
		{
			System.out.println(phrase1+" is not an anagram of "+phrase2);
		}
	}
	
	//make letters all lower case
	public void letterchange(String phrase)
	{
		phrase = phrase.toLowerCase();
	}
	
	//checks to see if all the letters are the same
	public boolean isAnagram()
	{
		p1array = phrase1.toCharArray();
		java.util.Arrays.sort(p1array);
		p2array = phrase2.toCharArray();
		java.util.Arrays.sort(p2array);
		
		if(p1array.equals(p2array))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

Recommended Answers

All 15 Replies

Member Avatar for coil

What output are you getting?
In your letterchange method, when you pass a String in Java, you pass by value, not by reference. In other words, when you make 'phrase' lower case, it changes a copy of the variable, not the variable itself. I'm fairly certain that this is a problem - it is slightly complicated by the fact that phrase 1 and 2 refer to elements of an array, and arrays are different.

A better option would be to have the method return a String(return phrase.toLowerCase()).

The output says: "Exception in thread "main" java.lang.NoSuchMethodError: main

I tried what you said, changing it to this:

public static String letterchange(String phrase)
	{
		return phrase.toLowerCase();
	}

but got the same result as above

Member Avatar for coil

Your main method header (line 8) must be 'public static void main(String[] args'. You're missing the keyword 'static.'

One more thing, you should check for the length of your incoming argument. If the number of argument is not equal to what you want, it could cause your program to crash at the commandline.

tried adding static to the main method and it now says that non-static method isAnagram() cannot be referenced from a static context

and if i change it to public static boolean then there are a bunch more errors

The main method must be static. One way to go is to create a new Anagram object and use the dot notation to access its methods.

public static void main(String args[])
{
   Anagram anag = new Anagram();
   anag.phrase1 = args[0]; //using dot notation to access the members phrase1 and phrase2
   anag.phrase2 = arg[1];

   letterchange(anag.phrase1);
   letterchange(anag.phrase2);
 
   if (anag.isAnagram()) //using the dot notation also to access the isAnagram() method
   {
      System.out.println(anag.phrase1 + " is an anagram of " + anag.phrase2);
   }
   else
   {
      System.out.println(anag.phrase1 + " is not an anagram of " + anag.phrase2);
   }
}

Well that did work. It compiles and runs but says words that are actually anagrams are not.
Eg. fluster restful
I also have to make phrases like "Forty Five" & "Over Fifty" work

Have you fixed your letterchange method as Coil suggested? Strings in Java passed by value to the method. Changing the value of the phrase1 inside the method will not affect it outside of it. You need your method to return the new String and update the variable accordingly:

public String letterchange(String phrase)
{
   return phrase.toLowerCase();
}

And the way to use it:

anag.phrase1 = letterchange(anag.phrase1);

yes I fixed it as mentioned. Still same result.

Member Avatar for coil

In your isAnagram method, try manually printing out the contents of the two char arrays (just before you compare them) and check if they are indeed the same for anagrams.

Show us your isAnagram() method definition?

The problem is with the line

if(p1array.equals(p2array))

It basically tries to check if p1array and p2array are the same object (it uses the native Object's equals method). You have to loop over the array and check if each cell is equal to the same cell of the other array. Since you use sort, the two arrays will be equal if and only if each cell contains the same character.

public boolean isAnagram()
{
   p1array = phrase1.toCharArray();
   java.util.Arrays.sort(p1array);
   p2array = phrase2.toCharArray();
   java.util.Arrays.sort(p2array);
   boolean equal = true;  //to check if all the cells are equal

   if(p1array.length != p2array.length)
   {
	return false; 
   }
		
   for(int i = 0; i < p1array.length; ++i)
   {
      if(p1array[i] != p2array[i])
      {
	equal = false; //one letter does not equal to the other, this is not an anagram.
      }
   }
   return equal;
}

Was going to post the same after I tested the code, but 1 minute too late. :P

I understand. It works now. I had something like that before but I found something else that did it differently so I tried it the other way.

Thanks for all the help

Glad to help, please mark the thread as solved.

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.