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

    Default Everything is Out of Bounds

    Ok, so I have received a lot of great help from the forum, but now I have realized that I have a serious error that is occurring over and over again in the program. It seems that all of my arrays in the class Calculations are out of bounds, and I do not seem to know why. Any ideas (or obvious observations)?

    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 printArray in the ReadFiles Class
        //aReadFiles.printArray();
        
        //This calls the method convertFile in the ReadFiles Class through the Calculations Class
        aCalculations.convertFile();
    
        //This calls the method createDateArray in the Calculations Class    
        aCalculations.createDateArray();
        
        //This calls the method createDataArray in the Calculations Class
        aCalculations.createDataArray();
        
        //This calls the method printArray in the Calculations Class
        //aCalculations.printArray();
        
        //This calls the method performCalculations in the Calculations Class
        aCalculations.performCalculations();
      }
    }
    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 [430][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:\\Users\\Brad\\Downloads\\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)
          {
    
            
            st = new StringTokenizer(convertFilePath, ",");
    
            while(st.hasMoreTokens())
            {
              myFBfile[newRowNumber][newColumnNumber] = st.nextToken();
              newColumnNumber++;          
              //this is a test to make sure the array is working properly
              //System.out.println (newRowNumber +" "+ newColumnNumber +" "+ myFBfile[newRowNumber][newColumnNumber]);
            }
            newRowNumber++;
            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");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
        
      }
      
      //This method prints out all of the data from the array
      public void printArray()
      {
        for (int x = 0; x <= rowNumber; x++)
        {
          for(int y = 0; y <= 7; y++)
          {
            System.out.print (x +" "+ y +" "+ myFBfile[x][y]);
          }
          //int y = 0;
        }
      }
      
      //This method returns the array
      public void returnArray()
      {
        //return myFBfile;
      }
    }
    Java Code:
    public class Calculations extends ReadFiles
    {
      public String []dateArray = new String [431];
      public float [][]dataArray = new float[431][7];
      public float highestOpen = 0;
      public float highestClose = 0;
      public float lowestOpen = 100000000;
      public float lowestClose = 100000000;
      public float []highValue = new float[10];
      public float []lowValue = new float[10];
      public float []difference = new float[myFBfile.length];
    
    
      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 <= myFBfile.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()
      {
        try
        {
          System.out.println("Your file is being read to organize information for processing. This may take several minutes.");
          for (int x = 0; x <= 431; x++)
          {
            for (int y = 1; y <= 6; y++)
            {
              dataArray[x][y] = Float.parseFloat(myFBfile[x][y]);
              //this is a test to make sure the array is working properly
              //System.out.println (dataArray[x][y]);
            }
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
      }
     
      public void printArray()
      {
        try
        {
          for (int x = 0; x <= dateArray.length; x++)
          {
            System.out.print(dateArray[x]);
            for (int y = 1; y <= 6; y++)
            {
              System.out.print (" " + dataArray[x][y]);
            }
            System.out.println();
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
      }
      
     
      public void performCalculations()
      {
        
        //find the highest opening value
        for (int x = 1; x <= dataArray.length; x++)
        {
          if (highestOpen < dataArray[x][1])
          {
            highestOpen = dataArray[x][1];
          }
        }
        System.out.println (highestOpen);
      }
        //find the highest closing value
        
        //find the lowest opening value
        
        //find the lowest closing value
        
        //find the top ten highest values
        
        //find the top ten lowest values
        
        //create a file for the difference in opening and closing values
        
      }
      
    //}

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

    Default Re: Everything is Out of Bounds

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Why does the code create an instance of ReadFiles?
    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: Everything is Out of Bounds

    The error message is as follows

    java.lang.ArrayIndexOutOfBoundsException: 430
    at Calculations.createDateArray(Calculations.java:26)
    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)

    I am not sure what you mean by creating an instance of read files?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Everything is Out of Bounds

    You are still creating an instance of ReadFiles to read in the values and then trying to use them by creating an instance of
    Calculations. DO NOT DO THAT!! Just use the calculations instance. This was mentioned in the other thread.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Everything is Out of Bounds

    Java Code:
    MyClass foo = new MyClass();
    
    // foo is an instance of MyClass.
    
    MyClass foo1 = new MyClass();
    
    //  foo1 is a different instance of MyClass.
    The two instances, foo and foo1, are totally unrelated (internally that is).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Everything is Out of Bounds

    Quote Originally Posted by jim829 View Post
    You are still creating an instance of ReadFiles to read in the values and then trying to use them by creating an instance of
    Calculations. DO NOT DO THAT!! Just use the calculations instance. This was mentioned in the other thread.

    Regards,
    Jim
    I think that I have made the correct adjustment to this issue???
    Java Code:
    /**
     * DataAnalyzer Class
     * This class instantiates the methods from the ReadFiles Class and Calculations Class.
     * @author Brad Strassburger
     * @version 1 March 20, 2014
     */
    public class DataAnalyzer
    {
      public static void main (String[] args)
      {
    
        Calculations aCalculations = new Calculations();
        
        //This calls the method fileCheck in the ReadFiles Class
        aCalculations.fileCheck();
        
        //This calls the method findRows in the ReadFiles Class
        aCalculations.findRows();
        
        //This calls the method printArray in the ReadFiles Class
        //aReadFiles.printArray();
        
        //This calls the method convertFile in the ReadFiles Class through the Calculations Class
        aCalculations.convertFile();
        
        //This calls the method createDateArray in the Calculations Class    
        aCalculations.createDateArray();
        
        //This calls the method createDataArray in the Calculations Class
        aCalculations.createDataArray();
        
        //This calls the method printArray in the Calculations Class
        //aCalculations.printArray();
        
        //This calls the method performCalculations in the Calculations Class
        aCalculations.performCalculations();
      }
    }
    But this does not seem to help with the out of bounds issue

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Everything is Out of Bounds

    You may indeed have other issues with the code. But you are now on the correct path to getting it working.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Everything is Out of Bounds

    the out of bounds issue
    Please copy the full text of the error message and paste it here. It has important info about the error.

    @jim829 I think you finally got the OP to get rid of the ReadFiles object.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Everything is Out of Bounds

    Quote Originally Posted by Norm View Post
    @jim829 I think you finally got the OP to get rid of the ReadFiles object.
    The proverbial straw. A number of us were pushing the OP in that direction.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Everything is Out of Bounds

    Quote Originally Posted by jim829 View Post
    The proverbial straw. A number of us were pushing the OP in that direction.

    Regards,
    Jim
    I finally got things working. The error was more systemic than I realized. I understand now why you indicated that I should work through one instance. The last part of this is sorting the whole list so I can identify highs and lows. Hopefully it will not be too painful.

    Thank you again.

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Everything is Out of Bounds

    Uh, you don't need to sort the list to identify highs and lows. Think about it. You have a list of unsorted numbers on a sheet of paper. You look at the first one. You have no others so its both high and low. You look at the next. If it's higher, you have a new high. If it's lower you have a new low. Then continue on in that fashion.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Everything is Out of Bounds

    That works for just finding the highest and lowest values. But I need to find the 10 highest and 10 lowest. I think that the easiest way to approach it is to sort the entire list?? There is a problem as there is a parallel list of dates that corresponds to the numbers, so I am not sure how to handle that??

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Everything is Out of Bounds

    IMO you're correct - sorting is the easiest solution to solve that particular requirement.

    There is a problem as there is a parallel list of dates that corresponds to the numbers, so I am not sure how to handle that??
    Well is there a real problem here? You can just leave the original list of unsorted numbers as it is and copy it to a second list which you sort.

    Or better perhaps: store everything in one list; create a class in which you can store both the number and the date and then stick objects of that class in your list.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: Everything is Out of Bounds

    I feel certain there is some algorithm that would let you find the N highs and lows of an arbitrary list of unsorted data whose size is >= 2*N. However, I cannot think of one at the moment. So sorting is clearly an option. It just seems like a waste of cycles for large data sets and relatively small values of N.

    Hmmm. Idea. Perhaps you can have two auxiliary lists. As you find a high value you add it to the high list if the high is higher than the lowest value in that list and the list size is < N. If the list == to N you simply replace the lowest value with your new found highest. The process is reversed for the other case. I don't know if this will work or not and it seems like a lot of possible iterations. But if it does it would be interesting to see how it compares to the sorting method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Everything is Out of Bounds

    Supply and demand friend. He was asking for the easiest solution, not the most CPU efficient one ;) A SortedSet or TreeMap would be my in-between solution; don't sort afterwards, just store sorted to begin with.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Everything is Out of Bounds

    Ok, so the sorting thing is over my head right now, so I thought that I would work on another part of the program; writing to a file. Unfortunately I have tried several different methods and none seem to work. This is the code for the section of the program

    Java Code:
          //create a file for the difference in opening and closing values
          FileWriter outFile = new FileWriter ("C:\\Users\\Brad\\Downloads\\NewFBFile");
          PrintWriter out = new PrintWriter(outFile);
          
          for (int x = 0; x < 431; x++)
          {
            
            difference = Math.abs(dataArray[x][1] - dataArray[x+1][4]);
            differenceArray[x] = difference;
            
            //this is a test to see results and if the formula is working.
            //System.out.println("the difference between " + dataArray[x][1] + " and " + dataArray[x+1][4] + " is " + difference);
            //RESULT: kind of working....I think
    
            record = (dateArray[x] + " " + dataArray[x][1] + " " + dataArray[x][2]
                        + " " + dataArray[x][3] + " " + dataArray[x][4] + " " + dataArray[x][5] + " " + dataArray[x][6]
                        + " " + difference + System.getProperty("line.separator"));
            //this is a test to see if the output is working
            //System.out.println (record);
            out.println(record); 
            
          }
          out.close();
        }
    Is there something obvious that I am missing?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Everything is Out of Bounds

    none seem to work
    Please explain.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Everything is Out of Bounds

    I get the following error for line 2:


    1 error and 2 warnings found:
    -------------
    *** Error ***
    -------------
    File: C:\Users\Brad\Downloads\Assignment 3\Calculations.java [line: 159]
    Error: Unhandled exception type java.io.IOException
    --------------

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Everything is Out of Bounds

    Unhandled exception type java.io.IOException
    The statements need to be enclosed in a try{} catch block
    OR the method the statements are in declared to throw that exception
    See the tutorial: The Catch or Specify Requirement (The Java™ Tutorials > Essential Classes > Exceptions)
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Everything is Out of Bounds

    Quote Originally Posted by Norm View Post
    The statements need to be enclosed in a try{} catch block
    OR the method the statements are in declared to throw that exception
    See the tutorial: The Catch or Specify Requirement (The Java™ Tutorials > Essential Classes > Exceptions)
    My understanding is that the try/catch will allow your program to continue without crashing, but it does not actually solve the problem itself. Is that correct, or I am completely off here?

Page 1 of 2 12 LastLast

Similar Threads

  1. Array out of bounds
    By cfc1252 in forum New To Java
    Replies: 5
    Last Post: 01-11-2012, 04:21 PM
  2. My picture is out of bounds?
    By ThatPinkSock in forum New To Java
    Replies: 3
    Last Post: 10-29-2010, 01:40 AM
  3. Bounds problem...
    By licka in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 09:38 PM
  4. array going out of bounds?
    By jabo in forum New To Java
    Replies: 9
    Last Post: 04-02-2010, 10:08 AM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 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
  •