• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Random Number Generator then Summing

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am brand new to programming and java. I decided I wanted to learn programming so now I'm in a class. I am into my second week and my assignment is to generate 100, 3 digit numbers then add them together. I understand how vast java is, but as of right now we have only studied if/else/switch statements, so naturally I had no idea how to begin this project. After doing a little homework I was able to get the 100, 3 digit numbers generated, but I cannot add then together. I have tried a few things that I found online, but either I didn't understand them or I could not get things to work out. Can someone please help give a nudge (or at my level, a violent shove) in the right direction. I appreciate the help.

 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings and Welcome,

ok - first you should use the code tag for your code -- but there is so little that should not be an issue for this post but in the future please use the code tags.

Now - two issues - you are declaring a new int each time you generate a random number - you should move the declaration out of the loop and only set the value in the loop.

So for your question - if you declare a variable ( say: int sum - 0; ) outside the loop, and the right after to generate and set the random number in the loop you add that to sum.



hope this helps

-steve
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Fahlbusch wrote:Now - two issues - you are declaring a new int each time you generate a random number - you should move the declaration out of the loop and only set the value in the loop.


Should he? What about Bloch's Minimize the scope of local variables rule?
According to Bloch:

Effective Java (2nd Edition) wrote:The most powerful technique for minimizing the scope of a local variable
is to declare it where it is first used. If a variable is declared before it is used, it’s
just clutter—one more thing to distract the reader who is trying to figure out what
the program does.

 
Marshal
Posts: 80775
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

I added the code tags because you are new, and doesn't it look better. I think that formula (999 − 100) + 1 will correctly find three‑digit numbers, but it is difficult to understand. I suggest you add a // comment explaining that formula
The () around 999 − 100 are unnecessary. Because + and − both associate to the left you can never get 999 − 101 from that formula.
It makes no difference to the execution where you declare the random number, but I suggest you shorten that loop. Start with
for (int i = 0; i < 1; i++)…
Note the spacing and also start with 0. The < operator is usually easier to understand than <= and you can use a very similar format for the for loops with arrays. And you can be confident that it will never throw an out of bounds exception.
As SF says, you need to declare sum; it needs an initial value because local variables don't default to 0.
If you only run the loop once, it will be easy to verify the addition by looking at the output. Then try 2 iterations and then go back to 100.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kris French wrote:I understand how vast java is, but as of right now we have only studied if/else/switch statements, so naturally I had no idea how to begin this project. After doing a little homework I was able to get the 100, 3 digit numbers generated...


First: Welcome to JavaRanch, Kris.

Second: Congratulations on getting this far; and especially on discovering the Random class. Many beginners miss it out.

...but I cannot add then together. I have tried a few things that I found online, but either I didn't understand them or I could not get things to work out. Can someone please help give a nudge (or at my level, a violent shove) in the right direction. I appreciate the help.


Well, you've correctly declared a 'sum' field, but you then don't do anything with it. You're incredibly close, and I don't want to just hand you the answer, because you'll be much happier if you work it out for yourself.
So: What do YOU think you should do with it?

Also, a little tip for you:

for(int i = 0; i < 100; i++) { ...

will also perform a loop 100 times, and you'll find it a much more useful way to write it, because Java indexes start at 0.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Fahlbusch wrote:you are declaring a new int each time you generate a random number - you should move the declaration out of the loop and only set the value in the loop.


I agree with Pawel on this one. I think Kris's code is perfect in that respect.

Furthermore, the declaration inside the loop does not declare a new variable each time it iterates - even though it admittedly looks like it does.

Winston
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help; I learned a couple of things from that small bit of code. I am still stuck though. I have a feeling it is something simple that I'm missing. What I have now when the program runs it will still output the 100 numbers, but at the end '4950' shows up each time. I'm starting to think I have no idea what I am doing. Again, thanks for all the help.


 
Campbell Ritchie
Marshal
Posts: 80775
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you had followed my suggestion about running the loop once you would probably have worked the problem out sooner.

Look what you are adding to sum.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell. I re-read your suggestion, and after scratching my head for a bit, I finally understood what you meant by running the loop once. You were right; once I did that it made getting to the answer quickly. Again thank you to all who helped. I can be pretty dense sometimes. Here is the end result:

 
Campbell Ritchie
Marshal
Posts: 80775
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done

Maybe better to print sum after the end of the loop.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good call; I had the print after the loop originally. Thanks!
 
Campbell Ritchie
Marshal
Posts: 80775
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Youi're welcome

Minor nit‑pick‑y point. The indentation in that loop is inconsistent.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's already been fixed. ;)
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kris French wrote:It's already been fixed. ;)


Well done Kris. And be proud that you did it all yourself. That's how you learn; and it's why we don't like to just hand out answers here.

Winston
 
Campbell Ritchie
Marshal
Posts: 80775
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kris French wrote:. . . I can be pretty dense sometimes. . . .

Not at all. Everybody else makes that sort of mistake, too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic