HEEEELP

im stuck in an infinite loop. Im very new to this and need help. Here is the program:

// Department.java - This program creates a report that lists transaction

// details for a department store listed by department number, total sales for

// each department, and the total sales for the store.

// Input: department.dat.

// Output: Report.

import java.io.*; // Import class for file input.

public class Department

{

public static void main(String args[]) throws Exception

{

// This is the housekeeping module() work.

// Declare variables.

String head1 = "DEPARTMENT STORE SALES";

int transNumber = 0;// Current record transaction number.

double amount = 0; // Current record transaction amount.

int deptNum = 0;// Current record department number.

String transString = "";// String version of transaction number.

String amountString = ""; // String version of transaction amount.

String deptNumString = ""; // String version of department number.

double deptTotal = 0;// Sales total for a department.

double grandTotal = 0;// Total of all sales.

int prevDept;// Previous department number.

// Open input file.

FileReader fr = new FileReader("department.dat");

// Create BufferedReader object.

BufferedReader br = new BufferedReader(fr);

// Print two blank lines.

System.out.println();

System.out.println();

// Print heading.

System.out.println(head1);

// Print two blank lines.

System.out.println();

System.out.println();

// Read first record from file.

if((transString = br.readLine()) != null) // Test for EOF.

{

amountString = br.readLine();

deptNumString = br.readLine();

// Convert Strings to integer or double.

transNumber = Integer.parseInt(transString);

amount = Double.parseDouble(amountString);

deptNum = Integer.parseInt(deptNumString);

}

// Assign department number to previous department number.

prevDept = deptNum;

// Check for EOF.

while(transString != null)

{

// Test control break variable here.

if(deptNum != prevDept)

{

// Perform control break work here.

System.out.println(" Department Number: " + prevDept);

}

// Print transaction number and transaction amount here.

System.out.println(transNumber + " Transaction Amount: " + amount);

// Add transaction amount to department total here.

deptTotal += amount;

}

// Read next record here.

if((deptNumString = br.readLine()) != null)

transString= br.readLine();

// This is the cleanUp() module work.

System.out.println("Department Number: " + prevDept);

System.out.println();

System.out.println();

// Print department number and department total here.

System.out.println(deptTotal + "Department Total: " + deptTotal);

// Add department total to grand total here.

grandTotal += deptTotal;

prevDept = deptNum;

deptTotal = 0;

// Print total sales here.

System.out.println("Total Sales: " + grandTotal);

// Close file.

br.close();

// Exit program.

System.exit(0);

} // End of main() method.

} // End of Department class.

[3390 byte] By [DamRatza] at [2007-10-2 4:23:36]
# 1

PLEEEEEEASE put code inside of code tags when you post code. Click the code button above the text editing box and put your code between the tags.

I think your problem is here:

if((deptNumString = br.readLine()) != null)

transString = br.readLine();

Your loop is:

while(transString != null)

but if the readLine that sets deptNumString returns null, you never do the readLine that sets transString to null!

eminformaticsa at 2007-7-15 23:51:07 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks allot.. I'll check it out in the morning and see what I get... I'll work on the edicate here a little more, sorry.
DamRatza at 2007-7-15 23:51:07 > top of Java-index,Java Essentials,Java Programming...
# 3

Well I cant seem to figure out what it is that needs to be done. I still stuck on it. What should I change?// Department.java - This program creates a report that lists transaction

// details for a department store listed by department number, total sales for

// each department, and the total sales for the store.

// Input: department.dat.

// Output: Report.

import java.io.*; // Import class for file input.

public class Department

{

public static void main(String args[]) throws Exception

{

// This is the housekeeping module() work.

// Declare variables.

String head1 = "DEPARTMENT STORE SALES";

int transNumber = 0;// Current record transaction number.

double amount = 0; // Current record transaction amount.

int deptNum = 0;// Current record department number.

String transString = "";// String version of transaction number.

String amountString = ""; // String version of transaction amount.

String deptNumString = ""; // String version of department number.

double deptTotal = 0;// Sales total for a department.

double grandTotal = 0;// Total of all sales.

int prevDept;// Previous department number.

// Open input file.

FileReader fr = new FileReader("department.dat");

// Create BufferedReader object.

BufferedReader br = new BufferedReader(fr);

// Print two blank lines.

System.out.println();

System.out.println();

// Print heading.

System.out.println(head1);

// Print two blank lines.

System.out.println();

System.out.println();

// Read first record from file.

if((transString = br.readLine()) != null) // Test for EOF.

{

amountString = br.readLine();

deptNumString = br.readLine();

// Convert Strings to integer or double.

transNumber = Integer.parseInt(transString);

amount = Double.parseDouble(amountString);

deptNum = Integer.parseInt(deptNumString);

}

// Assign department number to previous department number.

prevDept = deptNum;

// Check for EOF.

while(transString != null)

{

// Test control break variable here.

if(deptNum != prevDept)

{

// Perform control break work here.

System.out.println(" Department Number: " + prevDept);

}

// Print transaction number and transaction amount here.

System.out.println(transNumber + " Transaction Amount: " + amount);

// Add transaction amount to department total here.

deptTotal += amount;

}

// Read next record here. Im not sure how to get the next record to read from this point.

if((deptNumString = br.readLine()) != null)

transString = br.readLine();

// This is the cleanUp() module work.

System.out.println("Department Number: " + prevDept);

System.out.println();

System.out.println();

// Print department number and department totalhere.

System.out.println(deptTotal + "Department Total: " + deptTotal);

// Add department total to grand total here. down here do I need to zero out the amount variable too?

grandTotal += deptTotal;

prevDept = deptNum;

deptTotal = 0;

// Print total sales here.

System.out.println("Total Sales: " + grandTotal);

// Close file.

br.close();

// Exit program.

System.exit(0);

} // End of main() method.

} // End of Department class.

damtheratza at 2007-7-15 23:51:07 > top of Java-index,Java Essentials,Java Programming...
# 4

// Check for EOF

while(transString != null)

{

// Test control break variable here.

if(deptNum != prevDept)

{

// Perform control break work here.

System.out.println(" Department Number: " + prevDept);

}

// Print transaction number and transaction amount here.

System.out.println(transNumber + " Transaction Amount: " + amount);

// Add transaction amount to department total here.

deptTotal += amount;

}

Your while loop runs as long as transString is not null but there is nothing inside your while loop that changes transString. Thus, once you enter the while loop with a non-null transString, the while loop will loop forever.

eminformaticsa at 2007-7-15 23:51:07 > top of Java-index,Java Essentials,Java Programming...