CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jan 2012
    Posts
    17

    Reading a File into a 2D Array - Getting ArrayIndexOutOfBoundsException()

    Hi I've been doing Project Euler problems lately but I'm having trouble with Problem 18, here it is:

    By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

    3
    7 4
    2 4 6
    8 5 9 3

    That is, 3 + 7 + 4 + 9 = 23.

    Find the maximum total from top to bottom of the triangle below:

    75
    95 64
    17 47 82
    18 35 87 10
    20 04 82 47 65
    19 01 23 75 03 34
    88 02 77 73 07 63 67
    99 65 04 28 06 16 70 92
    41 41 26 56 83 40 80 70 33
    41 48 72 33 47 32 37 16 94 29
    53 71 44 65 25 43 91 52 97 51 14
    70 11 33 28 77 73 17 78 39 68 17 57
    91 71 52 38 17 14 91 43 58 50 27 29 48
    63 66 04 68 89 53 67 30 73 16 69 87 40 31
    04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

    NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

    My algorithm for finding the max total is correct. I tested it by manually entering the numbers in a 2d array and the program ran fine. The reason I don't want to do it that way is as this Problem says Problem 67 is the same except much larger numbers and I don't want to be typing numbers all day and also I want to practice manipulating files etc.

    Anyway to the point I suppose the best thing I can do really is show you my code and the errors I'm getting. I did some debugging but I can't seem to figure out why the problem is occurring. When I run the program it gives an ArrayIndexOutOfBoundsException(). The .txt file consists of the large group of numbers in the problem description above.

    Code:
    public class Problem18 {
    
    	public static void main(String[] args) throws Exception
    	{
    		Problem18 p18 = new Problem18() ;
    		p18.maxTotalPath() ;
    	}
    	
    	public int[][] readFile() throws Exception
    	{
    		ClassLoader loader = Thread.currentThread().getContextClassLoader() ;
        	InputStream file = loader.getResourceAsStream("Triangle.txt") ;
        	
        	Scanner scan = new Scanner(file) ;
        	
        	int[][] triangle = new int[15][15] ;
        	int m = 0, n = 0 ;
        	String line ;
        	while(scan.hasNext())
        	{
        		line = scan.nextLine() ;
        		String[] numbers = line.split(" ") ;
        	
        		for(int i = 0 ; i < numbers.length ; i++)
        		{
        			triangle[m][n] = Integer.parseInt(numbers[i]) ;
        		    //System.out.print(triangle[m][n] + " ") ;
        			n += 1 ;
        		}
        		n = 0 ;
        		// System.out.println("") ;
        		m += 1 ;
        	}
        	scan.close() ;
        	return triangle ;
    	}
    	
    	public void maxTotalPath() throws Exception
    	{
    		int[][] arr = readFile() ;
    		for (int i = arr.length - 2 ; i >= 0 ; i--) 
    		{
    			for (int j = 0 ; j < arr[i].length; j++) 
    			{
    				arr[i][j] += Math.max(arr[i + 1][j], arr[i + 1][j + 1]); 
    			}
    		}
    		System.out.println(Integer.toString(arr[0][0])) ;
    	}
    }
    The errors then are:

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
    	at com.jconnolly.projeuler.problems.Problem18.maxTotalPath(Problem18.java:83)
    	at com.jconnolly.projeuler.problems.Problem18.main(Problem18.java:44)
    Any help would be much appreciated thanks!
    Last edited by Schonge; July 31st, 2013 at 03:17 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
  •  





Click Here to Expand Forum to Full Width

Featured