• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble running simple sum-up program.

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha!!! beat you by eight seconds!!!

;-)
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic