Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EOF Exception?

1 view
Skip to first unread message

zhah99 via JavaKB.com

unread,
Mar 24, 2006, 9:33:58 AM3/24/06
to
Hello everyone! First of all I will go ahead and give a rundown on what my
program does that way you have a good idea of what I am doing, instead of my
just throwing code out there :).

The program will read the original sales data from SalesData.txt, that will
create salesmen and then that information will be entered into an array list
Salesman.

In the SalesData.txt each salesman has a level type of ENTRY, JUNIOR, or
SENIOR. I have an enumerated class for the sales level of a salesman. There
is a variable, bonus, associated with each type. The bonus for SENIOR is 15%.
The bonus for JUNIOR is 10%, and the bonus for ENTRY is 5%.

The user will be able to enter additional data on the GUI, either a new
salesman and his product sales or the user can update an existing salesman.

Then once all of the data is entered the data will be written to the Salesman.
data as objects.

I have three buttons on my GUI. One adds each salesman, one writes the
Salesman data to Salesman.data as objects and the third button reads the
Salesman.data file, put the information in a vector, sorts it (alphabetical
order), and last prints the information to the output area on the GUI in
alphabetical order.

Last, I created a NoSalesException. This exception will be thrown from
calculateSales and handled from the calling method. To handle this exception,
I will print a warning message dialog box that states the name of the
salesman and that he has no sales.

Ok so I am having two problems. One I am getting an error that says this:

