Please HELP! JAVA CODE problem
Hello everybody,
I'm a beginner in this field & I am asking for help .. I wrote my code completely & it says [Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Calculator.main(Calculator.java:12)] .
This is my code :
----------------------------------------
Code java:
public class Calculator {
public static void main(String [] args){
if (args.length != 1) {
System.out.println("Usage: java Calculator \"operand1 operator operand2\"");
System.exit(1);
}
int result = 0;
String[] tokens = args[0].split(" ");
switch (tokens[1].charAt(0) ){
case '+' : result = Integer.parseInt(tokens[0]) +
Integer.parseInt(tokens[2]);
break;
case '-' : result = Integer.parseInt(tokens[0]) -
Integer.parseInt(tokens[2]);
break;
case '*' : result = Integer.parseInt(tokens[0]) *
Integer.parseInt(tokens[2]);
break;
case '/' : result = Integer.parseInt(tokens[0]) /
Integer.parseInt(tokens[2]);
}
System.out.println(tokens[0] + ' ' + tokens[1] + ' ' + tokens[2] + " = " + result);
}
}
---------------------------------
DONE ! thank you for help in advance ;)
Re: Please HELP! JAVA CODE problem
Quote:
exception in thread main java.lang.arrayindexoutofboundsexception 1
At line ??? (shown in the error message) the code used an index of 1 in an array with less than 2 elements. Look at that line of the code and see why the code tried to use an index past the end of the array.
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
Re: Please HELP! JAVA CODE problem
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.
Re: Please HELP! JAVA CODE problem
Thanks Norm the error message says it's in line 12 according to CMD (Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Calculator.main(Calculator.java:12))
Quote:
Originally Posted by
Norm
At line ??? (shown in the error message) the code used an index of 1 in an array with less than 2 elements. Look at that line of the code and see why the code tried to use an index past the end of the array.
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
Re: Please HELP! JAVA CODE problem
Did you look at line 12 to see why the code was using an index past the end of the array?
An index of 1 is looking at the second element in the array. The error message says: the array has fewer than 2 elements.
Re: Please HELP! JAVA CODE problem
I can't see what is the problem?
Can you be more specific ?
& how to solve it?
Re: Please HELP! JAVA CODE problem
Take a look at ArrayIndexOutOfBoundsException (Java Platform SE 7 ) to better understand what the exception means.
Next, can you find line 12 in your Java source file? Which line is it? Based on the exception the line must involve an array, and the index that is provided to access the value in the array is illegal. If you're still unsure why the index is illegal, check the length of the array by printing out the length, and/or print out the entire contents of the array.
Remember, programming is not just about writing code. It's also about debugging code. Right now you're learning how to do the latter.