0

I'm working on an assignment and I'm not allowed to use Scanner. Instead we have to use int money = Integer.parseInt(args[0]). I tried writing my code and it compiles but there is a run time error and I was wondering if someone could tell me what I'm doing wrong to get me on the right track!

Here's my code:

class MakingChange {
  public static void main(String[] args) {
    int money   = Integer.parseInt(args[0]);
    int toonies = (money / 200);
    System.out.println(toonies);
  }
}

And the error is

java.lang.ArrayIndexOutOfBoundsException: 0
at Money.main(Money.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

I don't know what to make of this error! Any help would be greatly appreciated

1
  • 6
    Run the program with an argument? Commented Sep 26, 2015 at 23:09

4 Answers 4

3

You should write defensive code, try this:

if(args.length>0){
    int money   = Integer.parseInt(args[0]);
    int toonies = (money / 200);
    System.out.println(toonies);
}else{
    System.out.println("Missing Command Line Arguments");
}
Sign up to request clarification or add additional context in comments.

3 Comments

It printed "Missing Command Line Arguments". What do i have to add to fix this problem, as i just want a random number to be given to me for this assignment?
you have to run your program from command prompt and provide command line arguments. follow this link: javatpoint.com/command-line-argument
ya that's what i ended up doing thank you
2

You are not passing any arguments to your program.

Add this at the start of your method:

if (args.length == 0)
    throw new IllegalArgumentException("No arguments found");

To pass arguments, do this:

java Money 123

Comments

2

The problem is here:

int money = Integer.parseInt(args[0]);

You are probably trying to access the first element of the args array, but the array used is empty (zero items).

Are you sure you are passing any parameters correctly to your application? They should be space separated with the program, while executing your compiled program (here is an example using jar file):

java -jar MyProgram.jar 1234

You could add some error handling code in order to prevent this, such as an if statement checking the args array size:

int money = 0;
if(array.size() != 0) {
    money = Integer.parseInt(args[0]);
}

Or even by using a try-catch block:

try {
    //....
    int money = Integer.parseInt(args[0]);
    //....
}
catch(ArrayIndexOutOfBoundsException e) {
    //Error handling code here.
    System.err.println(e.getMessage());
}

6 Comments

I thought the array is supposed to be 0, that way I would be able to generate a random number. Also, when I tried checking the array size, Dr. Java told me there is no variable named array and it could not find the symbol. I'm sorry if the answers to these are obvious but I'm fairly new to programming and I'm at a loss here.
Well, args is a Java array, containing the command line arguments (notice that main() takes it as argument) passed to your program when executed. args[0] means "the first args array element" (in arrays, counting starts from 0, therefore args[0] is for the 1st array element, args[1] for the second etc).
So then what do I do to fix the error?
Please read my answer in detail. Thanks.
Well, if you're suggesting I change args[0] to args[1] it made no difference.
|
1

Well, an ArrayIndexOutOfBoundsException means that you are trying to access an index in memory that is not used or initialized.

It looks like you are running your program via the command line; you need to pass arguments to your compiled .class file to get this to even run.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.