[code]
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte
(ObjectInputSt
ream.java:2502)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1267)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
at Sales.readObjectData(Sales.java:285)
at Sales$Handler.actionPerformed(Sales.java:161)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:
18
49)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.
jav
a:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed
(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:
258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased
(BasicButtonL
istener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:
4212
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:
3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy
(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
[/code]

I am really unsure of why this is happening. This is an EndOfFileException
right? I have no idea how to handle this. The error pops up when ever I
click the button "Grand Total" which is where the file is read back in.

And the second problem I am having is with the NoSalesException I created
which is handled in the addSalesman() method and is thrown from CalcSales().
I thought I had it written correctly, but it is saying I cannot use "this" in
the JOptionPane because of everything being static. Is there away around
this or do I need to change something else in my code? Any help would be
very much appreciated. Thank you very much for any assistance.

Sales:
[code]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
import java.text.NumberFormat;

//Class Sales runs a program that reads data from an existing file (file
contains salesmen name and the amount of products sold),
//calculates their sales amount and adds their bonus to it. The class also
allows a user to enter additional salesman information
//which is also calculated and totaled. Last all of the Salesman data is
printed to the textarea.

public class Sales extends JFrame
{
static JLabel salesmanlabel;
static JTextField salesmanname;
static JLabel amtsold;
static JTextField amount;
static JLabel extra;
static JLabel productname;
static JComboBox product;
static JLabel level;
static JComboBox saleslevel;
static JButton salesmandone;
static JButton entrydone;
static JButton calctotal;
static JTextArea output;
static String outp;
static Handler handler = new Handler();
static ArrayList<Salesman> myList = new ArrayList<Salesman>();
static NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

//Creates GUI and adds ActionListeners.
public Sales()
{
super("Monthly Sales");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());

salesmanlabel = new JLabel("Salesman:");
salesmanname = new JTextField(20);
amtsold = new JLabel("Amount Sold:");
amount = new JTextField(20);
productname = new JLabel("Product Sold:");
product = new JComboBox();
extra = new JLabel("");
level = new JLabel("Sales Level:");
saleslevel = new JComboBox();
salesmandone = new JButton("Salesman Entry Done");
entrydone = new JButton("All Data Entry Done");
calctotal = new JButton("Grand Total");
output = new JTextArea(300, 300);

product.addItem("");
product.addItem("Product 1");
product.addItem("Product 2");
product.addItem("Product 3");
product.addItem("Product 4");
product.addItem("Product 5");

saleslevel.addItem("");
saleslevel.addItem("Entry");
saleslevel.addItem("Junior");
saleslevel.addItem("Senior");

c.add(salesmanlabel);
c.add(salesmanname);
c.add(amtsold);
c.add(amount);
c.add(extra);
c.add(productname);
c.add(product);
c.add(level);
c.add(saleslevel);
c.add(salesmandone);
c.add(entrydone);
c.add(calctotal);
c.add(output);

output.setVisible(true);
output.setRows(26);
output.setColumns(30);

salesmandone.addActionListener(handler);
entrydone.addActionListener(handler);
calctotal.addActionListener(handler);
}

//Creates the JFrame and runs the method readFileData().
public static void main (String args[])
{
Sales sale = new Sales();
sale.setSize(350,600);
sale.setVisible(true);

readFileData();
}

//Reads data from an existing text file and cacualtes a salesmans total
sales.
public static void readFileData()
{
String line;

ClassificationLevel level;

try
{
FileReader file = new FileReader("SalesData.txt");
BufferedReader buff = new BufferedReader(file);


while ((line = buff.readLine())!= null)
{
String[] elements = line.split("&");
String name = elements [0];
double sales = 0.0;
double sales1 = 0.0;
double sales2 = 0.0;
double sales3 = 0.0;
double sales4 = 0.0;
double sales5 = 0.0;

sales1 = Double.parseDouble(elements[1]) * 2.98;
sales2 = Double.parseDouble(elements[2]) * 4.50;
sales3 = Double.parseDouble(elements[3]) * 9.98;
sales4 = Double.parseDouble(elements[4]) * 4.49;
sales5 = Double.parseDouble(elements[5]) * 6.87;

level = ClassificationLevel.valueOf(elements[6]);

sales = (sales1 + sales2 + sales3 + sales4 + sales5);

sales = sales + (sales * level.getBonus());

myList.add(new Salesman(name, sales));
}
buff.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

//Implements three ActionListeners to three different buttons on the GUI.
public static class Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == salesmandone)
{
addSalesman();
}
else if(e.getSource() == entrydone)
{
writeFileData();
}
else if(e.getSource() == calctotal)
{
readObjectData();
}
}
}

//Sorts the names of salesman, if a name is already in the ArrayList then
the name is not created again,
//instead the additional information is added to the one name.
public static Salesman getSalesman(String salesman)
{
for (Salesman salesm : myList)
{
if (salesman.equals(salesm.getName()))
{
return salesm;
}
}
return null;
}

//Adds each Salesman name and sales to the ArrayList
public static void addSalesman()
{
try
{
double sales = calculateSales(product.getSelectedIndex(), Integer.parseInt
(amount.getText()));

if (getSalesman(salesmanname.getText()) != null)
{
getSalesman(salesmanname.getText()).addToSales(sales);
}
else
{
myList.add(new Salesman(salesmanname.getText(), sales));
}

salesmanname.setText("");
product.setSelectedIndex(0);
amount.setText("");
saleslevel.setSelectedIndex(0);
salesmanname.requestFocus();
}
catch(ArithmeticException ae)
{
JOptionPane.showMessageDialog(this, name + "has no Sales!!", "Sales equals
Zero",
JOptionPane.WARNING_MESSAGE);
}
}

//Calculates each salesman's sales and bonus and then returns that total to
addSalesman() to be entered in the ArrayList.
public static double calculateSales(int product,int amount) throws
ArithmeticException
{
double sales1;
double sales = 0.0;

product++;

switch(product)
{
case 2: sales1 = (amount * 2.98);
break;

case 3: sales1 = (amount * 4.50);
break;

case 4: sales1 = (amount * 9.98);
break;

case 5: sales1 = (amount * 4.49);
break;

case 6: sales1 = (amount * 6.87);
break;

default: sales1 = (0.00);
}

if(saleslevel.getSelectedItem().equals("Entry"))
{
sales = sales1 + (sales1 * .05);
}
else if(saleslevel.getSelectedItem().equals("Junior"))
{
sales = sales1 + (sales1 * .10);
}
else if(saleslevel.getSelectedItem().equals("Senior"))
{
sales = sales1 + (sales1 * .15);
}

return sales;
}

//Writes the ArrayList data as objects to Salesman.data.
public static void writeFileData()
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream
("Salesman.data"));

