Hello I am a new java student. I have a lab in which we have to compare strings to see if they are anagrams. When I compile it I'm not getting any errors but its not running as it should or as it was running the other night. I have looked at the code line by line and can not find where I made a mistake. Can anyone help me find my mistake? Thanks in advance

package lab2;
import java.util.Scanner;
/**
 *
 * @author Jay
 */
public class Lab2
{
    int rc = 0;
    public static void main(String[] args)      
    { 
         Lab2 p1 = new Lab2(); 
        return;
    }//CLOSE MAIN METHOD
    public int process()
    {
        System.out.println("process()");
        int i = 0;
        int k = 0;
        Lab2 p1 = new Lab2();
        int rc = p1.promptUser();
        return 1;
    }//CLOSE PROCESS
    public int promptUser()
    {
            Scanner t = new Scanner(System.in);         
            System.out.println("Enter the 1st word or statement: ");
            String s1 = t.next();
            System.out.println("Enter the 2nd word or statement:");
            String s2 = t.next();
            boolean rc = isAnagram(s1, s2);
            if (rc == false)
            {
                 System.out.println("This is not an anagram");

            }//close if
            return 0;

        }// CLOSE PROMPTUSER
        public boolean isAnagram(String s1, String s2)
        {
            boolean found;
            s1 = s1.trim();
            s1 = s1.toLowerCase();
            s1 = s1.replaceAll(" ", "");
            int i = s1.length();
            s2 = s2.trim();
            s2 = s2.toLowerCase();
            s2 = s2.replaceAll(" ", "");
            int k = s2.length();     
            if( i != k)
            {
                System.out.println("Fails length test");
                return false;
            }//close if

            for(i=0;i < s1.length(); i++)
            {
                found = false;
                for(k=0; k < s2.length(); k++ )
                {
                    if (s1.substring(i,i+1).equals(s2.substring(k,k+1)))
                    {
                        s2 = s2.replaceFirst(s2.substring(k,k+1),"|");
                        found = true;
                        break;
                    }//close if statement
                }//close nested for loop
                if(found = false)
                    {
                        System.out.println("This is not an anagram " +(s1.substring(i,i+1) + " was not found"));
                        return false;
                    }//close if
            }//close for loop           
       return true;
        }   // close isAnagram
}//close class lab 2

Recommended Answers

All 3 Replies

its not running as it should

Please explain.
Also copy and paste here the console output for when the program is executed showing what is wrong and add comments showing what the output should be.

Please edit your code and surround it with code tags. See icon above to right of input area.

i just finally found out. Thanks for your attempted assistance

Glad you figured it out.

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.