swing to read stock list from a file

swing to read stock list from a file

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2253 Views - Last Post: 18 August 2009 - 10:27 AM Rate Topic: -----

#1 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

swing to read stock list from a file

Post icon  Posted 18 August 2009 - 01:38 AM

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

/*
   Project 8	  hotStocks
   Programmer:	[Type your name here]
   Date:		  [Type the date here]
   Program Name:  hotStocks
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
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.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	  window.setTitle("Yesterday's 10 Hottest Stocks");
	  window.setSize(500, 500);
	  window.setVisible(true);
   }

   public Stock()
   {
	 stock.setBackground(Color.blue);
	 stock.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)
	 {
		System.exit(1);
	 }

	 //Construct window listener
	 addWindowListener(
		new WindowAdapter()
		   {
			   public void windowClosing(WindowEvent e)
			   {
				  System.exit(0);
			   }
		   }
	  );
   }

   public void actionPerformed(ActionEvent e)
   {
	   String arg = e.getActionCommand();
	   if(arg == "Next->")
	   {
			  try
			  {
				 //Read the data into the text fields
				  stock.setText(input.readUTF());
				  volume.setText(input.readUTF());
				  price.setText(input.readUTF());
				  change.setText(input.readUTF());

			  }
			  catch(IOException e2)
			  {
				 //Print End of file message and clear fields
				 JOptionPane.showMessageDialog(null, "That was the Last Record for the Top 10 hot Stocks");
			  }
			  clearFields();
		}
		else
		{
			try
			{
				input.close();
			  }
			  catch(IOException c)
			{
				  System.exit(1);
			}
			  System.exit(0);
		}
   }

   public void clearFields()
   {
	  stock.setText("");
	  volume.setText("");
	  price.setText("");
	  change.setText("");
   }
}





Is This A Good Question/Topic? 0
  • +

Replies To: swing to read stock list from a file

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: swing to read stock list from a file

Posted 18 August 2009 - 02:10 AM

View Postsammantah, on 18 Aug, 2009 - 07:38 AM, said:

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

are you sure the file has been onpened OK? I suggest printing the exception message so you can see what the cause was
Was This Post Helpful? 0
  • +
  • -

#3 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 02:35 AM

View Posthorace, on 18 Aug, 2009 - 01:10 AM, said:

View Postsammantah, on 18 Aug, 2009 - 07:38 AM, said:

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

are you sure the file has been onpened OK? I suggest printing the exception message so you can see what the cause was

i dunno but the code didn't compile at all as java application or java beans did it run with any one else?
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: swing to read stock list from a file

Posted 18 August 2009 - 03:27 AM

View PostoQMr FoxQo, on 18 Aug, 2009 - 08:35 AM, said:

View Posthorace, on 18 Aug, 2009 - 01:10 AM, said:

View Postsammantah, on 18 Aug, 2009 - 07:38 AM, said:

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

are you sure the file has been onpened OK? I suggest printing the exception message so you can see what the cause was

i dunno but the code didn't compile at all as java application or java beans did it run with any one else?

it compiled OK on my machine - I created an empty hotStocks.dat file and updated this to print an Exception message
   public void actionPerformed(ActionEvent e)
   {
	   String arg = e.getActionCommand();
	   if(arg == "Next->")
	   {
			  try
			  {
				 //Read the data into the text fields
				  stock.setText(input.readUTF());
				  volume.setText(input.readUTF());
				  price.setText(input.readUTF());
				  change.setText(input.readUTF());

			  }
			  catch(IOException e2)
			  {
				 System.out.println("error " + e2);   // <<< added this line
				 //Print End of file message and clear fields
				 JOptionPane.showMessageDialog(null, "That was the Last Record for the Top 10 hot Stocks");
			  }
			  clearFields();
		}


and when run it created the Frame and when i clicked Next it gave
error java.io.EOFException


which would be expected as my file had no data in it

This post has been edited by horace: 18 August 2009 - 03:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 04:26 AM

View Posthorace, on 18 Aug, 2009 - 02:27 AM, said:

View PostoQMr FoxQo, on 18 Aug, 2009 - 08:35 AM, said:

View Posthorace, on 18 Aug, 2009 - 01:10 AM, said:

View Postsammantah, on 18 Aug, 2009 - 07:38 AM, said:

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

are you sure the file has been onpened OK? I suggest printing the exception message so you can see what the cause was

i dunno but the code didn't compile at all as java application or java beans did it run with any one else?

it compiled OK on my machine - I created an empty hotStocks.dat file and updated this to print an Exception message
   public void actionPerformed(ActionEvent e)
   {
	   String arg = e.getActionCommand();
	   if(arg == "Next->")
	   {
			  try
			  {
				 //Read the data into the text fields
				  stock.setText(input.readUTF());
				  volume.setText(input.readUTF());
				  price.setText(input.readUTF());
				  change.setText(input.readUTF());

			  }
			  catch(IOException e2)
			  {
				 System.out.println("error " + e2);   // <<< added this line
				 //Print End of file message and clear fields
				 JOptionPane.showMessageDialog(null, "That was the Last Record for the Top 10 hot Stocks");
			  }
			  clearFields();
		}


and when run it created the Frame and when i clicked Next it gave
error java.io.EOFException


which would be expected as my file had no data in it


thanks for the replies but after adding the same code as what horace added the program still doesn't access the file which has data. any ideas guys???
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: swing to read stock list from a file

Posted 18 August 2009 - 04:32 AM

View Postsammantah, on 18 Aug, 2009 - 10:26 AM, said:

View Posthorace, on 18 Aug, 2009 - 02:27 AM, said:

View PostoQMr FoxQo, on 18 Aug, 2009 - 08:35 AM, said:

View Posthorace, on 18 Aug, 2009 - 01:10 AM, said:

View Postsammantah, on 18 Aug, 2009 - 07:38 AM, said:

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

are you sure the file has been onpened OK? I suggest printing the exception message so you can see what the cause was

i dunno but the code didn't compile at all as java application or java beans did it run with any one else?

it compiled OK on my machine - I created an empty hotStocks.dat file and updated this to print an Exception message
   public void actionPerformed(ActionEvent e)
   {
	   String arg = e.getActionCommand();
	   if(arg == "Next->")
	   {
			  try
			  {
				 //Read the data into the text fields
				  stock.setText(input.readUTF());
				  volume.setText(input.readUTF());
				  price.setText(input.readUTF());
				  change.setText(input.readUTF());

			  }
			  catch(IOException e2)
			  {
				 System.out.println("error " + e2);   // <<< added this line
				 //Print End of file message and clear fields
				 JOptionPane.showMessageDialog(null, "That was the Last Record for the Top 10 hot Stocks");
			  }
			  clearFields();
		}


and when run it created the Frame and when i clicked Next it gave
error java.io.EOFException


which would be expected as my file had no data in it


thanks for the replies but after adding the same code as what horace added the program still doesn't access the file which has data. any ideas guys???

what is the message from the exception? could you give us a sample of the data file to try?
Was This Post Helpful? 0
  • +
  • -

#7 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 04:55 AM

[quote name='horace' date='18 Aug, 2009 - 03:32 AM' post='738753']

View Postsammantah, on 18 Aug, 2009 - 10:26 AM, said:

View Posthorace, on 18 Aug, 2009 - 02:27 AM, said:

View PostoQMr FoxQo, on 18 Aug, 2009 - 08:35 AM, said:

View Posthorace, on 18 Aug, 2009 - 01:10 AM, said:

View Postsammantah, on 18 Aug, 2009 - 07:38 AM, said:

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

are you sure the file has been onpened OK? I suggest printing the exception message so you can see what the cause was

i dunno but the code didn't compile at all as java application or java beans did it run with any one else?

it compiled OK on my machine - I created an empty hotStocks.dat file and updated this to print an Exception message
   public void actionPerformed(ActionEvent e)
   {
	   String arg = e.getActionCommand();
	   if(arg == "Next->")
	   {
			  try
			  {
				 //Read the data into the text fields
				  stock.setText(input.readUTF());
				  volume.setText(input.readUTF());
				  price.setText(input.readUTF());
				  change.setText(input.readUTF());

			  }
			  catch(IOException e2)
			  {
				 System.out.println("error " + e2);   // <<< added this line
				 //Print End of file message and clear fields
				 JOptionPane.showMessageDialog(null, "That was the Last Record for the Top 10 hot Stocks");
			  }
			  clearFields();
		}


and when run it created the Frame and when i clicked Next it gave
error java.io.EOFException


which would be expected as my file had no data in it


thanks for the replies but after adding the same code as what horace added the program still doesn't access the file which has data. any ideas guys???

what is the message from the exception? could you give us a sample of the data file to try?


i have the file attached and the error message, it just pops up this: "errorjava.io.EOFException"

don't think my attachment was posted successsfully however this is what the file has

stock Chevron 
89,147,500 59.13 +2 Promina 
65,323,400 44.44 +.56 Yahoo 
60,709,500 29 +2.94 DellCptr 
40,835,400 33.44 +1 Westpac 
26,358,300 31.88 +2 Microsft 
25,092,700 61.31 +.69 ExodusC 
24,999,200 49.13 -4.13 EricTel 
23,418,800 15.94 -.13 Alumina 
22,579,400 63.88 ...


Was This Post Helpful? 0
  • +
  • -

#8 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: swing to read stock list from a file

Posted 18 August 2009 - 05:19 AM

View Postsammantah, on 18 Aug, 2009 - 10:55 AM, said:

View Posthorace, on 18 Aug, 2009 - 03:32 AM, said:

thanks for the replies but after adding the same code as what horace added the program still doesn't access the file which has data. any ideas guys???

what is the message from the exception? could you give us a sample of the data file to try?


i have the file attached and the error message, it just pops up this: "errorjava.io.EOFException"

don't think my attachment was posted successsfully however this is what the file has

stock Chevron 
89,147,500 59.13 +2 Promina 
65,323,400 44.44 +.56 Yahoo 
60,709,500 29 +2.94 DellCptr 
40,835,400 33.44 +1 Westpac 
26,358,300 31.88 +2 Microsft 
25,092,700 61.31 +.69 ExodusC 
24,999,200 49.13 -4.13 EricTel 
23,418,800 15.94 -.13 Alumina 
22,579,400 63.88 ...


are you sure the file is properly encoded UTF?
if in actionPerformed() I change the input.readUTF()s to input.readLine()s and remove the call to clearFields();
when I click Next it reads data and display it in the Frame

This post has been edited by horace: 18 August 2009 - 05:19 AM

Was This Post Helpful? 0
  • +
  • -

#9 ProdicalProgrammer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 26-June 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 05:44 AM

try this and see if it runs
   public void actionPerformed(ActionEvent e)
   {
	  try
	  {
		 //Read the data into the text fields
		 stock.setText(input.readUTF());
		 volume.setText(input.readUTF());
		 price.setText(input.readUTF());
		 change.setText(input.readUTF());
	  }
	  catch(IOException e2)
	  {
		 //Print End of file message and clear fields
		 stock.setText("End of File");
		 volume.setText("");
		 price.setText("");
		 change.setText("");
	  }
   }


Was This Post Helpful? 0
  • +
  • -

#10 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 05:48 AM

thanks a lot for your help horace. but you see for this one i must use readUTF
Was This Post Helpful? 0
  • +
  • -

#11 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 06:04 AM

that reads but it reads what you have in
 catch(IOException e2) 
so again it bypasses my
try{}

Was This Post Helpful? 0
  • +
  • -

#12 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: swing to read stock list from a file

Posted 18 August 2009 - 06:11 AM

View Postsammantah, on 18 Aug, 2009 - 11:48 AM, said:

thanks a lot for your help horace. but you see for this one i must use readUTF

are you sure the file is in correct UTF format - it gives EOF on the first input.readUTF()

was the file generated by another Java program?
Was This Post Helpful? 0
  • +
  • -

#13 sammantah  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 20-June 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 06:19 AM

no i'm not sure of the format and its supplied as part of the assignment so i don't know what program was used to create the file.
Was This Post Helpful? 0
  • +
  • -

#14 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: swing to read stock list from a file

Posted 18 August 2009 - 09:17 AM

View Postsammantah, on 18 Aug, 2009 - 12:19 PM, said:

no i'm not sure of the format and its supplied as part of the assignment so i don't know what program was used to create the file.

do you have a specification of the contents of the file?
could you attach the file?

This post has been edited by horace: 18 August 2009 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#15 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: swing to read stock list from a file

Posted 18 August 2009 - 09:39 AM

View Posthorace, on 18 Aug, 2009 - 02:27 AM, said:

View PostoQMr FoxQo, on 18 Aug, 2009 - 08:35 AM, said:

View Posthorace, on 18 Aug, 2009 - 01:10 AM, said:

View Postsammantah, on 18 Aug, 2009 - 07:38 AM, said:

hi, i have created a swing program to display stock information whenever i click the "Next" Button but now soon as it gets to the try{} it does nothing meaning it doesn't read from my file but rather goes straight to the catch{}. please help, i have the code below

are you sure the file has been onpened OK? I suggest printing the exception message so you can see what the cause was

i dunno but the code didn't compile at all as java application or java beans did it run with any one else?

it compiled OK on my machine - I created an empty hotStocks.dat file and updated this to print an Exception message
   public void actionPerformed(ActionEvent e)
   {
	   String arg = e.getActionCommand();
	   if(arg == "Next->")
	   {
			  try
			  {
				 //Read the data into the text fields
				  stock.setText(input.readUTF());
				  volume.setText(input.readUTF());
				  price.setText(input.readUTF());
				  change.setText(input.readUTF());

			  }
			  catch(IOException e2)
			  {
				 System.out.println("error " + e2);   // <<< added this line
				 //Print End of file message and clear fields
				 JOptionPane.showMessageDialog(null, "That was the Last Record for the Top 10 hot Stocks");
			  }
			  clearFields();
		}


and when run it created the Frame and when i clicked Next it gave
error java.io.EOFException


which would be expected as my file had no data in it


you are right after creating a hotStocks.dat it worked
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2