Results 1 to 17 of 17
  1. #1
    khalil is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Good day! can somebody help me out urgently.

    I want to read this csv file and store contents on column basis; as date (single array) for all dates. open, high, low and close and be able to manipulate individual elements of the array such as Sum += close[i];


    public void pratice() throws Exception
    {
    // Create a File instance
    BufferedReader br=new BufferedReader(new FileReader("gtk.csv"));
    content = new String[1000];
    while(content !=null)
    {
    content[++count] = br.readLine();
    }
    int i =1;
    for (int j=1; j < count; j++)
    {

    str[j] = content[j].split(",");
    int i=1;
    datep[j] = str[j][i];
    openp[j] = str[i+1];
    highp[j] = str[i+2];
    lowp[j] = str[i+3];
    closep[j] = str[i+4];
    i++;
    }
    }

    the file looks like this
    date Open high low close
    12-12-2011 15.00 15.40 14.5 15.2
    11-12-2011 15.02 15.70 14.9 15.00
    10-12-2011 14.70 16.00 14.70 15.02
    ...

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Good day! can somebody help me out urgently.

    Hi Khalil, welcome.

    What exactly is the problem you are facing?

    Regards.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Good day! can somebody help me out urgently.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    khalil is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Good day! can somebody help me out urgently.

    content[++count] = br.readLine(); is giving ArrayIndexOutOfBoundsException problem.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Good day! can somebody help me out urgently.

    It's because you are referencing an array index which is beyond those in the array.
    Array indexes start at 0 as opposed to 1, for example an array of size 5 contains the indexes 0, 1, 2, 3, 4.
    The code in you last post attempts to assign each line to an entry in the array which is fine. Your problem though is the value of count is increment end BEFORE it is used instead of after. If you check the content of the array as it is being populated, you will find content[0] does not contain the first value as you expect. This is caused by the position of ++ in relation to the variable.

    Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Read the section on Unary Operators for more details and an idea of how to resolve the issue.

    Regards.

  6. #6
    khalil is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Good day! can somebody help me out urgently.

    I tried to implement that and even break the statement. it is still giving the same error @ br.readLine();

    {
    content[count] =
    br.readLine();
    count++;
    }
    for (int j=0; j < count; j++)
    {
    str = content[j].split(",");
    int i=0;
    datep[j] = str[i];
    openp[j] = str[i+1];
    highp[j] = str[i+2];
    lowp[j] = str[i+3];
    closep[j] = str[i+4];
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Good day! can somebody help me out urgently.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Good day! can somebody help me out urgently.

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    As hinted at in the second link in the second response in this thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Good day! can somebody help me out urgently.

    Check your condition in the while loop.

    Regards.

  10. #10
    khalil is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Good day! can somebody help me out urgently.

    Java Code:
    public void pratice() throws Exception
    {
    // Create a File instance
    BufferedReader br=new BufferedReader(new FileReader("gtk.csv"));
    content = new String[1000];
    while(content[count] !=null)
    {
    content[count] =
    br.readLine();
    count++;
    }
    for (int j=0; j < count; j++)
    {
    str = content[j].split(",");
    int i=0;
    datep[j] = str[i];
    openp[j] = str[i+1];
    highp[j] = str[i+2];
    lowp[j] = str[i+3];
    closep[j] = str[i+4];
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Good day! can somebody help me out urgently.

    The tags are good, but the code has lost all it indentations. Nested statements should be indented.
    The way the code is now, it is hard to see the pairing {}s to see what code is in the while or the for statements.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Good day! can somebody help me out urgently.

    Think about the condition under which you want to terminate the loop.

    Regards.

  13. #13
    khalil is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Good day! can somebody help me out urgently.

    Java Code:
    public void pratice() throws Exception
    {
         // Create a File instance 
         BufferedReader br=new BufferedReader(new FileReader("gtk.csv"));
         content = new String[1000];
         while(content[count] !=null)
                {
                content[count] =
                                       br.readLine();
                count++;
               }
               for (int j=0; j < count; j++)
                         {
                          str = content[j].split(",");
                          int i=0;
                          datep[j] = str[i];
                          openp[j] = str[i+1];
                          highp[j] = str[i+2];
                          lowp[j] = str[i+3];
                          closep[j] = str[i+4];
                          }
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Good day! can somebody help me out urgently.

    To have a safe loop it should test to keep from going past the end of the array. The condition in the while loop can test more than one condition such as when the count is before the end.

    When will the current code exit the while loop?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    khalil is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: Good day! can somebody help me out urgently.

    the loop will end when there is no other line to read again. i even changed the to
    Java Code:
    while (count < content.length)
    the problem still persist. now datep[i] nullpointer

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Good day! can somebody help me out urgently.

    the loop will end when there is no other line to read again
    How is that detected? What is the value of count vs the index when the value from the last readLine() was read into the array?

    What values are in the content array when the loop starts?

    Try debugging the code by adding some println() statements that print out the value of the element of content[] before and after something is read into it.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Good day! can somebody help me out urgently.

    Reading, Writing, and Creating Files.
    Have a look at the while loop in the section titled 'Methods for Unbuffered Streams and Interoperable with java.io APIs'.

    Regards.

Similar Threads

  1. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 03:32 PM
  2. JSP (urgently)
    By G.Ajay Kumar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-19-2009, 05:43 AM
  3. Help!urgently please
    By kevN in forum AWT / Swing
    Replies: 10
    Last Post: 01-09-2009, 05:37 PM
  4. please help urgently
    By ananas7777 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:48 AM

Posting Permissions

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