Java Question

Status
Not open for further replies.

Spartan 051

Well-Known Member
Ive been writing this for a java class and ive ran into a little snag.

Ive been stupid at the last step of the assignment which is:

At the bottom of the report, give the total of all the numbers in each
numeric column and give the maximum value found in each numeric
column.

So any suggestions would be greatly appreciated

<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>
import java.io.*;

import java.text.*;

public class GellersonPrefab{
//**********************************************
// The Class that compiles and runs the program
//**********************************************

String department, supplier;
Double sale;

String companyList[] = new String[200];
String departmentList[]= new String[4];

int companyFound = 0;
int departmentFound = 0;

double departmentArray[][] = new double[200][4];
// double departmentArrayTotal[][] = new double[2][4];


public static void main (String args[])

{(new GellersonPrefab()).printReport();}

private void printReport()
{
//***************************************
// Prints the MISC.text files and the header
// and column headings
//***************************************

DecimalFormat formatDecimal = new DecimalFormat("0.00");

System.out.println(String.format("\n"+"%60s", "Gellerson Prefab"+"\n"));
System.out.println(String.format("%5s%-32s%-15s%-15s%-15s%9s%12s\n",
"","SUPPLIER", "Systems","Roofing","Furnishing","Structure","SALES"));

try
{
BufferedReader readDataFile = new BufferedReader(new FileReader("/Users/MISC.txt"));
String dataFromFile;

while((dataFromFile = readDataFile.readLine()) != null)
{
processSupplier(dataFromFile);
}
readDataFile.close();
} // end try

catch (IOException e)
{
System.err.print("error " + e);
}// End catch

//****************************************
// Creates the 2d array called departmentArray
// and prints out the departments
//******************************************

for( int departmentRow= 0; departmentRow<companyFound; departmentRow++)
{
double departmentTotal = 0;

System.out.print(companyList[departmentRow]);

for (int departmentCol = 0; departmentCol<4; departmentCol++)
{
departmentTotal += departmentArray [departmentRow][departmentCol];
System.out.printf("%15.2f",( departmentArray [departmentRow][departmentCol]));

}// end for loop

System.out.printf("%15s",formatDecimal.format(departmentTotal)+ "\n");

}// end for loop

System.out.println();

}// End printReport method

private void processSupplier (String readData)
{
//****************************************
// Reads the MISC.test file and creates companyLocation and
// departmentLocation integers. It also compiles sales
//****************************************

supplier = readData.substring(0,30);
department = readData.substring(30,40);
sale = Double.parseDouble(readData.substring(40,47));

int companyLocation = linearSearch (companyList, supplier, companyFound);
int departmentLocation = linearSearch (departmentList, department, departmentFound);

departmentArray[companyLocation][departmentLocation]+=sale;


//******************************************
// Two if statements that compile companies and departments
//******************************************

if(companyFound == companyLocation)
{
companyList[companyFound] = supplier;
companyFound++;

} // end if


if(departmentFound == departmentLocation)
{
departmentList[departmentFound] = department;
departmentFound++;

}// end if

} //end method processSupplier

static int linearSearch (String [] list, String search, int listLength)

// *****************************************************************
// This method looks for search in the list which currently has
// listLength elements. If fournd, the position in list is returned.
// If not, listLength is returned.
// *****************************************************************

{

int position = 0;

while (position < listLength && !list[position].equals(search))

position++;

return position;

} // end search

}</div>


Compiled code:
java.jpg
 
Last edited by a moderator:
okay, even though java sucks, it's probably not your language of choice, so I'll help ;)

but first, documentation. seriously, nobody needs // end try // end for loop in there. Some info on what the loop prints would be nice, though. But I'm guessing, that companyList is in fact the data in the supplier column.

departmentList contains "Systems","Roofing","Furnishing","Structure"? and departmentArray contains the numbers?

(I'm not sure how useful the other stuff is, but something tells me you should have a look at hashtables. oh well.)

So you need two more arrays:
<div class='codetop'>CODE</div><div class='codemain'>double departmentSum[] = new double[4];
double departmentMax[] = new double[4];</div>

then, in your column loop, you'd do this:
<div class='codetop'>CODE</div><div class='codemain'>// print the columns, count the row total, count the column total
for (int departmentCol = 0; departmentCol<4; departmentCol++)
{
    // make it easier to read;)
    double temp = departmentArray[departmentRow][departmentCol];
    
    departmentTotal += temp; // this is the row total
    
    System.out.printf("%15.2f", temp); // output column value
    
    departmentSum[departmentCol] += temp; // this is the column total
    
    if (departmentMax[departmentCol] < temp) // if the max value for this column is smaller...
    {
        departmentMax[departmentCol] = temp;
    }
}</div>

I'd recommend different variable names, but that's your choice.

then, you need to output the stuff, before you exit the printReport method, eg.
<div class='codetop'>CODE</div><div class='codemain'>System.out.println();

// output department total/sum

System.out.printf(String.format("%5s%-32s%", "","DEPARTMENT TOTAL"));

for (int departmentCol = 0; departmentCol<4; departmentCol++)
{
    System.out.printf("%15.2f", departmentSum[departmentCol]);
}

System.out.println();

// output department max value

System.out.printf(String.format("%5s%-32s%", "","DEPARTMENT MAX"));

for (int departmentCol = 0; departmentCol<4; departmentCol++)
{
    System.out.printf("%15.2f", departmentMax[departmentCol]);
}

System.out.println();</div>


Hope that helps!
 
Status
Not open for further replies.
Back
Top