try
{
for (Salesman s : myList)
{
oos.writeObject(s);
}
}
finally
{
oos.close();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

//Reads the data in Salesman.data as objects and puts that data into a
vector which then sorts the Salesman in alphabetical
//order and then displays the data in the texatarea.
public static void readObjectData()
{
try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream
("Salesman.data"));

try
{
Vector<Salesman> salesmanList = new Vector<Salesman>();

Salesman salesman;
while ((salesman = (Salesman) ois.readObject()) != null)
{
salesmanList.add(salesman);

Collections.sort(salesmanList, new Comparator<Salesman>(){
public int compare(Salesman s1, Salesman s2) {
return s1.getName().compareTo(s2.getName());}});

StringBuilder builder = new StringBuilder("\tTOTAL SALES\n\n");

double total = 0.00;

for (Salesman s : salesmanList)
{
builder.append(s.toString() +"\n");
total += s.getSales();
}

builder.append("\nTotal\t\t " + currencyFormat.format(total));

output.setText(builder.toString());
}
}
finally
{
ois.close();
}
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
}
[/code]

--
Message posted via http://www.javakb.com

Roedy Green

unread,
Mar 24, 2006, 1:08:08 PM3/24/06
to
On Fri, 24 Mar 2006 14:33:58 GMT, "zhah99 via JavaKB.com" <u19084@uwe>
wrote, quoted or indirectly quoted someone who said :

>I am really unsure of why this is happening. This is an EndOfFileException
>right? I have no idea how to handle this. The error pops up when ever I
>click the button "Grand Total" which is where the file is read back in.

you get an EOFException when you read past the end of a file. This is
normal. That's how you know you are done.

To deal with it:

try {

i/o read loop

}

catch ( EOFException e )
{
/* all done reading */
}
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

zhah99 via JavaKB.com

unread,
Mar 24, 2006, 7:56:30 PM3/24/06
to
Ok, I got that exception taken care of. Thank you for clearing that up for
me. I appreciate it! Also, I am still having trouble with the exception I
created. I was wondering if someone would mind checking out my code and
giving me an advice on how to get that running properly.

NoSalesException:
[code]
public class NoSalesException extends Exception
{
public NoSalesException()
{
super("Sales Equals Zero");
}
public NoSalesException(String message)
{
super(message);
}
}
[/code]

if( sales == 0.00 )
{
throw new NoSalesException();
}

myList.add(new Salesman(name, sales));

}
buff.close();

}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(NoSalesException nse)
{
for(Salesman salesm : myList)
{
JOptionPane.showMessageDialog(null, salesm.getName() + " has no Sales!!",
"Sales Equals Zero",
JOptionPane.WARNING_MESSAGE);
}
}
}

//Implements three ActionListeners to three different buttons on the GUI.

public class Handler implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == salesmandone)
{
addSalesman();
}
else if(e.getSource() == entrydone)
{
writeFileData();
}
else if(e.getSource() == calctotal)
{
readObjectData();
}
}
}

//Sorts the names of salesman, if a name is already in the ArrayList then
the name is not created again,
//instead the additional information is added to the one name.
public static Salesman getSalesman(String salesman)
{
for (Salesman salesm : myList)
{
if (salesman.equals(salesm.getName()))
{
return salesm;
}
}
return null;
}

//Adds each Salesman name and sales to the ArrayList

public void addSalesman()


{
double sales = calculateSales(product.getSelectedIndex(), Integer.parseInt
(amount.getText()));

if (getSalesman(salesmanname.getText()) != null)
{
getSalesman(salesmanname.getText()).addToSales(sales);
}
else
{
myList.add(new Salesman(salesmanname.getText(), sales));
}


salesmanname.setText("");
product.setSelectedIndex(0);
amount.setText("");
saleslevel.setSelectedIndex(0);
salesmanname.requestFocus();
}

//Calculates each salesman's sales and bonus and then returns that total to
addSalesman() to be entered in the ArrayList.
public static double calculateSales(int product,int amount)

