开发者

Java class Anagrams

Im new to the java programming language and need help writing a class Anagrams that prints the permutations of words in a sentence. Example: red car -> red car, car red. This is what i have written so far and i think im on the right track and even though my code is not finished, i would at least like to get it to run.

import javax.swing.JOptionPane;

public class Anagrams
{

private String x;
private char[] xarray;
private String[] words;

public void Anagrams(String phrase1)
{
    x = phrase1;
}

public void printPerms()
{
    int perms = 0;
    xarray = x.toCharArray();
    for (int i = 0; i < x.length(); i++)
    {
        if(xarray[i] == ' ') perms = perms + 1;
    }

    words = x.split(" ");
    for (int i = 0; i < perms; i++)
    {
        System.out.println(words[i]);
    }
}

public void main(String args[])
{
    String phrase1 = JOptionPane.showInputDialog("Enter phrase 1.");
    Anagrams(phrase1);   
    printPerms();
}


}

This is the error i get when i try to run.

Exception in thread "main" java.lang.NoSuchMethodError: main

Right now im just trying to get my program to run not print ou开发者_如何学Got the permutations. I think i can figure that out once it at least print something out. Can someone tell me why it doesnt run and how do you get input from the user like c++ cin>>, if there is another way other than JOptionPane.

Thanks


A main method needs to be static.

How about this:

public static void main(String args[])
{
    String phrase1 = JOptionPane.showInputDialog("Enter phrase 1.");
    new Anagrams(phrase1).printPerms();
}


Even After Declaring your main method as static you may or may not be required to make all other methods as static(If calling methods dirctly without use of objects make methods as static).Because a static method can call or use only static data memebers or methods.

And in your code because you have defined all the methods in the same class which contains main method you need to make other methods also as static.


The method should return true if the two arguments are anagrams of each other, false if they are not. For example, anagram(“glob”, “blog”) would return true;and anagram(“glob”, “blag”) false. Assumes that the input strings will contain only letters and spaces. Treat upper- and lower-case letters as identical, and ignore spaces.

<br/> Uses the following algorithm:
<ul>  <li> clean input strings from spaces and convert to lower case
</li> <li>convert to char array and sort them
</li> <li>if sorted arrays are identical, words are anagrams
</li></ul>
*/

public static boolean anagram(String str1, String str2)
{
      //handle nulls
      if(str1==null && str2==null)
         return true;
      else if( (str1==null && str2!=null) || (str2==null && str1!=null) )
         return false;

      //clean input strings from spaces and convert to lower case
      String s1 = str1.replace(" ", "").toLowerCase();
      String s2 = str2.replace(" ", "").toLowerCase();

      //convert to char array and sort them
      char[] cArr1 = s1.toCharArray();
      char[] cArr2 = s2.toCharArray();
      java.util.Arrays.sort(cArr1);
      java.util.Arrays.sort(cArr2);

      //if sorted arrays are identical, words are anagrams
      s1 = new String(cArr1);
      s2 = new String(cArr2);
      return s1.equals(s2);
    }
    public static void main(String[] args)
 {
     //test: anagram(“glob”, “blog”) would return true; anagram(“glob”, “blag”) false.

      System.out.println("anagram(“glob”, “blog”):"+(anagram("glob", "blog")));
      System.out.println("anagram(“glob”, “blag”):"+(anagram("glob", "blag")));
   }


You are missing static in:

public void main(String args[])

The main method needs to be static.

Also you are calling printPerms from main directly (without an object) so it must be made static as well or call them on a Anagram class object.

You are missing the new keyword while creating the object:

Anagrams(phrase1);   
printPerms();

try

new Anagrams(phrase1).printPerms();

Also there is no Anagram class constructor that takes a String. What you have is a method named Anagram as you've specified the return type.

public void Anagrams(String phrase1) {

drop the void.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