View Full Version : Wrong output from salesReport, array not filling properly.

03-17-2012, 05:04 AM
I need to print out the percentages of total sales and total expenses for each salesperson. For some reason I am only getting the last salespersons name and percentages. Also before my list starts, it outputs all names and the last persons percentages.

import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;

public class SalesReport {
String name; // Used for reading salesperson's name
int sales; // Used for reading sales values
float expenses; // Used for reading expenses values
int salesTotal;
float expenseTotal;
// Class constants
static String A = "\n|--------------------------------------------------------------|";
static String B = "\n| SALES REPORT |";
static String C = "\n| SALESPERSON | Total Sales | Total Expenses |";
static String D = "| |";

// Direct constructor
public SalesReport(String newName, int newSales, float newExpenses,
int newSalesTotal, float newExpenseTotal) {
name = newName;
sales = newSales;
expenses = newExpenses;
salesTotal = newSalesTotal;
expenseTotal = newExpenseTotal;

// Default constructor
public SalesReport() {
name = " ";
sales = 0;
expenses = 0;
salesTotal = 0;
expenseTotal = 0;

// Scanner based constructor
public SalesReport(Scanner inFile) {
String word = " ";
List<String> reportNames = new ArrayList<String>();
// Gather names
while (inFile.hasNext()) { // Loops for text only values
word = inFile.nextLine(); // Collects all name values
reportNames.add(word); // places file names into array
String nameArray[] = new String[reportNames.size()];
reportNames.toArray(nameArray); // assign names to array positions
for (int i = 0; i < nameArray.length; i++)
name = nameArray[i];

// Gather sales and expenses
int intgr = 0;
float expn = 0;
List<Integer> reportSales = new ArrayList<Integer>();
while (inFile.hasNextInt()) { // Loops for integer values
intgr = inFile.nextInt(); // Collects all sales integer values
Integer intArray[] = new Integer[reportSales.size()];
for (int j = 0; j < intArray.length; j++)
sales = intArray[j];
salesTotal += sales;

List<Float> reportExpenses = new ArrayList<Float>();
expn = inFile.nextFloat(); // Collects all expenses float values
Float floatArray[] = new Float[reportExpenses.size()];
for (int k = 0; k < floatArray.length; k++)
expenses = floatArray[k];
expenseTotal += expenses;
System.out.println(name+" ");
System.out.println(salesTotal+" "+expenseTotal+'\n');

// Observer methods
public String getName() {
return name; }
public int getSales() {
return sales; }
public float getExpenses() {
return expenses; }
public int getSalesTotal() {
return salesTotal; }
public float getExpenseTotal() {
return expenseTotal; }

// Isolate salespersons
public void SalesPersons(String names) {
names = getName();
System.out.println("These are sales persons: \n"+names);
// Sum all sales amounts
public void SalesTotals(int sumSales) {
sumSales = getSalesTotal();
System.out.println("This is sales total: \n"+sumSales);
// Sum all expense amounts
public void ExpenseTotals(float sumExpenses) {
sumExpenses = getExpenseTotal();
System.out.println("This is expenses total: \n"+sumExpenses);
// Calculate sales percentages
public void SalesPercent(float salesPer) {
salesPer = getSales()*100/(float)getSalesTotal();
System.out.print(" "+salesPer+"% ");
// Calculate expense percentages
public void ExpensePercent(float expensesPer) {
expensesPer = getExpenses()*100/getExpenseTotal();
System.out.print(" "+expensesPer+"% |"+A);
// Print method
public void print() {
//SalesPersons(" ");
System.out.println(D+" "+getSalesTotal()+" "+getExpenseTotal()+A);
System.out.print("| "+getName()+" ");

// Begin execution here - the driver
public static void main(String[] args) throws IOException {
// Sets scanner to read file
Scanner inFile = new Scanner(new FileReader("sales.txt"));
SalesReport out = new SalesReport(inFile);


The text file (sales.txt) shows:

Smith Kevin
80475 3966.27
Medina Norelis
71040 5677.21
Bailey Van
28305 11276.65
Keepes Karen
58830 7388.15
Keepes Ron
64935 6532.68
Carey Harry
34410 10810.03
Hairy Mata
40515 9954.56
Hurry Mata
52725 8243.62
Hurry Caine
46620 9099.09
Petz Edz
77145 4821.74

03-17-2012, 07:42 PM
The ultimate problem here is that there is no collection of the names or totals. All you have is the last item in the list.
The constructor for creating the totals is mostly correct, you do need to chomp off the trailing linefeeds from the scanner though. When you do non-string > non-string, etc, then try to retrieve string, the results is that of the linefeeds left by the scanner when fetching the numbers. A simple call to nextLine() after you are complete with the numbers removes that excess line feed.

Personally, I'd write a new class to contain each seller and their totals. Then all you need is a collection of that type, and you can draw the totals (either statically from that new class summing up a collection, or from the reporting class which iterates the existing ones). Then its easy to construct too, with something like this:

public SalesReport(Scanner in)
while (in != null && in.hasNext())
this.sellersCollection.add(new SellerInformation(in.nextLine(), in.nextDouble(), in.nextDouble());

The other pro to this is you can now use a Comparator to sort the sellersCollection.

03-19-2012, 12:00 AM
have you tried using asp cos i herd that php is to complicated

03-19-2012, 12:11 AM
have you tried using asp cos i herd that php is to complicated

PHP's not complicated, but I fail to see any relevance to the question. This is Java, not PHP or C#.