Question

Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts ho
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:-

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Histogram {

  

public static void main(String[] args) throws FileNotFoundException {

  

Scanner input = new Scanner(System.in);

System.out.print("Enter filename: ");

String fileName = input.next();

  

// opening file

Scanner sc = new Scanner(new File(fileName));

  

int countArr[] = new int[11];

  

int num;

  

while(sc.hasNextInt()){

  

num = sc.nextInt();

  

if(num >=0 && num <=9)

countArr[0]++;

else if(num>=10 && num <=19)

countArr[1]++;

else if(num>=20 && num <=29)

countArr[2]++;

else if(num>=30 && num <=39)

countArr[3]++;

else if(num>=40 && num <=49)

countArr[4]++;

else if(num>=50 && num <=59)

countArr[5]++;

else if(num>=60 && num <=69)

countArr[6]++;

else if(num>=70 && num <=79)

countArr[7]++;

else if(num>=80 && num <=89)

countArr[8]++;

else if(num>=90 && num <=99)

countArr[9]++;

else // out of range

countArr[10]++;

}

  

sc.close();

input.close();

  

int low = 0, high = 9;

  

System.out.println("range\t\tcount");

for(int i=0; i<10; i++){

System.out.print(low+"-"+high+"\t\t"+countArr[i]+" |");

for(int j=1; j<=countArr[i]; j++)

System.out.print("*");

System.out.println();

low = low+10;

high = high + 10;

}

System.out.print("out of range\t"+countArr[10]+" |");

for(int j=1; j<=countArr[10]; j++)

System.out.print("*");

System.out.println();

}

}

/*

Sample run:

Enter filename: input.txt

range count

0-9 3 |***

10-19 1 |*

20-29 4 |****

30-39 2 |**

40-49 0 |

50-59 0 |

60-69 1 |*

70-79 3 |***

80-89 0 |

90-99 2 |**

out of range 9 |*********

*/

############# input.txt ########

31 145 70 5 27 103 71 140 8 162 98 153 8 109 103 31 145 157 27 90 75 19 23 25 69

Add a comment
Know the answer?
Add Answer to:
Write a program that reads a file containing an arbitrary number of text integers that are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • This needs to be linux compatible in C++ code. Write a program that reads a file...

    This needs to be linux compatible in C++ code. Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167,...

  • C Program Question: Write a program that reads all integers that are in the range of...

    C Program Question: Write a program that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file. Note: The program ignores any number less than 0 or greater than 100. Note: Do not display zero if a...

  • Write a C program that reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • written in c++ Write a program that reads a file consisting of students’ test scores in...

    written in c++ Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data:76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149,...

  • Placing Exception Handlers File ParseInts.java contains a program that does the following: Prompts for and reads...

    Placing Exception Handlers File ParseInts.java contains a program that does the following: Prompts for and reads in a line of input Uses a second Scanner to take the input line one token at a time and parses an integer from each token as it is extracted. Sums the integers. Prints the sum. Save ParseInts to your directory and compile and run it. If you give it the input 10 20 30 40 it should print The sum of the integers...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...

    (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons (;) or pound signs (#) should be ignored. All empty lines must be ignored Lines containing a string between square brackets are sections. Within each section, lines may contain numbers or strings. For each section, calculate the lowest, highest and modal (most common) number, and print it. In each section, also count all non-whitespace characters, and print that. Example input: ; this is a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT