JAVA BEGINNER PROGRAMMING CHANGE THIS OUTP UT TO PRINT TEXT FILE.... import java
ID: 3680067 • Letter: J
Question
JAVA BEGINNER PROGRAMMING
CHANGE THIS OUTPUT TO PRINT TEXT FILE....
import java.util.Scanner;
public class SalesByQuarter
{
public static void main(String[] args)
{
int divs = 6;
int qtrs = 4;
double totalSales = 0.0;
double errorCheck;
double[][] sales = new double[divs][qtrs];
Scanner keyboard = new Scanner(System.in);
System.out.println("This program will calculate the " +
"total sales of");
System.out.println("all the company's divisions. " +
"Enter the following sales data:");
System.out.println();
for (int div = 0; div < divs; div++)
{
for (int qtr = 0; qtr < qtrs; qtr++)
{
System.out.printf("Division %d, Quarter %d: $",
(div + 1), (qtr + 1));
errorCheck = keyboard.nextDouble();
while(errorCheck<0) {
System.out.println("Positive numbers only please!");
System.out.printf("Division %d, Quarter %d: $",
(div + 1), (qtr + 1));
errorCheck = keyboard.nextDouble();
}
sales[div][qtr] = errorCheck;
}
System.out.println();
}
for (int div = 0; div < divs; div++)
{
for (int qtr = 0; qtr < qtrs; qtr++)
{
totalSales += sales[div][qtr];
}
}
double[] divsales = new double[divs];
for (int div = 0; div < divs; div++)
{
for (int qtr = 0; qtr < qtrs; qtr++)
{
divsales[div] += sales[div][qtr];
}
System.out.printf("Division %d sales: $%,.2f ", (div + 1), divsales[div]);
}
System.out.println();
for (int div = 1; div < divs; div++)
{
if ((divsales[div] - divsales[div - 1]) > 0)
{
System.out.printf("Division %d increase over its previous quarter: $%,.2f ", (div + 1), divsales[div] - divsales[(div - 1)]);
}
else if ((divsales[div] - divsales[div - 1]) < 0)
{
System.out.printf("Division %d decrease from its previous quarter: $%,.2f ", (div + 1), divsales[div] - divsales[(div - 1)]);
}
else
{
System.out.printf("Division %d had no increase or decrease from its previous quarter. ", (div + 1));
}
}
System.out.println();
double[] qtrsales = new double[qtrs];
for (int div = 0; div < divs; div++)
{
for (int qtr = 0; qtr < qtrs; qtr++)
{
qtrsales[qtr] += sales[div][qtr];
}
}
for (int qtr = 0; qtr < qtrs; qtr++)
{
System.out.printf("Total sales for quarter: %d: $%,.2f ", (qtr + 1), qtrsales[qtr]);
}
System.out.println();
for (int qtr = 0; qtr < qtrs; qtr++)
{
System.out.printf(" Average sale for quarter: %d: $%,.2f ", (qtr + 1), (qtrsales[qtr] / divs));
}
System.out.println();
int highestDiv = 0;
int[] highestDivi = new int[qtrs];
for (int qtr = 0; qtr < qtrs; qtr++)
{
highestDiv = 0;
for (int div = 0; div < (divs - 1); div++)
{
if (sales[highestDiv][qtr] < sales[(div + 1)][qtr])
{
highestDiv = (div + 1);
}
}
highestDivi[qtr] = highestDiv;
}
for (int qtr = 0; qtr < qtrs; qtr++)
{
System.out.printf("Division %d had the highest sales in quarter %d ", qtr + 1, highestDivi[qtr] + 1);
}
}
}
Explanation / Answer
import java.io.*;
public class SimplePrinting {
public static String strLine;
public static String strLine1="";
public static void main(String[] args) {
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("c: est.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
strLine1 = strLine1 + strLine;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
try {
FileWriter out = new FileWriter("lpt1");
out.write(strLine1);
out.write(0x0D); // CR
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
note- the above code is to print a text file,
and there are many syntax errors in the given question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.