Hey there folks,
So I am working on an assignment for class and I have gotten a considerable amount done thus far. I am having some issues with the final important aspect though. The assignment asks that we read a .txt file with a bunch of records of cars sold including attributes of each car, i.e. model, color, year, etc. We then store each entry into a 5 dimensional array. I have no issue with the part of the code as it seems to be storing fine after testing it. We then have to read another .txt file with a handful of queries. We take these queries and derive a total of the value of the cars sold based upon the query criteria. We are to derive the totals by getting the information from the 5 dimensional array we populate earlier. The issue I am having is I can not seem to get the nested loops to properly sum the total for each query. I can read the query.txt file fine and everything but it seems my logic is screwy in dealing with the data after that. I am wondering if anyone can point me in the right direction or maybe guide my debugging. I do NOT want any exact code that will fix the problem so please do not offer that as help. I am just asking for a fresh, more experienced set of eyes to offer a bit of guidance. Thanks! The code is included at the bottom of the message, it is fairly large and somewhat sloppy and for this I apologize. I am still a beginner yet and I am to stupid to understand the value of organization. If you want the .txt files I have been using to test I will offer those as well.
import java.io.*;
import java.lang.*;
public class Main
{
private TextFileReader inFile, inFile2;
private Members currentRecord,nextRecord;
private QueryFile currentRecord2;
String [] wordList = {"Lemur", "Rhesus", "Chimp", "Orangutan", "Gorilla", "Black", "White", "Red", "Blue", "Green", "Yellow", "Standard", "Automatic", "Air", "NoAir", "2007", "2008", "2009", "2010"};
int attributePosition [] = {1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,5,5,5,5};
int attributeValue [] = {0,1,2,3,4,0,1,2,3,4,5,0,1,0,1,0,1,2,3};
int attribute1, attribute2,attribute3,attribute4,attribute5 = 0;
String [] recordArray = new String [5];
double [][][][][] profit = new double[5][6][4][4][7];
String record, record2, record3, record4= " ";
String [] queryArray = new String[5];
double sum = 0.00;
double totalSum = 0.00;
String queryRecord= "";
int tempApp1 = 0;
int minApp1,minApp2,minApp3,minApp4,minApp5 = 0;
int maxApp1,maxApp2,maxApp3,maxApp4,maxApp5 = 0;
int loopApp1, loopApp2,loopApp3,loopApp4,loopApp5 =0;
public static void main (String args[])
{
(new Main()).populateArray ();
}
private void populateArray ()
/***************************************************************************
*Method that reads the members.txt file and populate's the dues array with
*the appropriate totals for each record category.
****************************************************************************/
{
inFile = new TextFileReader("CARS.txt");
inFile2 = new TextFileReader("query.txt");
nextRecord = new Members (inFile.readLine ());
DisplayHeader();
do
{
currentRecord = nextRecord;
nextRecord = new Members (inFile.readLine ());
recordArray = currentRecord.getModel().replaceAll("\\s+", " ").split("\\s");
for (int i =0; i<recordArray.length; i++){
// System.out.println(recordArray[i]);
}
if (recordArray.length ==6)
try {
sum = Double.valueOf(recordArray[5].trim()).doubleValue();
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
else if(recordArray.length ==5){
sum = Double.valueOf(recordArray[4].trim()).doubleValue();
}
attributeCheck();
} while (inFile.moreData());
}
private void attributeCheck()
{
for (int i = 0; i<wordList.length; i++){
for (int j = 0; j<recordArray.length; j++) {
if (recordArray[j].equalsIgnoreCase(wordList[i]) && i<=4){
attribute1 = attributeValue[i];
}
if (recordArray[j].equalsIgnoreCase(wordList[i])&& i>=5 && i<=10) {
attribute2 = attributeValue[i];
}
if (recordArray[j].equalsIgnoreCase(wordList[i])&& i>=11 && i<=12) {
attribute3 = attributeValue[i];
}
if (recordArray[j].equalsIgnoreCase(wordList[i])&& i>= 13 && i<=14){
attribute4 = attributeValue[i];
}
if (recordArray[j].equalsIgnoreCase(wordList[i]) && i>=15 && i <=19){
attribute5 = attributeValue[i];
}
}
}
profit[attribute1][attribute2][attribute3][attribute4][attribute5] += sum;
// System.out.println( attribute1+". "+ attribute2+". "+attribute3+". "+attribute4+". "+attribute5+". "+ profit[attribute1][attribute2][attribute3][attribute4][attribute5]);
compareQuery();
}
private void compareQuery(){
readChange();
for (int j = 0; j<queryArray.length; j++)
{
for (int i = 0; i<wordList.length; i++)
{
if (wordList[i].equalsIgnoreCase(queryArray[j])){
tempApp1 = i;
}
// System.out.println(queryArray[j]);
System.out.println(tempApp1);
switch (tempApp1){
case 0: case 1: case 2: case 3: case 4:
minApp1 = attributeValue[tempApp1];
maxApp1= attributeValue[tempApp1];
// System.out.println(tempApp1+" "+minApp1+" "+maxApp1+". "+loopApp1);
break;
case 5: case 6: case 7: case 8: case 9: case 10:
minApp2 = attributeValue[tempApp1];
maxApp2= attributeValue[tempApp1];
// System.out.println(tempApp1+" "+minApp2+" "+maxApp2+". "+loopApp2);
break;
case 11: case 12:
minApp3 = attributeValue[tempApp1];
maxApp3= attributeValue[tempApp1];
break;
case 14: case 15:
minApp4 = attributeValue[tempApp1];
maxApp4= attributeValue[tempApp1];
break;
case 16: case 17: case 18: case 19:
minApp5 = attributeValue[tempApp1];
maxApp5= attributeValue[tempApp1];
break;
}
}
}
for (loopApp1 = minApp1; loopApp1<maxApp1; loopApp1++)
for (loopApp2 = minApp2; loopApp2<maxApp2; loopApp2++)
for (loopApp3 = minApp3; loopApp3<maxApp3;loopApp3++)
for (loopApp4 = minApp4; loopApp4<maxApp4; loopApp4++)
for (loopApp5 = minApp5; loopApp5<maxApp5; loopApp5++)
totalSum += profit[loopApp1][loopApp2][loopApp3][loopApp4][loopApp5];
System.out.println("Total Sum: "+maxApp1+". "+maxApp2+". "+maxApp3+". "+maxApp4+ ". "+ maxApp5 +". "+ totalSum);
minApp1 = 0;
maxApp1= 0;
minApp2 = 0;
maxApp2= 0;
minApp3 = 0;
maxApp3= 0;
minApp4 = 0;
maxApp4= 0;
minApp5 = 0;
maxApp5= 0;
tempApp1=0;
totalSum =0;
}
private void readChange()
{
/*******************************************************************************************************************
This method reads the query records in the queries.txt file in order to split each record line and assign the
query request name in order to search the query request later.
******************************************************************************************************************/
currentRecord2 = new QueryFile (inFile2.readLine ());
queryRecord = currentRecord2.getQuery();
if (queryRecord.equals("ALL RECORDS"))
{
record = "ALL RECORDS";
}
// System.out.println(queryRecord);
for (int i = 0; i<queryArray.length; i ++){
queryArray[i]= "";
}
queryArray= currentRecord2.getQuery().replaceAll("\\s+", " ").split("\\s");
if (queryArray.length ==1)
{
record = queryArray[0];
}
if (queryArray.length ==2)
{
record = queryArray[0];
record2 = queryArray[1];
}
if (queryArray.length ==3)
{
record = queryArray[0];
record2 = queryArray[1];
record3 = queryArray[2];
}
if (queryArray.length ==4)
{
record = queryArray[0];
record2 = queryArray[1];
record3 = queryArray[2];
record4 = queryArray[3];
}
}
private void DisplayHeader()
/*************************************************************************
*Method to display header for query report.
*************************************************************************/
{
System.out.println("===========================================================================================================");
System.out.println(" DUES TOTALS QUERY REPORT FOR DATA STRUCTURES AWARENESS SOCIETY");
System.out.println("===========================================================================================================");
System.out.println(" Region Category Journals Membership Level Total Dues");
System.out.println("-----------------------------------------------------------------------------------------------------------");
}
class QueryFile
/*************************************************************************
*Class for the query file.
*************************************************************************/
{
public String getQuery()
{
return query;
}
public void setQuery(String query)
{
this.query = query;
}
String query;
public QueryFile(String inputLine)
/*************************************************************************
* This constructor intializes the data fields with the string information
* provided by one line from the data files.
*************************************************************************/
{
{
if (inputLine == null)
{
System.out.printf("\n\n%23s%23s"," ","All queries have been processed..........Exiting program.\n\n");
System.exit(1);
}
else
{
query = inputLine.trim();
if (query.equals(""))
{
query ="ALL";
}
}
}
}
}
class Members
/*************************************************************************
*Class for the members file information.
*************************************************************************/
{
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
String model;
public Members (String inputLine)
/*************************************************************************
* This constructor intializes the data fields with the string information
* provided by one line from the data files.
*************************************************************************/
{
{
if (inputLine == null)
{
}
else
{
model = inputLine.trim();
}
}
}
}
class TextFileReader
/***************************************************************************
* Class TextFileReader controls reading from a text file by opening the file,
* reading one line at a time, keeping track
* of the end of file, and handling I/O errors.
***************************************************************************/
{
private BufferedReader inputFile;
private String fileName;
private boolean dataLeft;
public TextFileReader (String name)
/***********************************************************************
* This constructor opens the file given by the name passed in the parameter.
***********************************************************************/
{
fileName = name;
try
{
inputFile = new BufferedReader(new FileReader(fileName));
}
catch (IOException error)
{
error.printStackTrace();
System.err.println ("An error occured opening file " + fileName + "!\n");
System.exit(1);
}
dataLeft = true;
} // end TextFileReader
public String readLine ()
/****************************************************************************
* This method reads and returns one line of the text file. if the file is
* empty, an empty string is returned.
****************************************************************************/
{
String inputLine;
try
{
if ( (inputLine = inputFile.readLine()) == null )
dataLeft = false;
else return inputLine;
}
catch (IOException error)
{
System.err.println ("An error occured reading from file " + fileName +
"!\n");
System.exit(1);
}
return null;
} // end readLine
public boolean moreData ()
/**************************************************************************
* This function returns true if the end has not
**************************************************************************/
{ return dataLeft; } // end moreData
}// end class TextFileReader
}