Results 1 to 20 of 20
- 10-02-2008, 06:21 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
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); } }
-
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.
- 10-02-2008, 07:57 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Change this,
into this,Java Code:public static void main(int[] 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.Java Code:public static void main(String[] args) {
- 10-02-2008, 08:22 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 10-02-2008, 02:08 PM #5
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.doesn't like me using an array of ints
- 10-02-2008, 11:35 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
Thanks for your help so far. I tinkered with it a little more, and I'm at this now:
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....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); } }
(Without the star, is says "Cannot find symbol" (pointint to the period.))
-
It's complaining because you forgot the period before the "*":
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.Java Code:import java.io.*;
Also, I don't see that your code is currently using any of the io package's classes.
-
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:
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.Java Code:for (int i = 0; i < args.length; i++) { //... }
- 10-03-2008, 12:11 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
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); } }
-
Congrats! Now remove that import and see if it still compiles and runs. If so, leave out the import.
- 10-03-2008, 01:06 AM #11
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
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.
- 10-03-2008, 02:40 AM #12
Question: Why are you going thru the args in reverse order?
That makes several parts of your code prone to coding errors.
- 10-03-2008, 04:44 AM #13
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
- 10-03-2008, 06:47 AM #14
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
i got it..thanks for share
-
Who is thangbomlennet? == Starclopsofish?
- 10-03-2008, 07:44 AM #16
Well, thangbomlennet.length() == Starclopsofish.length()
;)
- 10-03-2008, 02:17 PM #17
The prone to error bits werefor (int i = args.length; i >=1; i--) {
comp += Integer.parseInt(args[i-1]);
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.
- 10-03-2008, 02:28 PM #18
big end first
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
- 10-03-2008, 03:53 PM #19
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
- 10-03-2008, 04:22 PM #20
Similar Threads
-
Calling main method
By eva in forum New To JavaReplies: 7Last Post: 11-06-2009, 01:37 PM -
A query about main method
By mew in forum New To JavaReplies: 2Last Post: 12-24-2007, 09:44 AM -
Private main method
By bugger in forum New To JavaReplies: 1Last Post: 12-21-2007, 09:45 AM -
main method
By eva in forum New To JavaReplies: 5Last Post: 12-19-2007, 09:25 AM -
calling array from main
By nalinda in forum New To JavaReplies: 1Last Post: 11-17-2007, 09:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks