aspose file tools*
The moose likes Beginning Java and the fly likes Question about jGrasp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about jGrasp" Watch "Question about jGrasp" New topic
Author

Question about jGrasp

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
I am trying to run this program in jGrasp:


It compiles ok, but when I try to run it I get this response:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at RadiusToArea.main(RadiusToArea.java:4)


I can run it in the command prompt ok by putting in this:
java RadiusToArea 3

and I get back and answer. My question is, does jGrasp have a way to run a program and then put in arguments when necessary?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This is not an issue with JGrasp -- it's the code itself.

When you say "args[0]" you are referencing the first element in the array args. However, if this array has a length of zero -- which happens when the program is run without any command line arguments -- then the exception results.

You can avoid this by checking the array's length first. For example...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Ok I think I get it. So I am not giving the programming anything to go on, it's just saying (yes, I am personifying a computer program) "This is the way it is, so this is what I am going to do" as opposed to an "if/then" giving it a one or the other. But this has nothing to do with allowing the user to input an argument, right?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jake Miller:
So I am not giving the programming anything to go on, it's just saying (yes, I am personifying a computer program) "This is the way it is, so this is what I am going to do" as opposed to an "if/then" giving it a one or the other...

Right. When you use args[0] without checking first, you are assuming that the program is being provided these arguments and args[0] actually exists. If that assumption is wrong, then the program will fail. By checking this assumption, you can make the program more flexible and more forgiving of user error. You can basically say, "In most cases, I expect this to run using arguments in the args array. But if the user forgets, I'll resort to default values, or maybe I'll prompt the user for input," which (I think) is your next question...
Originally posted by Jake Miller:
... But this has nothing to do with allowing the user to input an argument, right?

If you want the user to be able to "interact" with the program while it's running, you need a way to get their input. For example, an instance of Scanner...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
An alternate approach is to check the input command-line arguments.
Remember they constitute an array, and every array has a length field. You can put in a check forand you have various options for the else, eg
  • print out "usage java RadiusToArea <radius>"
  • choose a default value
  • Close the program
  • etc etc
  • BTW: You might find Double.parseDouble(args[0]) simpler to write.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Question about jGrasp