• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading multiple Cvs files sequentially(open,close,high,low) and calculating Moving Average Converge

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please i need help to read through a CVSfile sequentially and calculating the MACD. what i have is not working, i will appreciate any correction. REQUIREMENT

"provides a convenient means of downloading an investment's history consisting of the following fields: Ticker, Date, Open, High, Low, Close, and Volume.

Download a year's history for each of the following investments into separate .csv files:
  • F Ford IPG Interpublic
    Grp Cos
    RTN Raytheon Co
    SYMC Symantec Corp
    TWX Time Warner
    TJX TJX Companies
    VSH Vishay Intertechnolog...
    IBM International Business Machine
  • s.
    Reading each file sequentially, use string split or the tokenizer with a comma as a delimiter to parse each of the .csv files into fields. Use each investment's history fields to determine the Moving Average Convergence Divergence (MACD) for each of the investments as of 4/1/2016 per"

    I am stalk here.....

     
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You have too much going on in main and too much repetition.

    I suggest creating an Investment class with the following fields: Ticker, Date, Open, High, Low, Close, and Volume. Provide a constructor that takes a single line of CSV data, breaks it down, and populates the fields.

    Then create a method in your main class to read a file and create a List of Investment's. You don't need one for each investment ticker, you can reuse this. Do away with all the BufferedReader's that you have now. You need only one inside your read() method.
     
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    Thank you for using code tags, but you haven't indented your code correctly. If you break a line, the second part should be indented more to the right than the first part (most people would use two additional indents), and don't double‑space your code.
    CB is right. You are not writing object‑oriented code there. You should have an Investment class and I agree about putting Investment objects into a List. He is right that you need one method to read the files, which you can do with a buffered reader or with a Scanner. Another thing is that you absolutely must close the file reading object. Try with resources is probably the best way to close it.
    Look in the documentation for StringTokenizer and it tells you,

    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.

    You should therefore consider using String#split instead. I would prefer to use a Scanner because that will give the values directly as doubles or some other type directly, but I suspect that is not in the spirit of using tokenizers or String#split. Try ",\\s*" or "\\s*,\\s*" as the regex to split on. The meaning of the comma should be obvious; I think it is not a meta‑character. The \s bit means whitespace and the * means any number (including 0) of repetitions. You have to escape the \ so it turns into \\. I am not an expert on regexes, so test that regex carefully with String#split before you let it loose on an unsuspecting world.

    Somebody else has a similar problem, which you will find discussion of on this forum.
     
    Adey Fater
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i dont understand what you mean pLease. any example?
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adey Fater wrote:i dont understand what you mean pLease. any example?

    You know how to make a class? Make one called Investment. You know how to add fields? Add the ones I mentioned. Post what you have so far and let us know where you're stuck.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I notice that the path names to your CSV files are not consistent. Is this intentional?
     
    Adey Fater
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    // making class investment and adding fields.

    public class Investment {





    }
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adey Fater wrote:


    Good. Now create a constructor that takes a single String as a parameter. This parameter will be one row from a CSV file. Use split to extract the necessary fields. Then show us again.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Fields are almost always declared 'private'.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You have declared all the fields to be Strings. This is ok temporarily but you will have to change the applicable ones to doubles and Dates. Maybe someone could point you at one of the new date objects to use.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Could you post a cut-n-paste of a few lines of one of your CSV files? We need to see what the format looks like.
     
    Adey Fater
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ford = new ford();
    Interrepublic = new interrepublic();
    Raytheon = new raytheon();
    Symantec = new symantec();
    TimeWarner = new timeWarner();
    TjxComp = new tjxComp();
    Vishay = new vishay();
    InterBusiness = new interBusiness();


    String stringLine = "";

    String result = stringLine(stringLine.indexOf("[") + 1, stringLine.indexOf("]"));



    }

     
    Adey Fater
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The CVS file looks like this......

    Date Open High Low Close Volume
    19-Apr-16 13.35 13.5 13.28 13.44 28793398
    18-Apr-16 12.98 13.28 12.95 13.25 30105551
    15-Apr-16 13.1 13.12 12.85 12.94 22532984
    14-Apr-16 13.09 13.18 13.02 13.09 22422565
    13-Apr-16 12.87 13.12 12.85 13.06 29324137
    12-Apr-16 12.72 12.84 12.66 12.81 23386607
    11-Apr-16 12.61 12.8 12.58 12.66 27653958
    8-Apr-16 12.62 12.78 12.51 12.55 20003807
    7-Apr-16 12.77 12.79 12.39 12.52 37373470
    6-Apr-16 12.75 12.87 12.65 12.82 22586290
    5-Apr-16 12.73 12.85 12.52 12.77 32265978
    4-Apr-16 13.11 13.12 12.76 12.8 42322760
    1-Apr-16 13.28 13.33 13.05 13.1 57856805
    31-Mar-16 13.34 13.52 13.34 13.5 32919961
    30-Mar-16 13.28 13.44 13.24 13.35 25278172
    29-Mar-16 13.05 13.2 12.97 13.2 26055638
    header1header2
    cell 1cell 2
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:Good. Now create a constructor that takes a single String as a parameter. This parameter will be one row from a CSV file. Use split to extract the necessary fields. Then show us again.

    Adey Fater wrote:

    Well, you are now calling a bunch of constructors that you haven't created. I was asking you to make (not call) a single constructor inside of your Investment class.

    You don't need the indexOf, when we get back to that point you'll use BufferedReader and its method readLine().
     
    Adey Fater
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    okay...

     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adey Fater wrote:okay...

    Where's the constructor you made?
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Working towards something like
    But line 15 needs an Investment constructor in order to work.
     
    Bartender
    Posts: 242
    27
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A constructor method is a method inside a specific class, that when called, returns an instance of that class. Look at your BufferedReader code:

    In here, you are calling the BufferedReader constructor (by typing new BufferedReader...). This returns a BufferedReader instance which you can then do things with,

    Now take a look at this code:

    Here, you're trying to call the constructor on a class named "ford". But that class does not exist.

    Carey wasn't asking you to call an already existing constructor, but to write one for the class "Investment" you made. After writing this constructor you could be able to do the following:


    The constructor will be a member inside of the Investment class. There are some rules to writing a constructor:
  • The name of the constructor must be identical to the class name
  • There is no return type on a constructor (at least, not like on other methods)
  • Constructors should set the initial values of all class instance variables, where needed

  • So in your case, a constructor would look like this:


    Constructors can have arguments or not have them. In this case, Carey recommended you take a String argument with the constructor and use it to initiate all the instance variables.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Zachary Griggs wrote:A constructor method is a method . . .

    Don't call constructors methods; the two are different. You cannot call a constructor apart from part of the object creation process, and constructors are not members of the class.
    Unfortunately it is possible to write a method which looks like a constructor, as your warnings illustrate. A lot of us think that should not have been permitted, but there is nothing you can do to change it now. But if classes start with CapitalLetters and methods start lowerCase, there should in theory be some protection against such mistakes.


    The constructor will be a member inside of the Investment class. There are some rules to writing a constructor:

  • The name of the constructor must be identical to the class name
  • There is no return type on a constructor (at least, not like on other methods)
  • Constructors should set the initial values of all class instance variables, where needed
  • . . .

    Agree about initialising all the instance variables. Good point there
     
    Adey Fater
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i HAVE THIS NOW
    But line 15 needs an Investment constructor in order to work.[/code]
     
    Zachary Griggs
    Bartender
    Posts: 242
    27
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Campbell, I don't think calling a constructor a method is inaccurate - it's just a special case. The constructors do belong to a specific class (but they aren't members of objects of that class). I always like thinking of constructors as a specialized static method that allocates memory for a new object of the class type and returns it, without forcing the programmer to do any of that work. In fact in one of the other languages I use, constructors are declared like "public static method create takes nothing returns thistype"

    @Adey, you still have not written the constructor for the Investment class. Without a constructor, nothing will give those String fields a value, and they will just be null. Your constructor declaration must be written in the Investment class like:

    There are also problems with the read function, but let's work on the constructor first.

    (edit) And you should really stop copy/pasting code written here. The code people write here is just to get you on the right track, not to write the program for you; not only will you not learn anything by doing this, but you'll end up with nonsense code.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Zachary Griggs wrote:. . . in one of the other languages I use, constructors are declared like "public static method create takes nothing returns thistype"

    That sounds like a factory method, which is common in Java® too, but different languages do different things and feature X in language Y is often not a direct analogue of feature Z in language W.

    . . .And you should really stop copy/pasting code written here. . . . you'll end up with nonsense code.

    You will also get a 0 if you plagiarise code. If anybody copies code, the original will be here for all to see.
     
    Nothing up my sleeve ... and ... presto! A tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic