• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about jGrasp

 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jake Miller
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic