2 Replies - 2112 Views - Last Post: 20 November 2008 - 09:25 AM Rate Topic: -----

#1 simonc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 17-November 08

Problem with File address in Class constructor

Post icon  Posted 20 November 2008 - 08:59 AM

Hi, new to java and having a problem with a file address (in Class Engine) which I would like to use in Class DataAccess.

The error that I get is

Unhandled exception type IOException

But can't thing of a way to fix it. The file and address are valid

If anyone has any ideas they would be appreciated. Below please find offending code

Thanks
Simon

package StatPairsModel;



public class Engine {
		  public static void main(String[] arguments) {
//**The next line has the offending error**
			  DataAccess rawStockData = new DataAccess("c:\\Beg Java Stuff\\SGC1.txt");
		Timeseries[] myTimeseries = new Timeseries[4];
		Stock stock1 = new Stock();
		Stock stock2 = new Stock();
		
		int numberOfPairs = 3;

		int[] myId = { 1, 2, 3, 4 };
		int[] myDate = { 37795, 37796, 37797, 37798 };
		double[] myClose_price_euro = { 1.23, 1.26, 1.25, 1.30 };
		double[] myClose_volume = { 2.321, 2.325, 2.365, 4.321 };
		double[] myClose_market_cap = { 3.21, 3.21, 3.21, 3.22 };
		int[] myCorp_action_ind = { 0, 1, 0, 0 };
		double[] myBeta = { 1.21, 1.32, 1.51, 1.21 };
		double[] myClose_price_local = { 2.23, 2.26, 2.25, 2.30 };

		
		
		
		// for (int i = 0; i<numberOfPairs; i++){

		stock1.setStock("0015425", "Vodafone", "VOD LN", "UK", "TELECOM",
				"GBP", "VOD LN", "FTSE");
		stock2.setStock("0045125", "Telecom Italia", "TIM IM", "IT", "TELECOM",
				"EUR", "TIM IM", "MIB");
		int i=0;
		for ( i = 0; i < myTimeseries.length; i++) {
			myTimeseries[i] = new Timeseries();
			myTimeseries[i].setTimeseries(myId, myDate, myClose_price_euro,
					myClose_volume, myClose_market_cap, myCorp_action_ind,
					myBeta, myClose_price_local);

			//System.out.println(myTimeseries[i].getId(myId));
			//System.out.println(myTimeseries.length);
			myTimeseries[i].display(i);
			
			
			
			
		}
		
		 //stock1.setMnemonic("123456");
		 //stock1.display();
		 //System.out.println();
		 //stock2.display();
	}
}


package StatPairsModel;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Enumeration;

public class DataAccess {

	String thisLine;
	String address;
	int dataPointCount = 0;

	DataAccess(String address) throws IOException {

		{
			try {

				FileReader file = new FileReader(address);
				BufferedReader buff = new BufferedReader(file);
				int dateKeyField = 0;
				int tickerKeyField = 0;
				String keyField = "";
				boolean eof = false;

				while ((thisLine = buff.readLine()) != null && !eof) {
					StringTokenizer st = new StringTokenizer(thisLine, ",");
					while (st.hasMoreElements()) {
					}

				}

				System.out.println("dataPointCount " + dataPointCount);
				System.out.println("address " + address);
			}

			catch (IOException e) {
				System.out.println("Error -- " + e.toString());
			}

		}

	}

	void process() {
		System.out.println("Processing....");
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with File address in Class constructor

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Problem with File address in Class constructor

Posted 20 November 2008 - 09:09 AM

Well something you are doing is not allowed
// Unhandled exception type IOException
DataAccess rawStockData = new DataAccess("c:\\Beg Java Stuff\\SGC1.txt");



Your constructor
DataAccess(String address) 
{
    // .. code .. // 
}



Perhaps try changing what you are passing the constructor to something else... perhaps "test.txt" ?
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Problem with File address in Class constructor

Posted 20 November 2008 - 09:25 AM

A ctor should not throw an exception and normally shouldn't even have to catch one. Give DataAccess a load method and handle opening the file in there. The ctor should simply return the instance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1