Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default More Array Troubles

    So I am back again with more problems. First, what I have working is the ReadFiles class, but now I have a new class (Calculations) and I am trying to simply take the data from one column in my file. As I have it now it outputs null several times and then an IndexOutOfBoundsException. Any help would be very greatly appreciated.

    Java Code:
    public class DataAnalyzer
    {
      public static void main (String[] args)
      {
        //This creates an instance of ReadFiles
        ReadFiles aReadFiles = new ReadFiles();
        Calculations aCalculations = new Calculations();
        
        //This calls the method fileCheck in the ReadFiles Class
        aReadFiles.fileCheck();
        
        //This calls the method findRows in the ReadFiles Class
        aReadFiles.findRows();
        
        //This calls the method convertFile in the ReadFiles Class
        aReadFiles.convertFile();
        
        //This calls the method printArray in the ReadFiles Class
        //aReadFiles.printArray();
        
        //This calls the method createDateArray in the Calculations Class
        aCalculations.createDateArray();
      }
    }
    Java Code:
    import java.nio.file.*;
    import static java.nio.file.AccessMode.*;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.StringTokenizer;
    
    
    public class ReadFiles
    {
      public static int numberOfRows;
      public static int rowNumber = 0;
      public static int columnNumber = 0;
      public static int newRowNumber = 0;
      public static int newColumnNumber = 0;
      public String [][] myFBfile = new String [435][8];
    
      //This method attempts to read a file to ensure that it is available to be used by the rest of the program.
      public void fileCheck()
      {
        Path filePath = Paths.get("C:\\temp\\FB.csv");
        System.out.println("Path is " + filePath.toString());
        
        try
        {
          filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
          System.out.println("File can be read and executed");
        }
        
        catch (IOException e)
        {
          System.out.println ("File cannot be used for this application");
        }
      }
      
      //This method reads a file, then tokenizes it to find the number of rows in the file.
      public void findRows()
      {
        String filePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(filePath));
          StringTokenizer st = null;
          System.out.println("Your file is being analysed. This may take several minutes.");
    
          while((filePath = br.readLine()) != null)
          {
            rowNumber++;
                    
            st = new StringTokenizer(filePath, ",");
            columnNumber = 0;
          }
        }
        
        catch (FileNotFoundException e)
        {
          System.out.println ("Number of rows for this file cannot be found");
        } 
        
        catch (IOException e) 
        {
          System.out.println ("Number of rows for this file cannot be found");
        }
            
        System.out.println("Total Rows: " + (rowNumber));
      }
      
      //This method converts the file into an array to be used by the rest of the program.
      public void convertFile()
      {
        String convertFilePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        //The fields for the file are: Date,Open,High,Low,Close,Volume,Adj Close
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(convertFilePath));
          StringTokenizer st = null;
          System.out.println("Your file is being converted. This may take several minutes.");
    
          while((convertFilePath = br.readLine()) != null)
          {
            newRowNumber++;
    
            st = new StringTokenizer(convertFilePath, ",");
    
            while(st.hasMoreTokens())
            {
              newColumnNumber++;
              myFBfile[newRowNumber][newColumnNumber] = st.nextToken();
              //this is a test to make sure the array is working properly
              //System.out.println (newRowNumber +" "+ newColumnNumber +" "+ myFBfile[newRowNumber][newColumnNumber]);
            }
            newColumnNumber = 0;
          }
        }       
        catch (FileNotFoundException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        } 
        catch (IOException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        }
        
      }
      
      //This method prints out all of the data from the array
      public void printArray()
      {
        for (int x = 1; x <= 434; x++)
        {
          for(int y = 1; y <= 7; y++)
          {
            System.out.print (x +" "+ y +" "+ myFBfile[x][y]);
          }
          int y = 1;
          System.out.println();
        }
      }
      
      //This method returns the array
      public void returnArray()
      {
        //return myFBfile;
      }
    }
    Java Code:
    import java.nio.file.*;
    import static java.nio.file.AccessMode.*;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.StringTokenizer;
    
    public class Calculations extends ReadFiles
    {
      public String []dateValue = new String [435];
      public float []openValue = new float[435];
      public float []highValue = new float[435];
      public float []lowValue = new float[435];
      public float []closeValue = new float[435];
      public float []volumeValue = new float[435];
      public float []adjCloseValue = new float[435];
    
      public void createDateArray()
      {
        int x = 1;
    
        //Date [ ] dateArray = new Date[r.findRowNumber()];
        String filePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        //The fields for the file are: Date,Open,High,Low,Close,Volume,Adj Close
         
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(filePath));
          StringTokenizer st = null;
          System.out.println("Your file is being read to find dates. This may take several minutes.");
    
          while((filePath = br.readLine()) != null)
          { 
            st = new StringTokenizer(filePath, ",");
            
            while(st.hasMoreTokens())
            {
              newColumnNumber++;
              
              dateValue[x] = myFBfile[1][x];
              //this is a test to make sure the array is working properly
              System.out.println (dateValue[x]);
              x++;
            }
          }
        }
        
        catch (FileNotFoundException e)
        {
          System.out.println ("Number of rows for this file cannot be found");
        }
        
        catch (IOException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        }
      }
      
      public void createDataArray()
      {
        
      }
     
      public void printArray()
      {
       
      }
     
      public void performCalculations()
      {
       
      }
      
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    it outputs null several times and then an IndexOutOfBoundsException
    Please copy the full contents of the console from when you execute the program and paste it here. It should show what you are talking about.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    The compiler output is as follows:
    > run DataAnalyzer
    Path is C:\temp\FB.csv
    File cannot be used for this application
    Your file is being analysed. This may take several minutes.
    Total Rows: 434
    Your file is being converted. This may take several minutes.
    Your file is being read to find dates. This may take several minutes.
    null
    null
    null
    null
    null
    null
    null
    java.lang.ArrayIndexOutOfBoundsException: 8
    at Calculations.createDateArray(Calculations.java:41)
    at DataAnalyzer.main(DataAnalyzer.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
    >

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    java.lang.ArrayIndexOutOfBoundsException: 8
    at Calculations.createDateArray(Calculations.java:41)
    At line 41 the program used an index to an array that was past the end of the array. Remember array index values range from 0 to the array length -1.
    Look at line 41 and see why it used an index past the end of the array. The array has less than 9 elements in it. The index: 8 is past the end.

    Suggestion: Printing out the value of an array element would be more useful if the array index was also printed so you know what element you are looking at.
    Last edited by Norm; 03-20-2014 at 03:07 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    I have tried playing with the code and I think what I have now is better
    Java Code:
    { 
              dateValue[x] = myFBfile[0][x];
              //this is a test to make sure the array is working properly
              System.out.println (dateValue[x]);
              x++;
            }
    But I still get the same error. I have looked over all my array declarations and still not understand how the dateValue array or myFBfile array is limited to 9 pieces of information.???

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    myFBfile array is limited to 9
    Look at where it is defined:
    Java Code:
      public String [][] myFBfile = new String [435][8];
    The range of the first index is 0 to 434
    The range of the second index is 0 to 7
    For the second index a value of 8 is past the end.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    Ohhh. Ok so I have made a fairly critical mistake. So I have changed to the code to:
    Java Code:
    dateValue[x] = myFBfile[x][1];
    But it is now printing out null 435 times. Am I mistaken in that the value of an array variable can be extended (passed onto) a separate class?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    the value of an array variable can be extended (passed onto) a separate class
    Not sure what that means.
    There are no restrictions on passing the value of a variable from one class to another.

    Can you post the full code that shows what you are talking about?

    A suggestion for testing: Use a smaller value for the arrays' size than 435.
    Define an int variable with the size to be used and use that variable when the arrays are defined:
    Java Code:
      final  int TheSize = 44;    // define the size of the arrays
    
      public String []dateValue = new String [TheSize];
      public float []openValue = new float[TheSize];
      public float []highValue = new float[TheSize];
      etc
    Use the array property: .length to get the size of the array instead of hardcoding a number:
    Java Code:
      for (int x = 1; x < theArray.length; x++)
    Last edited by Norm; 03-20-2014 at 08:13 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    Ok, so I have looked and looked but cannot figure out why the array will not extend to the second class.

    Java Code:
    public class DataAnalyzer
    {
      public static void main (String[] args)
      {
        //This creates an instance of ReadFiles
        ReadFiles aReadFiles = new ReadFiles();
        Calculations aCalculations = new Calculations();
        
        //This calls the method fileCheck in the ReadFiles Class
        aReadFiles.fileCheck();
        
        //This calls the method findRows in the ReadFiles Class
        aReadFiles.findRows();
        
        //This calls the method convertFile in the ReadFiles Class
        aReadFiles.convertFile();
        
        //This calls the method printArray in the ReadFiles Class
        //aReadFiles.printArray();
        
        //This calls the method createDateArray in the Calculations Class
        aCalculations.createDateArray();
      }
    }
    Java Code:
    import java.nio.file.*;
    import static java.nio.file.AccessMode.*;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.StringTokenizer;
    
    
    public class ReadFiles
    {
      public static int numberOfRows;
      public static int rowNumber = 0;
      public static int columnNumber = 0;
      public static int newRowNumber = 0;
      public static int newColumnNumber = 0;
      public String [][] myFBfile = new String [435][8];
    
      //This method attempts to read a file to ensure that it is available to be used by the rest of the program.
      public void fileCheck()
      {
        Path filePath = Paths.get("C:\\temp\\FB.csv");
        System.out.println("Path is " + filePath.toString());
        
        try
        {
          filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
          System.out.println("File can be read and executed");
        }
        
        catch (IOException e)
        {
          System.out.println ("File cannot be used for this application");
        }
      }
      
      //This method reads a file, then tokenizes it to find the number of rows in the file.
      public void findRows()
      {
        String filePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(filePath));
          StringTokenizer st = null;
          System.out.println("Your file is being analysed. This may take several minutes.");
    
          while((filePath = br.readLine()) != null)
          {
            rowNumber++;
                    
            st = new StringTokenizer(filePath, ",");
            columnNumber = 0;
          }
        }
        
        catch (FileNotFoundException e)
        {
          System.out.println ("Number of rows for this file cannot be found");
        } 
        
        catch (IOException e) 
        {
          System.out.println ("Number of rows for this file cannot be found");
        }
            
        System.out.println("Total Rows: " + (rowNumber));
      }
      
      //This method converts the file into an array to be used by the rest of the program.
      public void convertFile()
      {
        String convertFilePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        //The fields for the file are: Date,Open,High,Low,Close,Volume,Adj Close
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(convertFilePath));
          StringTokenizer st = null;
          System.out.println("Your file is being converted. This may take several minutes.");
    
          while((convertFilePath = br.readLine()) != null)
          {
            newRowNumber++;
    
            st = new StringTokenizer(convertFilePath, ",");
    
            while(st.hasMoreTokens())
            {
              newColumnNumber++;
              myFBfile[newRowNumber][newColumnNumber] = st.nextToken();
              //this is a test to make sure the array is working properly
              //System.out.println (newRowNumber +" "+ newColumnNumber +" "+ myFBfile[newRowNumber][newColumnNumber]);
            }
            newColumnNumber = 0;
          }
        }       
        catch (FileNotFoundException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        } 
        catch (IOException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        }
        
      }
      
      //This method prints out all of the data from the array
      public void printArray()
      {
        for (int x = 1; x <= 434; x++)
        {
          for(int y = 1; y <= 7; y++)
          {
            System.out.print (x +" "+ y +" "+ myFBfile[x][y]);
          }
          int y = 1;
        }
      }
      
      //This method returns the array
      public void returnArray()
      {
        //return myFBfile;
      }
    }
    Java Code:
    public class Calculations extends ReadFiles
    {
      public String []dateArray = new String [435];
      public float [][]dataArray = new float[435][7];
    
      public void createDateArray()
      {     
        try
        {
          System.out.println("Your file is being read to find dates. This may take several minutes.");
          for (int x = 0; x < dateArray.length; x++)
          {
            dateArray[x] = myFBfile[x][0];
            //this is a test to make sure the array is working properly
            System.out.println (dateArray[x]);
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
      }
      
      public void createDataArray()
      {
        
      }
     
      public void printArray()
      {
       
      }
     
      public void performCalculations()
      {
       
      }
      
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    why the array will not extend to the second class.
    Can you explain?
    What does extend mean?
    Which array are you talking about?
    What is the name of the second class?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    The class ReadFiles is the class that creates an array to hold the data from a file in the method convertFile. I am then using the 'extends' command to extend the variables to the Calculations class. I then want to use the array myFBfile that is created in ReadFiles in the Calculations class.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    The code creates separate instances of the two classes. The data in one instance will NOT be available in the other instance.
    Do NOT create a separate instance of the ReadFiles class. Use the Calculations class's super version of ReadFiles.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    Quote Originally Posted by Norm View Post
    The code creates separate instances of the two classes. The data in one instance will NOT be available in the other instance.
    Do NOT create a separate instance of the ReadFiles class. Use the Calculations class's super version of ReadFiles.
    I have to be honest in that I do not really understand your advice. My understanding of "Super" is that is overrides the existing method. I do not want to override (do I??), I want to use the data in one class in a second class that extends the first. I thought that if you extend a class, that the child class inherits all of the variables of the first?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    if you extend a class, that the child class inherits all of the variables of the first
    Yes that is what happens. All the variables and methods of both classes are in the ONE object that was created.
    If there is another object created, NONE of the values in that instance will be available in the the first object that was created.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    Ok. I am really sorry, but I am confused. I feel like what I have done should be correct but are you indicating that I should call the convertArray method a second time in the child class? I guess I just do not understand why the myFBfile (which is the array that holds all of the data does not carry over from the ReadFiles class to the Calculations class??

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    Create ONE instance of Calculations class and use its methods. All of the ReadFiles class's methods are there.
    Do NOT create an instance of the ReadFiles class.

    myFBfile (which is the array that holds all of the data does not carry over from the ReadFiles class to the Calculations class
    If you have two objects and put data into one object, that data is NOT in the other object.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    Quote Originally Posted by Norm View Post
    Create ONE instance of Calculations class and use its methods. All of the ReadFiles class's methods are there.
    Do NOT create an instance of the ReadFiles class.


    If you have two objects and put data into one object, that data is NOT in the other object.
    Ok, then that solves my problem I guess. What I have been trying to do just does not work. I will need to read the data from the file again rather than use the information from the myFBfile array.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    Have you tried using the ReadFiles methods that are included with the Calculations class object? Use the Calculations object to call the ReadFiles methods.
    That is what "extends" means. All of the ReadFiles stuff is in the Calculations class.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    do you mean call the method convert file in the Calculations class? Do I do that the same way as when I called the methods in DataAnalyzer class?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: More Array Troubles

    Have you tried it yet? You can change it back if it doesn't compile.

    In the main() method:
    Use the Calculations object to call the ReadFiles methods.
    Do NOT create a ReadFiles object.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Array Troubles
    By shodai in forum New To Java
    Replies: 28
    Last Post: 02-17-2014, 12:15 AM
  2. Array Troubles
    By shodai in forum New To Java
    Replies: 2
    Last Post: 02-11-2014, 06:43 PM
  3. Troubles with Vector
    By Outsider418 in forum AWT / Swing
    Replies: 8
    Last Post: 05-21-2012, 01:10 PM
  4. Textpad troubles
    By Sthassaris in forum Other IDEs
    Replies: 3
    Last Post: 06-03-2011, 10:38 PM
  5. [SOLVED] Array troubles, yes I searched...
    By Reiyn in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 12:28 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
  •