• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Bar chart program

 
Greenhorn
Posts: 4
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a college student tasked with building a simple program to where I can input 5 random numbers between 1-30 and have the program print out the amount of stars equal to number inputted. I am struggling because the output of stars only works for the last number inputted into the program and not for all inputted numbers. I am stuck trying to figure out what I am missing to make all inputted numbers output stars at the very end.  Any help is greatly appreciated as I have searched many places before posting this issue.

 
Sheriff
Posts: 13676
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kyle Greer wrote:


That's not what that code does. That's actually setting the variable number to the value of the mathematical expression (1 - 30) -- I'll leave it to you to do the subtraction.
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that sort of program is annoying, but it is useful for practising for loops.
Always use the conventional form of a for loop:-Declare the loop variable inside the loop if at all possible (not in line 8). Start with 0 and use < rather than <= That is the form people are used to seeing, and they will understand it better. Also format your code. The indentation in your post is very inconsistent, and the lack of spaces between tokens and lack of {} makes your code harder to read. And remember who it is who will have the most difficulty reading your code: yourself.
I suggest you simply write the instruction, “Please enter five numbers: ”. Then work out what you are doing with those numbers. What is number's initial value in line 9? Just as well you are changing it. Then what is the value of number when you reach line 19? How many numbers are you entering? How many of them are actually used? Is number any good as a variable name?
 
Kyle Greer
Greenhorn
Posts: 4
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Kyle Greer wrote:


That's not what that code does. That's actually setting the variable number to the value of the mathematical expression (1 - 30) -- I'll leave it to you to do the subtraction.



After I removed that line it gave me an error that said variable number might not be initialized. From my reading, variables are defaulted to 0, so I wrote and to try both ways and it initialized it but was still not able to print out any other inputs but the last one entered into the program.
 
Junilu Lacar
Sheriff
Posts: 13676
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kyle Greer wrote:try both ways and it initialized it but was still not able to print out any other inputs but the last one entered into the program.


That line isn't the reason though. Just wanted to point out that what your comment said was happening wasn't true, so you realized what it actually does.

Your logic is wrong. The sequence of commands you gave does not match what you want it to do. I would advise you to solve this in a step-wise manner rather than try to write a bunch of code to solve the problem all at once.

Here are the steps I'd recommend:

1. Write a very simple program that uses a loop to print out the numbers 1 to 5. (make this work first before moving on to #2)
2. Change the program slightly so that instead of just printing the numbers 1 to 5, it's asking the user to input a number, then it displays the number entered. This should happen 5 times. (make this work first before moving on to #3)
3. Change the program slightly again so that instead of displaying the number entered, it prints out "*" that many times. (make this work first before adding checks for valid number 1-30)

If you try to work up to the solution by writing smaller, simpler programs that kind of but not quite do what you eventually want it to do, then you will better understand the sequence and logic and how to structure it such that it does do what you want it to do.

 
Liar, liar, pants on fire! refreshing plug:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!