Results 1 to 9 of 9
  1. #1
    havfunonline is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default NoSuchMethodError : Main (isn't that a blatant lie)

    I'm trying to create a random word generator based on a user input of "a"s and "b"s, I know I haven't validated the input yet, but what worries me is that it doesn't work at all, it just spits up an error, which t'interweb claims is very rare

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

    I don't understand what's wrong, code is below, all/any help is appreciated, thank you!

    Java Code:
     import java.util.*;
    
    class CarolVordeman{
        public static void main(String[] arguments) {
    		String[] vowels = {"a", "e", "i", "o", "u"};
    		String[] consonants = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
    	
    	Random generator = new Random();
    	int arraylength = arguments.length;
    	String[] output = new String[arraylength];
    	
    	for (int count=0; count < arguments.length; count++) {
    		if (arguments[count] == a) {
    			int r = generator.nextInt(5);
    			output[count] = vowels[r];
    		}
    		else {
    			int r = generator.nextInt(21);
    			output[count] = consonants[r];
    		}	
        }
    	System.out.println();
    	for (int count2=0; count2 < output.length; count2++) {
    		System.out.print(output[count2]);
    	}
    	}	
    }

  2. #2
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    You have no Main Class. The compiler is looking for a Main Class. And in the Main Class it look for the Main Method. Thats why the COmpiler cannot find your Main Method because you dont have a Main Class ^_^

    Here is my Suggestion:
    Change this:
    Java Code:
    class CarolVordeman{
    TO this

    Java Code:
    public class Main {
    I Hope that helps
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    No, I don't think that you need a class by any given name.
    Here's what I get when I try to execute a class that does NOT have a main method:
    D:\JavaDevelopment\Testing>java Testing.WriteSerializedData
    Exception in thread "main" java.lang.NoSuchMethodError: main
    Check that the class file is correct and what you think it is.
    Copy and paste the console when you get the error.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    For me the above code is ok, except one thing on the if statement, about comparing arguments array.

    Back to the question, as Norm says it check the class name and the file name. But even in such a case you should comes with ClassNotFoundException. But here you get NoSuchMethodError. Anyway, check file name and class name.

  5. #5
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    I stand corrected ^_^
    I didnt get the error you got. here is the code. I Just fixed 1 line

    Java Code:
    import java.util.*;
    
    class CarolVordeman{
        public static void main(String[] arguments) {
    		String[] vowels = {"a", "e", "i", "o", "u"};
    		String[] consonants = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
    	
    	Random generator = new Random();
    	int arraylength = arguments.length;
    	String[] output = new String[arraylength];
    	
    	for (int count=0; count < arguments.length; count++) {
    		if (arguments[count].equals("a")) {
    			int r = generator.nextInt(5);
    			output[count] = vowels[r];
    		}
    		else {
    			int r = generator.nextInt(21);
    			output[count] = consonants[r];
    		}	
        }
    	System.out.println();
    	for (int count2=0; count2 < output.length; count2++) {
    		System.out.print(output[count2]);
    	}
    	}	
    }
    It works fine for me. Here is my output:
    Java Code:
    C:\>java CarolVordeman
    A 2 o 9 d
    
    phxds
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    havfunonline is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    Works like a charm, you guys rock, cheers!

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    It looks like the OP was getting a compiler error on the original program and didn't see. There was an old program class file with the same name that didn't have a main() method.
    After doing a compile that failed, he tried to execute the old program and got the error.
    When he fixed the program so that it compiles, the old version of the class file was replaced by the new code and it now works.

  9. #9
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Yup thats the only thing i've done. ^_^ And Norm has a point. Cheers! ^_^
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

Similar Threads

  1. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 09:37 PM
  2. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 09:25 AM
  3. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  4. Replies: 1
    Last Post: 08-06-2007, 09:11 AM
  5. Problem with main
    By Hyder in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-28-2007, 10:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •