I need help once again. I am new in this course and are still battleling with some of the work. I wrote a code for Displaying Stock Information, however it's giving me a error. Please help?

/*
   Chapter 8:   Programming Assignment 9
   Programmer:  T. du Preez
   Date:        September 20, 2009
   Filename:    Stock
*/

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

public class Stock extends JFrame implements ActionListener
{
    //Declare an input variable
    DataInputStream input;

    //Construct components
    JPanel fieldPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JLabel stockLabel = new JLabel("Stock Name:");
    JLabel stock = new JLabel("                        ");
    JLabel volumeLabel = new JLabel("Volume");
    JLabel volume = new JLabel("                       ");
    JLabel priceLabel = new JLabel("Closing Price:");
    JLabel price = new JLabel("                        ");
    JLabel changeLabel = new JLabel("Change:");
    JLabel change = new JLabel("                       ");
    JButton next = new JButton("Next->");


    public static void main(String[] args)
    {
        Stock window = new Stock();
        window.setTitle("Yesterday's 10 Hottest Stocks");
        window.setSize(300, 175);
        window.setVisible(true);
    }

    public Stock()
    {
        //Set colors
        setBackground(Color.blue);
        setForeground(Color.white);
        next.setForeground(Color.black);

        //Set layout managers
        setLayout(new BorderLayout());
        fieldPanel.setLayout(new GridLayout(4,2));
        buttonPanel.setLayout(new FlowLayout());

        //Add components and actionListener to interface
        fieldPanel.add(stockLabel);
        fieldPanel.add(stock);
        fieldPanel.add(volumeLabel);
        fieldPanel.add(volume);
        fieldPanel.add(priceLabel);
        fieldPanel.add(price);
        fieldPanel.add(changeLabel);
        fieldPanel.add(change);
        buttonPanel.add(next);
        add(fieldPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
        next.addActionListener(this);

        try
        {
           //Open the file
           input = new DataInputStream(new FileInputStream("hotStocks.dat"));
        }
        catch(IOException ex)
        {
           closeFile();
        }

        //Construct window listener
        addWindowListener(
        new WindowAdapter()
        {
           public void windowClosing(WindowEvent e)
           {
               closeFile();
           }
        }
        );
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            stock.setText(input.readUTF());
            volume.setText(input.readUTF());
            price.setText(input.readUTF());
            change.setText(input.readUTF());
        }
        catch(IOException e2)
        {
            stock.setText("End of File");
            volume.setText("");
            price.setText("");
            change.setText("");
        }
    }

    public void closeFile()
    {
          try
          {
             input.close();
          }
          catch(IOException c)
          {
                System.exit(1);
          }
                System.exit(0);
    }
}[/*
   Chapter 8:   Programming Assignment 9
   Programmer:  T. du Preez
   Date:        September 20, 2009
   Filename:    Stock
*/

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

public class Stock extends JFrame implements ActionListener
{
    //Declare an input variable
    DataInputStream input;

    //Construct components
    JPanel fieldPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JLabel stockLabel = new JLabel("Stock Name:");
    JLabel stock = new JLabel("                        ");
    JLabel volumeLabel = new JLabel("Volume");
    JLabel volume = new JLabel("                       ");
    JLabel priceLabel = new JLabel("Closing Price:");
    JLabel price = new JLabel("                        ");
    JLabel changeLabel = new JLabel("Change:");
    JLabel change = new JLabel("                       ");
    JButton next = new JButton("Next->");


    public static void main(String[] args)
    {
        Stock window = new Stock();
        window.setTitle("Yesterday's 10 Hottest Stocks");
        window.setSize(300, 175);
        window.setVisible(true);
    }

    public Stock()
    {
        //Set colors
        setBackground(Color.blue);
        setForeground(Color.white);
        next.setForeground(Color.black);

        //Set layout managers
        setLayout(new BorderLayout());
        fieldPanel.setLayout(new GridLayout(4,2));
        buttonPanel.setLayout(new FlowLayout());

        //Add components and actionListener to interface
        fieldPanel.add(stockLabel);
        fieldPanel.add(stock);
        fieldPanel.add(volumeLabel);
        fieldPanel.add(volume);
        fieldPanel.add(priceLabel);
        fieldPanel.add(price);
        fieldPanel.add(changeLabel);
        fieldPanel.add(change);
        buttonPanel.add(next);
        add(fieldPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
        next.addActionListener(this);

        try
        {
           //Open the file
           input = new DataInputStream(new FileInputStream("hotStocks.dat"));
        }
        catch(IOException ex)
        {
           closeFile();
        }

        //Construct window listener
        addWindowListener(
        new WindowAdapter()
        {
           public void windowClosing(WindowEvent e)
           {
               closeFile();
           }
        }
        );
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            stock.setText(input.readUTF());
            volume.setText(input.readUTF());
            price.setText(input.readUTF());
            change.setText(input.readUTF());
        }
        catch(IOException e2)
        {
            stock.setText("End of File");
            volume.setText("");
            price.setText("");
            change.setText("");
        }
    }

    public void closeFile()
    {
          try
          {
             input.close();
          }
          catch(IOException c)
          {
                System.exit(1);
          }
                System.exit(0);
    }
/*
   Chapter 8:   Programming Assignment 9
   Programmer:  T. du Preez
   Date:        September 20, 2009
   Filename:    Stock
*/

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

public class Stock extends JFrame implements ActionListener
{
    //Declare an input variable
    DataInputStream input;

    //Construct components
    JPanel fieldPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JLabel stockLabel = new JLabel("Stock Name:");
    JLabel stock = new JLabel("                        ");
    JLabel volumeLabel = new JLabel("Volume");
    JLabel volume = new JLabel("                       ");
    JLabel priceLabel = new JLabel("Closing Price:");
    JLabel price = new JLabel("                        ");
    JLabel changeLabel = new JLabel("Change:");
    JLabel change = new JLabel("                       ");
    JButton next = new JButton("Next->");


    public static void main(String[] args)
    {
        Stock window = new Stock();
        window.setTitle("Yesterday's 10 Hottest Stocks");
        window.setSize(300, 175);
        window.setVisible(true);
    }

    public Stock()
    {
        //Set colors
        setBackground(Color.blue);
        setForeground(Color.white);
        next.setForeground(Color.black);

        //Set layout managers
        setLayout(new BorderLayout());
        fieldPanel.setLayout(new GridLayout(4,2));
        buttonPanel.setLayout(new FlowLayout());

        //Add components and actionListener to interface
        fieldPanel.add(stockLabel);
        fieldPanel.add(stock);
        fieldPanel.add(volumeLabel);
        fieldPanel.add(volume);
        fieldPanel.add(priceLabel);
        fieldPanel.add(price);
        fieldPanel.add(changeLabel);
        fieldPanel.add(change);
        buttonPanel.add(next);
        add(fieldPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
        next.addActionListener(this);

        try
        {
           //Open the file
           input = new DataInputStream(new FileInputStream("hotStocks.dat"));
        }
        catch(IOException ex)
        {
           closeFile();
        }

        //Construct window listener
        addWindowListener(
        new WindowAdapter()
        {
           public void windowClosing(WindowEvent e)
           {
               closeFile();
           }
        }
        );
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            stock.setText(input.readUTF());
            volume.setText(input.readUTF());
            price.setText(input.readUTF());
            change.setText(input.readUTF());
        }
        catch(IOException e2)
        {
            stock.setText("End of File");
            volume.setText("");
            price.setText("");
            change.setText("");
        }
    }

    public void closeFile()
    {
          try
          {
             input.close();
          }
          catch(IOException c)
          {
                System.exit(1);
          }
                System.exit(0);
    }
}[/*
   Chapter 8:   Programming Assignment 9
   Programmer:  T. du Preez
   Date:        September 20, 2009
   Filename:    Stock
*/

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

public class Stock extends JFrame implements ActionListener
{
    //Declare an input variable
    DataInputStream input;

    //Construct components
    JPanel fieldPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JLabel stockLabel = new JLabel("Stock Name:");
    JLabel stock = new JLabel("                        ");
    JLabel volumeLabel = new JLabel("Volume");
    JLabel volume = new JLabel("                       ");
    JLabel priceLabel = new JLabel("Closing Price:");
    JLabel price = new JLabel("                        ");
    JLabel changeLabel = new JLabel("Change:");
    JLabel change = new JLabel("                       ");
    JButton next = new JButton("Next->");


    public static void main(String[] args)
    {
        Stock window = new Stock();
        window.setTitle("Yesterday's 10 Hottest Stocks");
        window.setSize(300, 175);
        window.setVisible(true);
    }

    public Stock()
    {
        //Set colors
        setBackground(Color.blue);
        setForeground(Color.white);
        next.setForeground(Color.black);

        //Set layout managers
        setLayout(new BorderLayout());
        fieldPanel.setLayout(new GridLayout(4,2));
        buttonPanel.setLayout(new FlowLayout());

        //Add components and actionListener to interface
        fieldPanel.add(stockLabel);
        fieldPanel.add(stock);
        fieldPanel.add(volumeLabel);
        fieldPanel.add(volume);
        fieldPanel.add(priceLabel);
        fieldPanel.add(price);
        fieldPanel.add(changeLabel);
        fieldPanel.add(change);
        buttonPanel.add(next);
        add(fieldPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
        next.addActionListener(this);

        try
        {
           //Open the file
           input = new DataInputStream(new FileInputStream("hotStocks.dat"));
        }
        catch(IOException ex)
        {
           closeFile();
        }

        //Construct window listener
        addWindowListener(
        new WindowAdapter()
        {
           public void windowClosing(WindowEvent e)
           {
               closeFile();
           }
        }
        );
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            stock.setText(input.readUTF());
            volume.setText(input.readUTF());
            price.setText(input.readUTF());
            change.setText(input.readUTF());
        }
        catch(IOException e2)
        {
            stock.setText("End of File");
            volume.setText("");
            price.setText("");
            change.setText("");
        }
    }

    public void closeFile()
    {
          try
          {
             input.close();
          }
          catch(IOException c)
          {
                System.exit(1);
          }
                System.exit(0);
    }
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

One, code tags and formatting.

[code=JAVA] // paste code here

[/code]

Repost using them, please. Code looks terrible without them.

Two, you've posted the Stock class three or four times. Why?

Three, you need to ask a more detailed question. What error do you get? Compile or run-time? If run-time, under what circumstances? What is the program supposed to do? Etc. "It's giving me an error" is vague.


Repost your program ONE time, not three or four, and use code tags please, as explained above.

This article has been dead for over six months. Start a new discussion instead.