Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Anagram program help?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Anagram program help?

    For this program I have it almost completely done. I just need to find out that if the two strings are not anagrams where or why is it not an anagram? ex: banana/sanana = missing b or missing s.

    import java.util.*;
     
    public class lab3 {
     
     
        public static void main(String[] args) {
     
        lab3 run = new lab3();
        Scanner type = new Scanner(System.in);
        String firstStatement;
        String secondStatement;
        boolean anagrams;
     
        for(int i = 0;i<1;i=0){
            System.out.println("Please enter the first string -or type QUIT to quit-.");
            firstStatement = type.nextLine();
            System.out.println("Please enter another string -or type QUIT to quit-.");
            secondStatement = type.nextLine();
     
            if(firstStatement.matches("QUIT"))
                break;
            else if(secondStatement.matches("QUIT"))
                break;
            else{
                anagrams = run.isAnagram(firstStatement,secondStatement);
     
                System.out.println("The two are statements being an anagram is: "+anagrams);
            }
        }
        }
        public boolean isAnagram(String s1, String s2){
            char[] c1 = s1.toCharArray();
            char[] c2 = s2.toCharArray();
            Arrays.sort(c1);
            Arrays.sort(c2);
     
            s1 = String.copyValueOf(c1);
            s2 = String.copyValueOf(c2);
     
            s1 = s1.trim();
            s2 = s2.trim();
     
            if(s1.equals(s2))
                return true;
            else
                return false;
        }
    }

    If you can help me it would be much appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Anagram program help?

    Do you have an algorithm for solving the problem? Given two Strings what does the program need to do to give you the answer you want.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 9th, 2012, 12:47 AM
  2. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 08:29 AM
  3. anagram program wont compile please help
    By Rusak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2011, 07:23 PM
  4. Anagram program wont compile
    By Rusak in forum Member Introductions
    Replies: 0
    Last Post: March 25th, 2011, 03:38 PM