Results 1 to 10 of 10
- 04-20-2010, 03:52 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
Help with my code to find MinOfAll numbers
I am new to Programming world and so to Java,
I tried my hands on writing the following code to find minimum of all the numbers entered via command prompt,
but I am getting the error
Exception in thread "main" java.lang.ArrayIndexOutOfBound Exception :5 at MinOfAll.main
Kindly tell me where am I going wrong?
Java Code:public class MinOfAll{ public static void main(String[] args){ int result = Integer.parseInt(args[0]); for (int i =0 ; i< args.length; i++) { int current=Integer.parseInt(args[i+1]); if (current < result){ result = current; } } System.out.println("Min of all is : " + result); } } [color=red]I/P at console : java MinOfAll 2 3 4 5 6[/color]
- 04-20-2010, 03:54 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Why did you do that?
should be,Java Code:int current=Integer.parseInt(args[i+1]);
Because arrays in Java are zero based indexing.Java Code:int current=Integer.parseInt(args[i]);
- 04-20-2010, 04:21 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
I did that to start the comparison with next element in the array.
Even tough I edit it to,
the output error still persists.Java Code:int current=Integer.parseInt(args[i]);
- 04-20-2010, 04:24 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
How did you run your code? Did you pass relevant parameters?
- 04-20-2010, 04:26 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
This is what I do on console
java MinOfAll 2 3 4 5 6
- 04-20-2010, 04:32 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So that's the error. Can you show your correct code. I don't think that you fix the correct place.
- 04-20-2010, 04:35 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
Java Code:public class MinOfAll{ public static void main(String[] args){ int result = Integer.parseInt(args[0]); for (int i =0 ; i< args.length; i++) { int current=Integer.parseInt(args[i]); if (current <= result){ result = current; } } System.out.println("Min of all is : " + result); } }
- 04-20-2010, 04:36 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Right, so it' working. There cannot be any error at all. You say that you get the same error? It cannot happen.
- 04-20-2010, 04:43 AM #9
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
Thanks Erangs, its working
My bad I had kept the same code at 2 places ad was running the unpdated one.
Thanks again!!
- 04-20-2010, 04:46 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome. :)
If you are satisfied with the solution, please mark the thread solved.
Similar Threads
-
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Code errors (incompatible types, cannot find variables, etc)
By Menre in forum Advanced JavaReplies: 7Last Post: 08-28-2009, 08:23 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
how to find if the code contains deprecated API?
By Pooja Deshpande in forum Advanced JavaReplies: 3Last Post: 05-29-2008, 09:12 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks