Results 1 to 9 of 9
- 05-15-2011, 09:55 PM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
Problem with statement "Integer.parseInt(args[0]);
See below. Below the class is the error message at running.
I use Netbeans. Saving the class goes ok. But at running it gives the error message. The problem seems to be at the statement "int peter3 = Integer.parseInt(agrs[0]);"
class PeterTry1 {
public static void main(String[] args) {
String peter1 = "p";
System.out.println(peter1);
int peter3 = Integer.parseInt(args[0]);
float peter4 = peter3;
Float peter5 = new Float(peter4);
Integer peter6 = peter5.intValue();
System.out.println(peter6);
}
}
run:
p
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at PeterTry1.main(PeterTry1.java:6)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
- 05-15-2011, 09:59 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Your args array is empty. How do you start your program? Have you set the run arguments in netbeans?
Run -> Set project configuration -> Run -> Input a argument ....
-
Line 6 will tell you where the error is occurring. You appear to be trying to use an array that has no members, whose size is 0. The only array here is the args array. You seem to be trying to get the command line parameters in this program but have neglected to run it with command line parameters.
Solution 1: pass in some parameters when you run this code.
Solution 2: Try to understand what the error message is telling you for often the answer is right there.
- 05-16-2011, 06:29 PM #4
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
Thanks for the hints!! I had entered "10", when I now entered just a one digit argument, it worked fine. Can that have been the reason?
- 05-16-2011, 06:35 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-16-2011, 06:36 PM #6
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
Correction: I answered too quick. It was not the number of digits. It was choosing "Run Main Project" and not "Run File". I used to use "Run File".
Why is that?
- 05-17-2011, 01:45 PM #7
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
I did write it myself though. 1) I am probably the newest of all newbies; 2) I didn't understand the runtime error (and do still not understand it); 3) but by running it as "main project" and not as "file" the code accepts any type of argument.
- 05-17-2011, 02:59 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 05-17-2011, 04:13 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Integer.parseInt() error
By niteangell21 in forum New To JavaReplies: 4Last Post: 02-06-2011, 05:36 AM -
Integer.parseInt("5.843"); Error
By Cemi in forum New To JavaReplies: 3Last Post: 04-15-2010, 05:16 PM -
[SOLVED] Integer.parseInt() problem
By eggmanpete in forum New To JavaReplies: 18Last Post: 02-15-2009, 04:25 PM -
Integer.parseInt?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:31 AM -
Problem with Integer.parseInt()
By Hevonen in forum New To JavaReplies: 2Last Post: 12-14-2008, 03:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks