It's not a secret anymore!
The moose likes Beginning Java and the fly likes why doesn't this work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why doesn Watch "why doesn New topic

why doesn't this work?

william roberts

Joined: Sep 25, 2001
Posts: 3
Hi it's me again, the java absolute novice. Can someone please tell me why the little program below doesn't work. It's supposed to sum the positive numbers from ten inputs and to discount any negatives.

forgive me if it's a problem for four year olds, but I'm only two!!

[This message has been edited by Cindy Glass (edited September 27, 2001).]
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Hi William -
Thing are a little mixes up here; first, don't assign total a value from the console; just initialize it to 0 at the beginning of your main(). Second, change your if test to (num > 0).

Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Aaron Wilt
Ranch Hand

Joined: Sep 26, 2001
Posts: 49
Well, I'm sort of new to java too, but I'll give it a shot. It seems like to me that you have some of the workings there, just kind of in a different order. This is how I would do it...
// skiped class def
int total, num, counter; // declare vars
//start for loop, go from 1 to 10
for(counter = 1; counter <= 10; counter++) { <br /> System.out.println("Enter a number"); //prompt user<br /> num = Course_io.readInt(); //read in their int<br /> if(num >= 0) //check to see if num is non-negative
total += num; //if it is, add it to total
} // end of for
System.out.println("The total is " + total); //print total
Note: total += num; is the same as total = total + num. Here we are only checking to see if it is non negative, we don't need an else because when the "if statement" is untrue, it will skip the addition to total. then you hit the closing brace for the "for loop" and it bounces back up to the top incrementing our counter.
Hope this helps!
[This message has been edited by Aaron Wilt (edited September 27, 2001).]
Peter Tran

Joined: Jan 02, 2001
Posts: 783
A couple of things.
First, your for() loop syntax is wrong. You need to use a ';' to separate the individual parts of the for() loop. E.g. for( ; ; ).
Second, I think you want to execute several statements inside the for() loop, so you'll need to put a pair of { } around those statements.
To increment the counter, the standard idiom is to use the incremental operator. E.g. counter++ or ++counter.

I just have to ask, where did you get Course_io? I'm assuming this is some io class from the course you're taking.
william roberts

Joined: Sep 25, 2001
Posts: 3
To Peter Tran - bartender.
Thanks for your help. In answer to you question was downloaded from:
I agree. Here's the link:
subject: why doesn't this work?
It's not a secret anymore!