Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3333470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:48:01+00:00 2026-05-17T23:48:01+00:00

Just finished up a recent homework, but I know it could be more efficient.

  • 0

Just finished up a recent homework, but I know it could be more efficient. It reads two words from command line, ignores spaces and punctuation, and determines if they are anagrams. What I have is below; it’s fully functional as far as I can tell.

/**
 * Find out if a string is an anagram of another string
 * 
 */

import java.util.Arrays;

public class Anagram
{
    public static void main(String[] args)
    {
        if (args.length != 2)
            System.out.println("You did not enter two words!");        
        else            
            printWords(args[0], args[1]);                              
    }

    // method to determine whether two strings have the same chars
    public static boolean areAnagrams(String wordOne, String wordTwo) 
    {
        // new strings for letters only
        String ltrsOnlyOne = lettersOnly(wordOne);
        String ltrsOnlyTwo = lettersOnly(wordTwo);      

        // convert strings to lowercase char arrays
        char[] first = ltrsOnlyOne.toLowerCase().toCharArray();
        char[] second = ltrsOnlyTwo.toLowerCase().toCharArray();

        // sort char arrays using sort method
        Arrays.sort(first);
        Arrays.sort(second);

        if (Arrays.equals(first, second))
            return true;
        else
            return false;
    }

    public static String lettersOnly(String word) 
    {
        int length = word.length();
        StringBuilder end = new StringBuilder(length);
        char x;

        for (int i = (length - 1); i >= 0; i--) {
            x = word.charAt(i);
            if (Character.isLetter(x)) {
                end.append(x);
            }
        }
        return end.toString();
    }

    public static void printWords(String wordOne, String wordTwo)
    {
       boolean b = areAnagrams(wordOne, wordTwo);
       if (b == true) {
            System.out.println(wordOne + " is an anagram of "+ wordTwo);
       }

       if (b == false) {
            System.out.println(wordOne + " is not an anagram of "+ wordTwo);
       }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:48:01+00:00Added an answer on May 17, 2026 at 11:48 pm

    Bug fix: lettersOnly‘s return value is lost

    Your lettersOnly method does not modify its arguments in place, it returns the new strings. When you call it you need to do something with this return value. Otherwise you are simply calling it and throwing away the result.

    // method to determine whether two strings have the same chars
    public static boolean sameChars(String wordOne, String wordTwo) 
    {
        lettersOnly(wordOne);
        lettersOnly(wordTwo);
        wordOne = lettersOnly(wordOne);
        wordTwo = lettersOnly(wordTwo);
    
        ...
    }

    Dead code removal

    In main() you call sameChars without checking its return value. Since sameChars doesn’t modify its arguments in place or have any other side effects, this call is dead code and should be removed.

    public static void main(String[] args)
    {
        if (args.length != 2) {
            System.out.println("You did not enter two words!");
        }
    
        sameChars(args[0], args[1]);        
        printWords(args[0], args[1]);             
    }

    Refactoring the if statements

    While your if statements are correct, using == to compare against true and false is not idiomatic. There is no need for explicit comparisons when you are using boolean values. It is shorter and reads better to simply omit the comparisons.

    if (sameChars(wordOne, wordTwo) == true) {
    if (sameChars(wordOne, wordTwo)) {
        System.out.println(wordOne + " is an anagram of "+ wordTwo);
    }
    if (sameChars(wordOne, wordTwo) == false) { if (!sameChars(wordOne, wordTwo)) { System.out.println(wordOne + " is not an anagram of "+ wordTwo); }

    Notice how == false is replaced by the equivalent ! (NOT).

    Actually, there is no reason to repeat the call to sameChars. Having code that looks so similar ought to raise a small alarm in your mind. "There should be a way to eliminate the duplicate code," you think. Ah yes, let's switch the second if to an else!

    if (sameChars(wordOne, wordTwo)) {
        System.out.println(wordOne + " is an anagram of "+ wordTwo);
    }
    else {
        System.out.println(wordOne + " is not an anagram of "+ wordTwo);
    }

    Advanced

    Along similar lines, you could simplify the following set of statements:

    if (Arrays.equals(first, second))
        return true;
    else
        return false;
    

    When the result of equals() is true you return true. Otherwise when it is false you return false. If you think about it, you are effectively just returning whatever equals returned. You could actually eliminate the if and else entirely and simply return the result of equals directly. Huh!

    return Arrays.equals(first, second);
    

    This change is a bit more advanced than the earlier one. You might find this change a bit odd if you've never seen it before. But it's indeed equivalent to the four line version.

    Loop reversal

    Typically when programmers write for loops they start at 0 and iterate up to some upper bound. Is there a reason you wrote your loop as a descending loop?

    for (int i = (length - 1); i >= 0; i--) {
        x = word.charAt(i);
        if (Character.isLetter(x)) {
            end.append(x);
        }
    }
    

    Some programmers will loop backwards like this in a questionable attempt to be "more efficient". As in, they only perform the calculation (length - 1) once, so it's slightly faster. Personally I consider this a waste of time and brain power.

    In fact, written backwards your lettersOnly function has the odd side effect of returning the strings in reverse order after it strips out the unwanted characters. It turns out this doesn't matter since you later sort the characters into alphabetical order. But this is a type of thing that could bite you later. It just so happened that you got away with the reversal because of the sorting. I would switch the loop to iterate in normal 0-to-n order:

    for (int i = 0; i < length; ++i) {
        x = word.charAt(i);
        if (Character.isLetter(x)) {
            end.append(x);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just finished building my program, but I noticed that vb.net (2010) creates two
I just finished a homework problem for Computer Science 1 (yes, it's homework, but
I just finished work on a C++-program where I've implemented my own exceptions (although
i just finished Coding my PHP application now the coding has become somewhat Huge
I just finished an open source project for the company I was interning with.
I just finished working out a memory allocation problem with the current program I'm
I've just finished putting together a basic flash video chat client that publishes the
I have just finished creating my signup form and now ready to insert data
I have a project that I've just finished painstakingly rebuilding because duplicating a target
We all know that App Engine limits you to 1 MB for most input/output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.