Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,486 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,772 people online right now. Registration is fast and FREE... Join Now!




Problem with File address in Class constructor

 

Problem with File address in Class constructor

simonc

20 Nov, 2008 - 07:59 AM
Post #1

D.I.C Head
**

Joined: 17 Nov, 2008
Posts: 55

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

CODE

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....");
    }
}


User is offlineProfile CardPM
+Quote Post


bbq

RE: Problem With File Address In Class Constructor

20 Nov, 2008 - 08:09 AM
Post #2

omgwtfbbq
Group Icon

Joined: 15 May, 2008
Posts: 725



Thanked: 64 times
Dream Kudos: 125
My Contributions
Well something you are doing is not allowed
java

// Unhandled exception type IOException
DataAccess rawStockData = new DataAccess("c:\\Beg Java Stuff\\SGC1.txt");


Your constructor
java

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


Perhaps try changing what you are passing the constructor to something else... perhaps "test.txt" ?
User is offlineProfile CardPM
+Quote Post

g00se

RE: Problem With File Address In Class Constructor

20 Nov, 2008 - 08:25 AM
Post #3

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,142



Thanked: 132 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:09AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month