Results 1 to 20 of 20
  1. #1
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default accessing hashtables from another class

    Hi, very very new to Java (this is my first post)!

    I am trying to access a set of Hashtables that I have created in another class but am having real difficulty getting my head around it.

    Data is loaded from a file in the class DataAccess
    Set into a group of Hashtables in Timeseries
    I am then looking to query the group of hash tables in class TimeseriesManipulation, but keep getting a error. All the classes are in the same package.
    I can't figure out how from the Engine class I call class DataManipulation and once I've successfully called the class I don't know the syntax to get data out of the hashtables.

    Can someone help please.
    Please find attched a txt file with all relevant code. (if you cannot access the code please let me know).
    Very many thanks
    Attached Files Attached Files

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you first gone through a basic Java textbook or tutorial series before trying to do this code? From the code I've seen, it looks as if you're trying to fly before you can crawl as there are mistakes that suggest misconceptions of basic core Java such as trying to use objects before they have been declared. It's either that or you have tried to borrow other code but have only done so incompletely. If the former, then I hate to say this but the only solution that I see to this is for you to stop what you are doing and study up on the rudiments of the language. If the latter, then don't do this. Learn how to understand what you are doing before trying to borrow other's code. Again, the tutorials will help you immensely here.
    Last edited by Fubarable; 12-05-2008 at 02:20 PM.

  3. #3
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks for the reply, this is my own code. I've tried reading up and obviously I have not grasped something. Apart from re reading a 1500 word text book in my free time and missing which ever concept i've suppose to have missed again (which you did not make clear) this is my only source help. I am not at college/school or frustratingly work as unemployed. Can someone please help me with a practical solution to my problem please?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I tried compiling your code and found many many errors suggesting that you're breaking one of the first rules of coding: adding good code to bad code. The best solution for this code is what I call the "Old Yeller" treatment.

    Also remember that when coding you should:

    * Only add small amounts of code to a project at a time.
    * Before adding code to a project, compile it first, run it if possible, and make sure that there are no errors first.
    * If you find compile or run-time errors, fix them first before adding any code.
    * Work with smaller classes that are testable in their own right. This often means heavy use of interfaces.

    Good luck.

  5. #5
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    I tried to par the code down so it was easier to read. Please find attched new txt with all the code. The only errors should now be associated with my problem (in the Engine and TimeSeriesManipulation classes).
    Attached Files Attached Files

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    what about the text file?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, describe what each class is supposed to be doing? Where are you comments / javadoc notes?

    Also, here:
    Java Code:
    public class TimeSeriesManipulation
    {
      void addIdTogether(int start, int finish)
      {
        int Total = 0;
        myTimeseries.displayTsRecord("1234561");
        for (int i = start; i < finish; i++)
        {
          String newString = "1234" + i;
          //Total=Total+Timeseries.getSingleId(newString);
          Timeseries.this.getSingleId(newString);
        }
      }
    }
    you're using Timeseries.this.... as if this method were being called from within an instance of a Timeseries object. It's not, and so this has no meaning here. You must have an instance of this object here.

    You're also using an instance, myTimeseries without declaring or instantiating it. Both of these errors go to the core of basic Java programming.

    Edit: similar problems are occuring with the Engine class:
    Java Code:
    import java.io.IOException;
    import java.util.Hashtable;
    
    public class Engine
    {
    
      public static void main(String[] arguments) {
    
        DataAccess tsData = new DataAccess("c:\\PairsData\\timeseries1.txt");
        
        addIdTogether TSMan = new TimeSeriesManipulation(myTimeseries);
       
        System.out.println(Timeseries.class.myTimeseries.getSingleId("1234561"));
        myTimeseries.
        
      }
    }
    addItTogether is a method but you seem to be treating it as a class. How can that work/

    Timeseries.class.myTimeseries ahs no meaning here. Do you instead want to declare and initialize a Timeseries object here and use it?

    Finally, what is "myTimeseries." for?
    Last edited by Fubarable; 12-05-2008 at 03:27 PM.

  8. #8
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    can you see it now (Hashtable access1.txt) attached? If not I'll copy it into the mesage box
    Attached Files Attached Files

  9. #9
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    Engine- this is the main class
    DataAccess - Time series data from text files is accessed and put into some Hashtables
    Timeseries - Gives the hashtables a common theme namily belonging to a type timeseries
    TimeSeriesManipulation - this is where I would like to access the hash tables and manipulate the data

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by SimC View Post
    can you see it now (Hashtable access1.txt) attached? If not I'll copy it into the mesage box
    my bad, I meant the data file, "timeseries1.txt"

  11. #11
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    ahh please see attached
    Attached Files Attached Files

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm still having a great deal of difficulty trying to figure out what you are trying to do here. It seems as if you are trying to store numeric data into a bunch of parallel HashTables. Why? Why not group data that needs to be stored together into a single object and place that object into a HashMap if need be? Or better still use a database which was built to store lots of data?

    Your design may need to be completely rethought here.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Going through some of this. First I see that you are hard-coding your file strings in your program. Don't do this as it's very very bad practice and makes your program hard to maintain and harder for others to try out. For one instance, I will have a different directory structure than you. Also, don't make logical decisions based on file names. What if they're changed in the future?

  14. #14
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    The Timeseries1 file is only one record long, this is just for testing. In reality this file will consist of thousands of day records for thousands of different stocks. I need to be able to query the tables quickly and very often. All the individual data types in the hash tables will eventually be analysed individually. eg beta data for a number of days will be queried, analysed and a result put somewhere.

    I do not have access to database software. Does this help?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    In reality this file will consist of thousands of day records for thousands of different stocks. I need to be able to query the tables quickly and very often.
    Then it's settled. You don't need to re-invent the wheel as the only thing that will work here, and I mean only thing, is a database. Anything else is bound to fail.

    I do not have access to database software.
    now you're talkin' nonsense. We all have access to database software; all you have to do is to download mySQL or some other free database and start learning it.

  16. #16
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    don't worry about that I know about hard coding address, I just did it to get this project running. It will be changed at some point

  17. #17
    SimC is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    no, believe me this has to work without a database (it has to)

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    why is this so?

  19. #19
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Corrections

    Some observations:
    • Do not compare strings using "==".. use the String methods of equal, compare, etc
    Java Code:
    if (address == "c:\\PairsData\\timeseries1.txt")[COLOR="Red"]//wrong !!![/COLOR]
    • The variable eof (at least in the file I was looking at) is initalized as false

    Java Code:
    boolean eof = false;
    but it is never is set to true therefore, the following use of eof serves no purpose:
    Java Code:
    while ((thisLine = buff.readLine()) != null && !eof)
    I'm coming to the conclution that this code is too buggy even start to review it.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm coming to the conclution that this code is too buggy even start to review it.
    I agree, but even more important than the code, what about the premise of using a bunch of hashtables (not even generic HashMaps) in parallel here instead of a stable database?

Similar Threads

  1. Replies: 0
    Last Post: 02-17-2008, 09:03 AM
  2. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  3. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 AM
  4. Replies: 5
    Last Post: 01-03-2008, 07:54 AM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •