• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sam's Java Hour 4 problem  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble getting this simple program to run successfully. It comes from Sam's Teach Yourself Java in 24 Hours Hour 4 lesson. I have added the three argument values retromingent purple lactose-intolerant into the arguments field using NetBeans IDE Run, Set Project Configuration, Customize field. Also, I entered BlankFiller in the Main Class text field.

*************************

class BlankFiller {
public static void main(String[] args) {
System.out.println("The " + args[0]
+" " + args[1] + " fox"
+"jumped over the "
+ args[2] + "dog."
);
}
}

*************************

Here is my error:

run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at BlankFiller.main(BlankFiller.java:3)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


Can someone help me figure out what I'm doing incorrectly? How would I solve this without using an IDE, ie, how would I enter the arguments directly into the code?
 
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can run from the command line as follows:

C:\<your path here>\> java BlankFiller retromingent purple lactose-intolerant


I'm not sure how command line arguments work in netbeans.... you might try that first.

I also, as a sidenote, recommend folks learn basic coding without the aid of an IDE. Like learning to crawl before learning to walk.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:...I also, as a sidenote, recommend folks learn basic coding without the aid of an IDE...

I agree. When beginning, IDEs can be more of a hinderance than an aid.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To delve more deeply into your questions, because I'm in that mood, you asked:

Q: Can someone help me figure out what I'm doing incorrectly?
A: You stated that, "I have added the three argument values retromingent purple lactose-intolerant into the arguments field using NetBeans IDE Run, Set Project Configuration, Customize field." We do not all have the experience necessary with all IDEs to tell you if that should work. It appears, based on your testimony, that it does not. I personally do not have enough experience with NetBeans to know how to add values that will be passed to the program as command-line arguments at run time. Though I tried myself a time or two to do something similar in other IDEs, I could never get it to work. Running the program at the command line with the necessary arguments eliminated the frustration of making an IDE do something it apparently didn't want to or couldn't.

Q: How would I solve this without using an IDE . . . ?
A: Already answered by J. D.

Q: . . . , ie, how would I enter the arguments directly into the code?
A: The value of String[] args is set at run time just as if the line:

String[] args = { arg1, arg2, arg3, . . ., argn };
where arg1, arg2, arg3, . . ., argn are command line arguments in the same form as suggested by J. D.'s answer,

was executed as the first line of the main() method. As with any static array, the size of args is set at array definition and cannot be changed. The content of args can be modified within the program. So, an to answer the question you might be asking is that you cannot add more args[] values in your code than the number of args[] values specified on the command line at run time.

Finally, the results you observed and the character/performance of the Java language were independent of the editor used to write the source code. Everybody learns differently, and I have seen no evidence that all who use/don't use a certain tool or follow/don't follow a certain approach to study and practice that language will be guaranteed a shorter time to mastery of that programming language. As with all acquired skills, I believe mastery comes with breadth of practice and depth of experience.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help regarding NetBeans (although you might see some differences depending on your version): PassRuntimeArgs - NetBeans.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had the same problem and found that the authors site has updates for this type of error. In this case instead of Run, Run File in Netbeans use Run, Run Main Project.

Presumably there will be other errors like this and the authors site is at http://workbench.cadenhead.org/book/java-6-24-hours/
 
Donny Shuler
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you help, guys!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch George Carter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!