Page 1 of 1

Java Help

Posted: 2004-09-15 10:02pm
by Tasoth
I've finished two of the three assignments for homework, but the last is a text book short answer essay. We're supposed to find the errors in the code, but I can't. Could someone help?

Code: Select all

int  number1;
int number2;
int result;

number1 = (4 * 6 - 4) / (10 % 4 - 2 );
number2 = ( 16 / 3) - 2 * 6+ 1;
result = number1 - number2;
my initial thought was division by zero, but I worked it out on paper and it seems fine.

Posted: 2004-09-16 12:03am
by Executor32
I don't know whether these are the errors you had in mind, or simple mistypes while composing the post, but I see two instances of extra spaces and one of a missing space. There is an extra space before number1 in line 2, an extra space before 16 in line 7, and a missing space after the 6 in line 7.

So, if I'm not mistaken, it should read as this:

Code: Select all


int number1;
int number2;
int result;

number1 = (4 * 6 - 4) / (10 % 4 - 2 );
number2 = (16 / 3) - 2 * 6 + 1;
result = number1 - number2;

Posted: 2004-09-16 12:05am
by Crayz9000
It's trivially easy to point out -- if you know the way that Java evaluates the order of operation.

Here's a hint: it's parenthesis, *, /, %, and then + and -.

Now re-evaluate the stuff and you'll see that there is a very blatant and obvious divide by zero error in there.

Posted: 2004-09-16 12:45am
by Tasoth
Thankee so much.