Using the input file I have already downloaded (sprocketorders.txt), write a Jav
ID: 3555763 • Letter: U
Question
Using the input file I have already downloaded (sprocketorders.txt), write a Java program to summarize the data from the input file and output a report. The report should appear in the output window when the program runs. I would like to use switch structure to solve this but if not, using a series of if-then statements would suffice.
The output report needs to look like this:
Spacely Sprockets
Taking Sprockets into the Future
Sales Summary Report
Sprocket Number Total Quantity Sold
1 90
2 155
3 50
4 300
5 100
The data in the txt file appears as such:
3 50
2 20
2 100
5 15
1 90
5 85
4 300
2 35
3 100
Explanation / Answer
import java.util.*;
import java.io.*;
public class report
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(new File("sprocketorders.txt"));
int[] array = new int[5];
for(int i=0; i<array.length; i++)
array[i] = 0;
while(in.hasNext())
{
int num = in.nextInt();
int Qty = in.nextInt();
array[num-1] += Qty;
}
System.out.println("Spacely Sprockets");
System.out.println("Taking Sprockets into the Future");
System.out.println("Sales Summary Report");
System.out.println("Sprocket Number Total Quantity Sold");
for(int i=0; i<array.length; i++)
System.out.println(" "+(i+1) + " " + array[i]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.