• 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

Histogram

 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone another question. I am honestly just lost on this one, I am trying to make a histogram using a couple different files to use a txt file with a list of a 100 numbers. We were given a csc file to use to be able to import that file. For each of the hundred numbers depending on what number it is a * is supposed to be printed. I am stuck on how i am supposed to get the array to work in my code and to display the *. Any help is greatly appreciated. Some of the code is commented out because I am unsure if I need all of that or not. Thanks again.


the infile is the txt for the 100 integers
this is my code
 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please copy the full text of the compiler's error messages and paste it here so we can see what the problems are.

For each of the hundred numbers depending on what number it is a *  


Does that mean a 5 should produce: *****
and a 3 ***

Is there a max number allowed?

Sounds like you need a method that can be called with the desired number of *s to print.
 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry always forgot to put the error codes

C:\Users\Home\Documents\Histogram\Histogram7.java:17: error: cannot find symbol
if (numbers[x] >= 1 && numbers[x] <= 10)
           ^
 symbol:   variable x
 location: class Histogram7
C:\Users\Home\Documents\Histogram\Histogram7.java:17: error: cannot find symbol
if (numbers[x] >= 1 && numbers[x] <= 10)
                              ^
 symbol:   variable x
 location: class Histogram7
C:\Users\Home\Documents\Histogram\Histogram7.java:58: error: cannot find symbol
System.out.println(" 1 - 10  | " + numbers[x]);
                                          ^
 symbol:   variable x
 location: class Histogram7
3 errors

Tool completed with exit code 1
 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, just one * for each number code is supposed to look like this:
1 – 10  | *****
11 – 20  | **
21 – 30  | *******************
31 – 40  |
41 – 50  | ***
51 – 60  | ********
61 – 70  | **
71 – 80  | *****
81 – 90  | *******
91 – 100 | *********
so 5 numbers were between 1-10 2 between 11-20 and so on
 
Norm Radder
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

C:\Users\Home\Documents\Histogram\Histogram7.java:17: error: cannot find symbol
if (numbers[x] >= 1 && numbers[x] <= 10)
           ^
 symbol:   variable x  


The compiler can not find a definition for the variable x.  Since the code is working with an array, you should have a for loop with a variable to iterate through the array's elements.

one * for each number code is supposed to look like this:  


That will require a loop to count all the numbers in each range of numbers before trying to print the *s
 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is new code, got it to print the * but how do i get it to line up. Should i do something along the lines of *++?
 
Norm Radder
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how do i get it to line up


You need to count all the numbers in each range,  saving the counts in an array and then use a loop to go through the array of counts and print the *s for each of the counts on a separate line.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should try to work through the process using pen and paper, as though you are the computer. You need to write down what your different variables would look like then do the work that you think the computer should do.

For example, to represent what an array would look like on paper, you would write something like this:

Then, take your list of numbers that are in your input file and manually do whatever it is you want the computer to do. When you do it yourself and understand the process, then you'll have a better idea of how you'll want the computer to do it.
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To go back to your first post. There are other things in it. You don't appear to be closing the Scanner. That is hazardous because the file might remain open for ever. You shou‍ld always close Scanners, readers, streams writers, etc, pointing to a file. The best way to do that is with try with resources (Java7+).
Don't catch the two unchecked exceptions on lines 18 and 28. If they occur (which they won't), the file is corrupt and you will have to terminate the program regardless. Avoid System.exit, which can be dangerously vicious terminating things. Consider declaring that the constructor throws the FNF exception and letting some other code deal with it. Look at this loop:-Why are you using a file input stream? Use a FileReader for a text file. In which case inputStream becomes a bad name for the Scanner; try fileScanner or numberScanner instead.
Don't use number literals if you can possibly avoid it. Write the heading for the for loop like this:-
for (int i = 0; i < myArray.length; i++) ...
Always start with that format which is certain to run without exceptions, unless you use i + 1 or i − 1 or similar, in which case you will have to change the loop header slightly.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then loop to plot histogram.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also greatly simplify the logic for counting number occurences by recognizing some patterns that lend themselves to calculation.


If a number is evenly divisible by 10:

N = 10, increment count[0]
N = 20, increment count[1]
N = 30, increment count[2]
N = 40, increment count[3]
N = 50, increment count[4]
...
N = 90, increment count[8]
N = 100, increment count[9]

If a number is NOT evenly divisible by 10:

N = 1..9, increment count[0]
N = 11..19, increment count[1]
N = 21..29, increment count[2]
...
N = 81..99, increment count[8]
N = 91..99, increment count[9]
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get that by n / 10.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can get that by n / 10.


If that's all you do though, you'd be chalking up counts for 10, 20, 30, ..., 100 in the wrong column.

Edit: Although I suppose with a little bit of trickeration, you could get away with just using (something) / 10.  The code would be a little non-intuitive and would warrant a comment as to why the trickery works.
 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to all who helped, sorry for the late reply but it got figured out. All the help is greatly appreciated!
 
I found a beautiful pie. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic