Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rather than this code using a txt document for input, how do I change it to allo

ID: 3683770 • Letter: R

Question

Rather than this code using a txt document for input, how do I change it to allow for the user to input? I know it uses a Scanner class to create an object, but I do not know which code to put in, which code to take out, and where it is. In Java please, thank you

import java.io.*;
import java.text.DecimalFormat;

/**
* Chapter 7
* Programming Challenge 7: Quarterly Sales Statistics
*/

public class SalesStats
{
public static void main(String[] args) throws IOException
{
// Create a Divisions object.
Divisions d = new Divisions();

// Read the values from the file into d.
readValues(d);
  
// Display the values in d.
displaySales(d);
  
// Display the quarterly increase or decrease.
displayQtrDifference(d);
  
// Display the total sales by quarter.
displaySalesByQtr(d);
  
// Display the quarterly increase or decrease
// by quarter.
displayCompanyQtrDifference(d);
  
// Display the average sales per quarter.
displayAverageSalesPerQtr(d);
  
// Display the division with the highest sales
// by quarter.
displayHighestDivisionPerQtr(d);
}

/**
* readValues method.
* Reads the values from Sales.txt into the object.
*/

public static void readValues(Divisions d) throws IOException
{
String input;
FileReader freader = new FileReader("Sales.txt");
BufferedReader inFile = new BufferedReader(freader);
  
for (int div = 1; div <= 6; div++)
{
for (int qtr = 1; qtr <= 4; qtr++)
{
input = inFile.readLine();
d.setSales(div, qtr, Double.parseDouble(input));
}
}
}

/**
* displayValues method.
* Displays a list of sales figures by division.
*/

public static void displaySales(Divisions d)
{
System.out.println("SALES AMOUNTS BY DIVISION");
System.out.println("=========================");
for (int div = 1; div <= 6; div++)
{
System.out.println("DIVISION " + div);
for (int qtr = 1; qtr <= 4; qtr++)
{
System.out.println("Quarter " + qtr + ": $" +
d.getSales(div, qtr));
}
}
System.out.println();
}

/**
* displayQtrDifference method.
* Displays the quareterly increase or decrease for each
* division, starting at quarter 2.
*/

public static void displayQtrDifference(Divisions d)
{
System.out.println("QUARTERLY INCREASE/DECREASE BY DIVISION");
System.out.println("=======================================");
for (int div = 1; div <= 6; div++)
{
System.out.println("DIVISION " + div);
for (int qtr = 2; qtr <= 4; qtr++)
{
System.out.println("Quarter " + qtr +
"'s increase: $" +
d.getQuarterlyIncrease(div, qtr));
}
}
System.out.println();
}

/**
* displaySalesByQtr method.
* Displays total sales by quarter.
*/

public static void displaySalesByQtr(Divisions d)
{
DecimalFormat dollar = new DecimalFormat("#,##0.00");
  
System.out.println("SALES AMOUNTS BY QUARTER");
System.out.println("========================");
for (int qtr = 1; qtr <= 4; qtr++)
{
System.out.println("Quarter " + qtr + ": $" +
dollar.format(d.totalQuarterSales(qtr)));
}
System.out.println();
}

/**
* displayCompanyQtrDifference method.
* Displays the quareterly increase or decrease for the
* company, starting at quarter 2.
*/

public static void displayCompanyQtrDifference(Divisions d)
{
double increase;
  
DecimalFormat dollar = new DecimalFormat("#,##0.00");
  
System.out.println("QUARTERLY INCREASE/DECREASE FOR THE COMPANY");
System.out.println("===========================================");
for (int qtr = 2; qtr <= 4; qtr++)
{
increase = 0.0;

for (int div = 1; div <= 6; div++)
{
increase += d.getQuarterlyIncrease(div, qtr);
}

System.out.println("Quarter " + qtr + ": $" +
dollar.format(increase));
}
System.out.println();
}

/**
* displayAverageSalesPerQuarter method.
* Displays the average sales for all divisions per quarter.
*/

public static void displayAverageSalesPerQtr(Divisions d)
{
double total, average;
DecimalFormat dollar = new DecimalFormat("#,##0.00");
  
System.out.println("AVERAGE SALES PER QUARTER");
System.out.println("=========================");
for (int qtr = 1; qtr <= 4; qtr++)
{
total = 0.0;
for (int div = 1; div <= 6; div++)
{
total += d.getSales(div, qtr);
}
average = total / 6.0;
System.out.println("Quarter " + qtr + ": $" +
dollar.format(average));
}
System.out.println();
}

/**
* displayHighestDivisionPerQtr method.
* Displays the division with the highest sales per quarter.
*/

public static void displayHighestDivisionPerQtr(Divisions d)
{
System.out.println("DIVISION WITH THE HIGHEST SALES PER QUARTER");
System.out.println("===========================================");
  
for (int qtr = 1; qtr <= 4; qtr++)
{
int highDiv = 1;
double highSales = d.getSales(1, qtr); // Get sales for div 1, this qtr
for (int div = 2; div <= 6; div++)
{
double salesQtr = d.getSales(div, qtr);
if (salesQtr > highSales)
{
highSales = salesQtr;
highDiv = div;
}
}
System.out.println("Highest division for quarter " + qtr +
" is division " + highDiv);
}
System.out.println();

}
}

Explanation / Answer

Include below line at the beginning of your program.

import java.util.Scanner;

You're taking input in main function. So, you sould define a scanner at the beginning of main function. So your main function should be like below

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

Rest of the code goes here

}

Now you are ready to scan using Scanner in

You're using below line to ready value from file into the object d

readValues(d);

Instead of that you can call like below to ready value from keyboard

So if you have a variable called id of type int in object d of type Divisions you can assign the value of variable salary as below

d.id= in.nextInt();