{
double sales1;
double sales = 0.0;

product++;

switch(product)
{
case 2: sales1 = (amount * 2.98);
break;

case 3: sales1 = (amount * 4.50);
break;

case 4: sales1 = (amount * 9.98);
break;

case 5: sales1 = (amount * 4.49);
break;

case 6: sales1 = (amount * 6.87);
break;

default: sales1 = (0.00);
}

if(saleslevel.getSelectedItem().equals("Entry"))
{
sales = sales1 + (sales1 * .05);
}
else if(saleslevel.getSelectedItem().equals("Junior"))
{
sales = sales1 + (sales1 * .10);
}
else if(saleslevel.getSelectedItem().equals("Senior"))
{
sales = sales1 + (sales1 * .15);
}

if(sales == 0.00)
{
try
{
throw new NoSalesException();
}
catch(Exception e)
{
e.printStackTrace();


}
}

return sales;

}

//Writes the ArrayList data as objects to Salesman.data.
public static void writeFileData()
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream
("Salesman.data"));

try
{
for (Salesman s : myList)
{
oos.writeObject(s);
}
}
finally
{
oos.close();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

//Reads the data in Salesman.data as objects and puts that data into a
vector which then sorts the Salesman in alphabetical
//order and then displays the data in the texatarea.
public static void readObjectData()
{

boolean eof = false;
Salesman salesman;


Vector<Salesman> salesmanList = new Vector<Salesman>();

try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream
("Salesman.data"));

try
{

while(true)
{
salesman = (Salesman) ois.readObject();



salesmanList.add(salesman);

Collections.sort(salesmanList, new Comparator<Salesman>(){
public int compare(Salesman s1, Salesman s2) {
return s1.getName().compareTo(s2.getName());}});

StringBuilder builder = new StringBuilder("\tTOTAL SALES\n\n");

double total = 0.00;

for (Salesman s : salesmanList)
{
builder.append(s.toString() +"\n");
total += s.getSales();
}

builder.append("\nTotal\t\t " + currencyFormat.format(total));

output.setText(builder.toString());

}
}
finally
{
ois.close();
}
}

catch(EOFException eofe)
{
System.out.println("End of file");
}
catch (IOException ioe )
{
ioe.printStackTrace();


}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
}
[/code]

Thank you so much, I appreciate it!

Roedy Green wrote:
>>I am really unsure of why this is happening. This is an EndOfFileException
>>right? I have no idea how to handle this. The error pops up when ever I
>>click the button "Grand Total" which is where the file is read back in.
>
> you get an EOFException when you read past the end of a file. This is
>normal. That's how you know you are done.
>
>To deal with it:
>
>try {
>
> i/o read loop
>
>}
>
>catch ( EOFException e )
> {
> /* all done reading */
>}

--
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200603/1

Roedy Green

unread,
Mar 24, 2006, 9:06:58 PM3/24/06
to
On Sat, 25 Mar 2006 00:56:30 GMT, "zhah99 via JavaKB.com" <u19084@uwe>

wrote, quoted or indirectly quoted someone who said :

>if( sales == 0.00 )
> {
> throw new NoSalesException();
> }
If I were doing this I would capture a bit more information in the
Exception constructor about the circumstances.

You need some code of the form
try {
.... code that directly or indirectly calls readFileData.
}
catch ( NoSalesException e )
{
System.err.println( "oops " + e.getMessage() );

Ian Shef

unread,
Mar 30, 2006, 3:09:47 PM3/30/06
to
Roedy Green <my_email_is_post...@munged.invalid> wrote in
news:n89922tp2r74ag0rr...@4ax.com:

> On Sat, 25 Mar 2006 00:56:30 GMT, "zhah99 via JavaKB.com" <u19084@uwe>
> wrote, quoted or indirectly quoted someone who said :
>
>>if( sales == 0.00 )
>> {
>> throw new NoSalesException();
>> }
> If I were doing this I would capture a bit more information in the
> Exception constructor about the circumstances.
>

<snip>

Oh, and by the way, it is rarely constructive to compare floats or doubles to
exactly zero (0.0). Preferable:

if ( abs(sales) < 0.005d )

or perhaps just

if ( sales < 0.005d)

depending upon what the meaning of negative sales is.

--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *

0 new messages