Java programming: write a program that creates a histogram that allows you to vi
ID: 3825181 • Letter: J
Question
Java programming:
Explanation / Answer
Let this be our text file
1
2
3
4
5
6
14
12
13
16
26
24
54
35
36
45
46
56
58
69
69
79
79
91
Output:
1-10 ******
11-20 ****
21-30 **
31-40 **
41-50 **
51-60 ***
61-70 **
71-80 **
81-90
91-100 *
Code:
package histogram;
import java.io.*;
import java.util.Scanner;
public class Histogram
{
public static void main(String[] args) throws IOException
{
//let a b c ----- j will store count of each range
int data;
int[] ar={0,0,0,0,0,0,0,0,0,0};
try
{
Scanner in = new Scanner(new File("/Users/hitansh/Desktop/input.txt"));
while(in.hasNextInt())
{
data=in.nextInt();
ar[(data-1)/10]++;
}
}
finally
{
}
int k;
//lets print the graph
System.out.print("1-10 ");
for(k=0;k<ar[0];k++)
System.out.print('*');
System.out.println();
System.out.print("11-20 ");
for(k=0;k<ar[1];k++)
System.out.print('*');
System.out.println();
System.out.print("21-30 ");
for(k=0;k<ar[2];k++)
System.out.print('*');
System.out.println();
System.out.print("31-40 ");
for(k=0;k<ar[3];k++)
System.out.print('*');
System.out.println();
System.out.print("41-50 ");
for(k=0;k<ar[4];k++)
System.out.print('*');
System.out.println();
System.out.print("51-60 ");
for(k=0;k<ar[5];k++)
System.out.print('*');
System.out.println();
System.out.print("61-70 ");
for(k=0;k<ar[6];k++)
System.out.print('*');
System.out.println();
System.out.print("71-80 ");
for(k=0;k<ar[7];k++)
System.out.print('*');
System.out.println();
System.out.print("81-90 ");
for(k=0;k<ar[8];k++)
System.out.print('*');
System.out.println();
System.out.print("91-100 ");
for(k=0;k<ar[9];k++)
System.out.print('*');
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.