This is my code, I know I need to enter a Scanner to allow for input but don\'t
ID: 3683779 • Letter: T
Question
This is my code, I know I need to enter a Scanner to allow for input but don't know what code, where to insert it, and which to take out. I am wanting to use input instead of a text document.
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
->Divisions class is not found in your program and so I am not getting the program goal.
->You want to Use Scannar class to read the data so First create the object either globally or in the method where you want to read data.
Scannar sc=new Scannar();
->and where ever you want to read data you can use folloing methods.
->and it provides the methods to get Integer values or String values
sc.nextInt()-->method is to readnext int from the user.
sc.nextLine()-->To read the string data.
example : System.out.println("Enter your Id");
int id=Integer.parseInt(sc.nextLine()); or int id=sc.nextInt();
-->If you want to convert string to integer data you can do as follows
int a=Integer.parseInt(sc.nextLine());
-----> and you can create as many Scannar objects as you want locally.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.