# Histogram java problem

Printable View

• April 4th, 2014, 08:58 AM
supernowlin
Histogram java problem
My assignment is

8.3: Histogram
Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values . The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.

No input prompt
Terminate input by typing CTRL/Z (two keys typed at the same time) on a separate input line (use CTRL/D on Linux/UNIX systems)
Use hasNextInt() to terminate your input
Format as below (slightly different from the text example)
Z:\dbraffitt\Week10> javac Histogram.java
Z:\dbraffitt\Week10> java Histogram
10 10 10 10 10 20 20 20 20 20 20
20 25 35 45 55 65 75 85 95
ctrl/z
1 - 10 | *****
11 - 20 | *******
21 - 30 | *
31 - 40 | *
41 - 50 | *
51 - 60 | *
61 - 70 | *
71 - 80 | *
81 - 90 | *
91 - 100 | *

What I can not figure out is how to use hasNextInt() to terminate the loop. How to not have an input prompt. How to use ctrl z to terminate the input. Or to make it where it doesn't involve range values like -1. The problem is due today at 8pm and I really need help.

Code :

```import java.util.Scanner;   public class Histogram { public static void main (String[] args) { Scanner scan = new Scanner (System.in);   int [] nums = new int[101];   System.out.println("Numbers between 1 and 100: "); int num = scan.nextInt();   int varb = 0;   while (num !=0) { nums[num]++; num = scan.nextInt(); }   for (int count = 1; count <= 100;count+=10) { System.out.print (count + " - " + (varb+=10) + " | " ); } } }```
• April 4th, 2014, 09:16 AM
Norm
Re: Histogram java problem
Also posted at: Histogram java problem
• April 4th, 2014, 09:21 AM
GregBrannon
Re: Histogram java problem
What happens if you try while ( scan.hasNextInt() )?
• April 4th, 2014, 09:44 AM
supernowlin
Re: Histogram java problem
Im actually really bad at programming. I haven't tried it yet. Im not sure how to incorporate it but ill give it a go.

--- Update ---

and Norm its supposed to keep asking for numbers until you enter crtl z
• April 4th, 2014, 11:29 AM
GregBrannon
Re: Histogram java problem
"Really bad at programming" is what most (all?) people are before they start studying and trying things. However, most who become proficient probably never thought of themselves as really bad at it. You shouldn't either.