Search code examples
javaswingjoptionpaneanagram

Converting System.out.print to JOption Pane?


Want to output this program which tells if 2 words are anagrams or not. I was wondering how to change all the System.out.print commands to JOption Pane commands! I would love any help, as I am a 1st year programmer and have this assignment due.

import java.util.Arrays;

public class NewAnagram
{
public static void main(String[] args)
{
    if (args.length != 2)
    {
        System.out.println("2 words have not been entered!");        
    }
    else
    {
        printPhrases(args[0], args[1]);                              
    }
}

public static boolean anagramSearch(String phrase1, String phrase2) 
{

    String ltrsOnlyOne = lettersOnly(phrase1);
    String ltrsOnlyTwo = lettersOnly(phrase2);      
    char[] first = ltrsOnlyOne.toLowerCase().toCharArray();
    char[] second = ltrsOnlyTwo.toLowerCase().toCharArray();

    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 j;

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

public static void printPhrases(String phrase1, String phrase2)
{
    boolean isFound = anagramSearch(phrase1, phrase2);
    if (isFound == true) 
    {
        System.out.println(phrase1 + " is an anagram of "+ phrase2);
    }

    if (isFound == false) 
    {
        System.out.println(phrase1 + " is not an anagram of "+ phrase2);
    }
}

}


Solution

  • A line like

    System.out.println(phrase1 + " is not an anagram of "+ phrase2);
    

    would be in JOptionPane version this:

    JOptionPane.showMessageDialog(null, phrase1 + " is not an anagram of " + phrase2);
    

    where the first parameter is the parent of the dialog. Since you don't have any other windows in your application, just pass null.