Results 1 to 20 of 20
  1. #1
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default Int Array Allowed in main() Method?

    So I've been writing a program to try to get the hang of Java and I'm having a bit of trouble. When I compile, I get:

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

    It seems to be that it doesn't like me using an array of ints instead of Strings for the args... or is it something else? Thanks a lot in advance!

    Java Code:
    //Simple program that adds all the numbers entered as arguments.
    
    public class Add {
    
    		public static void main(int[] args) {
    		
    			//Get length of array.	
    			int lengthy = args.length;
    			int comp = 0;		
    
    				
    			
    				for (int i = lengthy; lengthy >= 0; lengthy--) {
    				
    				
    				comp += args[i-1];	
    				
    				
    				}
    
    			System.out.println(comp);
    		
    				
    		}
    		
    	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The "signature" of your main method cannot vary, cannot be an array of ints, Dates, or whatnots. It must have an array of Strings and only an array of Strings. You have no leeway, no say in this (nor do I) as this is simply the way that it has been decreed.

    Now having said that, you can still pass ints to the program, but they will be in String representation. To convert them, simply parse each array item with the Integer.parseInt(...) int.

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

    Default

    Change this,

    Java Code:
    public static void main(int[] args) {
    into this,

    Java Code:
    public static void main(String[] args) {
    As Fubarable says, you have to pass a String array into the main method. You can convert it into different types within the code.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    To clarify, main is a method like any other. There is no difference. You can overload it and have a min that takes an int array or anything else you want. But that overloaded method will not be used to "start" the application.

    Java will use reflection (essentially, if not in fact) to "start" the application. To do so, it will attempt to call a static method main that takes a String array as an argument, and since that "starter" is not in the same package as the Class to be started, that method must be public.

    So, as I have intimated, there is nothing "magical" about the main method. But, a method with exactly that signature must exist in the Class to "start", as that is what the JVM will attempt to execute.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    doesn't like me using an array of ints
    How are you going to pass an array of int to the program? There is not way to enter ints from the keyboard. Only Strings. "1" is not an int.

  6. #6
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks for your help so far. I tinkered with it a little more, and I'm at this now:

    Java Code:
    import java.io*;
    
    //Simple program that adds all the numbers entered as arguments.
    
    public class Add {
    
    		public static void main(String[] args) {
    		
    			//Get length of array.	
    			int lengthy = args.length;
    			int comp = 0;
    			
    			int[ ] intBuff;
    
    
    				for (int i = lengthy; i >=0; lengthy--) {	
    			
    				intBuff[i-1] = Integer.parseInt(args[i-1]);
    			
    				}
    				
    			
    
    				
    			
    				for (int i = lengthy; lengthy >= 0; lengthy--) {
    				
    				
    				comp += intBuff[i-1];	
    				
    				
    				}
    
    			System.out.println(comp);
    		
    				
    		}
    		
    	
    }
    It's telling me it expects ';' before '*' (from the import at the begining) From what I understand, the asterisk indicates that all of the classes in that part of the API will be imported, so I used it like in an example I saw. However, with or without the star, it won't compile....

    (Without the star, is says "Cannot find symbol" (pointint to the period.))

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It's complaining because you forgot the period before the "*":
    Java Code:
    import java.io.*;
    Myself, I try to avoid putting wild card characters in my import statements as it can lead to trouble later. I try to import only the specific class that I need.

    Also, I don't see that your code is currently using any of the io package's classes.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Also,

    1) If you are using an int array such as intBuff, you need to initialize your intBuff array: you need to tell the compiler to create a new int array, and how long you want it.
    2) You might as well start writing for loops as most everyone else does:
    Java Code:
        for (int i = 0; i < args.length; i++)
        {
          //...
        }
    3) Also, do you even need an intBuff array? If all you're doing is parsing the command line argument to an int and then adding it to an int summation variable, then skip with the int array altogether, and in the for loop, parse the String argument and add it to the summation variable. One key to good programming style is to be lazy, to try not do extra work if you don't have to, and here's an example of this.

  9. #9
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Hmmmm... I didn't think of being lazy... hah. Anyway, thanks for all the help, after starting from scratch with your suggestions, the program now compiles and runs! Here's the final code:

    Java Code:
    import java.io.*;
    
    //Simple program that adds all the numbers entered as arguments.
    
    public class Add {
    
    	public static void main(String[] args) {
    		
    		int comp = 0;
    		
    		
    	
    		for (int i = args.length; i >=1; i--) {
    		
    			
    		comp += Integer.parseInt(args[i-1]);	
    			
    		}	
    		
    		System.out.println(comp);
    	}
    
    }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Congrats! Now remove that import and see if it still compiles and runs. If so, leave out the import.

  11. #11
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Yup, it does indeed work without the import. I guess I assumed that Integer.parseInt was in IO. Ah well. Thanks for all the help, I've also coded three more similar programs for Multiplication, Subtraction, and Division. I think I'm starting to get the hang of this.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Question: Why are you going thru the args in reverse order?
    That makes several parts of your code prone to coding errors.

  13. #13
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Question: Why are you going thru the args in reverse order?
    That makes several parts of your code prone to coding errors.
    I don't know, that's just the way I ended up doing it. Is there any particular reason not to do it this way?

  14. #14
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    i got it..thanks for share

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Who is thangbomlennet? == Starclopsofish?

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Well, thangbomlennet.length() == Starclopsofish.length()

    ;)

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    for (int i = args.length; i >=1; i--) {


    comp += Integer.parseInt(args[i-1]);
    The prone to error bits were
    starting the index past the end of the array and decreasing it to 1 more than the lowest index and then subtracting one from it when used.
    Vs having the index start at one end and move to the other.

  18. #18
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Wink big end first

    Quote Originally Posted by Norm View Post
    The prone to error bits were
    starting the index past the end of the array and decreasing it to 1 more than the lowest index and then subtracting one from it when used.
    Vs having the index start at one end and move to the other.
    int[] someints = { 1,55,25,4,74,5,7,7,54};
    int arrayLength = someints.length;
    do
    {
    result = someints[--arrayLength];
    }
    while(arrayLength > 0x00000000);

    imports are for the weak-willed, we must prepare for the return of the Krell.

    anything else is little-endian, an abbreviated existence.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  19. #19
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Who is thangbomlennet? == Starclopsofish?
    System.out.println("wtf");
    thangbomlennet != Starclopsofish;

    Either a) his post went into the wrong thread,
    b) he was just following the thread, or
    c) he's my evil twin. I didn't see a mustache on him though....

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    The problem with nick's code is you'll get an exception if the array is empty when the loop starts.

Similar Threads

  1. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 02:37 PM
  2. A query about main method
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 10:44 AM
  3. Private main method
    By bugger in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 10:45 AM
  4. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 10:25 AM
  5. calling array from main
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:41 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
  •