File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Trouble running simple sum-up program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble running simple sum-up program." Watch "Trouble running simple sum-up program." New topic
Author

Trouble running simple sum-up program.

Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Hello.

I was wondering if anyone could check this code for me?
It's supposed to sum-up any number of input values given by the user, e.g. if the user wishes to add together three values (for example), the program would then go on to prompt the user for those three values and they would type something like 1, 2, 3 with the sum being 6.

The catch is that all values typed in must be between 1 and 100 inclusive and if the user types in -9999, the program should terminate.

Any help/advice/guidance would be greatly appreciated.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

What exact problem are you having? Does this compile? Does it crash? Does it give the wrong total?

It's hard to help you if you don't TellTheDetails (<--click that).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
fred rosenberger wrote:What exact problem are you having? Does this compile? Does it crash? Does it give the wrong total?

It's hard to help you if you don't TellTheDetails (<--click that).


It compiles perfectly, but no matter what number I type in when the program asks me how many values I'm adding together, it will only let me enter one value and then it crashes.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

so...you must be seeing something different than me. When I compile and run it, it lets me enter one number, and then it hangs - it sits there seemingly doing nothing, until i kill it.

Is that what you mean by 'crash'? The term 'crash' means that the JVM quits, it prints an ugly (but meaningful) message to the screen, and then are back at your DOS/CMD window (assuming that is how you are running it.

My initial impression is that you started writing too much code too soon.

Clearly, your program runs up to line 20, as it takes the input value. But look at your lines 22-26. What do you think this is doing?


Assume i input a value of 7 as my input. What do you think will happen? If you're not sure, stick a System.out.println() statement in the body of the loop, something like this:

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Your while loop condition checks the value of currentValue, but that value never changes, so you will either never enter that loop or never exit from it.


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

Joanne Neal wrote:Your while loop condition checks the value of currentValue, but that value never changes, so you will either never enter that loop or never exit from it.
awww...you gave it away.
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
fred rosenberger wrote:
Joanne Neal wrote:Your while loop condition checks the value of currentValue, but that value never changes, so you will either never enter that loop or never exit from it.
awww...you gave it away.


Lol. xD
Well I kinda knew it was something to do with the while loop that was causing the problem, as everything else seems to be working fine.

I don't quite understand why the currentValue never changes at the moment though, as I thought that was being in-putted by the user.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Sorry Fred

Hayles Berry wrote:I don't quite understand why the currentValue never changes at the moment though, as I thought that was being in-putted by the user.

Which line of code gets input from the user ?
Does that line of code ever get called once your while loop starts ?
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Joanne Neal wrote:
Which line of code gets input from the user ?
Does that line of code ever get called once your while loop starts ?


I tried calling that line inside the while loop and that now allows me to enter the correct number of values and terminate the program with -9999.
My only problem now is that the result of all positive integers I enter is being output as 0. My guess is maybe my 'sum = sum + currentValue;' line is possibly in the wrong place? ^^;;
I could be wrong, but I can't see anything else so far that could be causing this.

Here's what I have up to now:

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Hayles Berry wrote:My guess is maybe my 'sum = sum + currentValue;' line is possibly in the wrong place? ^^;;

How many times does the message
in the while loop, sum is
get printed. Does that give you a hint as to what your problem is ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

So I think at this point, I need to give my common speech...

I would suggest you step away from the computer. Stop thinking about Java, about coding, about data and control structures. I think you are trying to design as you code.

What you need to do is write down in English the steps you need to take. Pretend you have paper, pencil, and eraser. You ask me how many numbers I'm going to give you. then, you ask for them one at a time. Somehow you'll know when I'm done giving you numbers. And then, you will tell me the total. You can also tell me "nope, that is an invalid number for me to take", at which point you discard what I say.

How would you do it? Better yet, write down the directions so that a 10-yr old child can follow them. This often requires you to write the steps down, look at what you wrote, and revise/expand upon them.

Only when you have literally done the above should you really code.

THEN....

When you start coding, only write a few lines at a time. break your code up into discrete methods. For example, you could have a method that gets input from the user. Write that method, and test the heck out of it. Once you KNOW that works, you can use it, perhaps in another method that takes a parameter on how many times to ask the user for input. Perhaps you'd have another method that asks the user how many numbers they want to input.

You really don't want all your code in one main() method.
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Joanne Neal wrote:
Hayles Berry wrote:My guess is maybe my 'sum = sum + currentValue;' line is possibly in the wrong place? ^^;;

How many times does the message
in the while loop, sum is
get printed. Does that give you a hint as to what your problem is ?


Well I've made it so that message is now only printed once, but the addition still isn't being calculated correctly.
Can't seem to see what I need to do with the brackets, either:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

IMHO, they made a big mistake in java with curly brackets, making them optional in some cases.

A while or a for loop, and an if statement, do NOT need brackets if there is only one line in the body.

Further, you can put a pair of curly brackets almost anywhere.

Those two things are causing you problems. Look at this:


So in the above, line 2 is looped as the body of your while. Lines 3-5 are NOT part of any loop. They are run exactly once (although it is only line 4 that does anything).

Have you taken my advice and really THOUGHT THROUGH what you need to do at each step? You seem to be sort of making random changes, hoping each change works. That will never work if you get to anything remotely complicated.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Hayles Berry wrote:Well I've made it so that message is now only printed once,

Are you saying that with the previous code, the message was printed multiple times ? If so, then you weren't running the code you posted.

If a while loop is not followed immediately by a { (before any other statement) then only the next line of code is included in the while loop. Look at the position of your opening { and the currentValue = input.nextInt(); line.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
fred rosenberger wrote:So in the above, line 2 is looped as the body of your while. Lines 3-5 are NOT part of any loop. They are run exactly once (although it is only line 4 that does anything).

awww...you gave it away.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

Ha!!! beat you by eight seconds!!!

;-)
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
I actually have it working now guys, but there's one more thing I'd like the program to do now as well as printing out the sum and that is to print out the largest number entered by the user.

I know I'll need a variable called something like 'largest' which is initialised to a small value to compare each user in-putted value to, but I'm not too sure as to what point in the program I'd need to do the comparison.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2447
    
  28

How would you do it if you had to do it by hand? How would you do it if you were the computer? The answer will be clear to you if you put yourself in the computer's shoes and pretend that you yourself are the computer.

Going back to fred's speech, I think you are still trying to solve the problem by programming through it. You are thinking too much about programming it, than actually solving it

Or as Mystic Jayesh likes to say:- Don't try to program the computer. Be the computer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble running simple sum-up program.