Results 1 to 9 of 9
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Runtime error in a method

    I get runtime error in a method, I have tired for hours to figure it out but no positive progress so far.

    PHP Code:
    	public static String[][] produceKey () throws Exception
    	{
    		StringBuilder[][] key = new StringBuilder[theChars.length + 2][100];
    		char[] encryptChar = {'', '', '$'};
    
    		//All the "a" characters will be replaced with one of the chars in encryptChar array.
    		for (int q = 0; q < key.length; q++)
    		{
    			for (int w = 0; w < 100; w++)
    				key[q][w] = new StringBuilder ("|aaaaaaaaaa|");
    		}
    
    		//Time to generate the key code!
    		for (int i = 0; i < key.length; i++)
    		{
    			FLoop:
    			for (int j = 0; j < 100; j++)
    			{
    				for (int charPosition = 1; charPosition <= 10; charPosition++)
    				{
    					key[i][j].setCharAt (charPosition, encryptChar[(int) (3 * Math.random())]);
    				}
    
    				if (j == 0 && i == 0)
    					continue FLoop;	//There is nothing to compare with if i and j is zero
    
    				//Here starts a check algoritm, to make sure there is no duplicated code
    				for (int iC = 0; iC < i; iC++)
    				{
    					for (int jC = 0; jC < j; jC++)
    					{
    						if (key[i][j].equals (key[iC][jC]))
    						{
    							j--;
    							continue FLoop;
    						}
    					}
    				}
    			}
    		}
    		//Convert key to a string array
    		String[][] rkey = new String[key.length][100];
    		for (int n = 0; n < rkey.length; n++)
    		{
    			for (int m = 0; m < 100; m++)
    				rkey[n][m] = new String (key[n][m].toString ());
    		}
    
    		//Print the key into a file
    		File fil = new File ("cryptKey.key");
    		PrintWriter fout = new PrintWriter (fil);
    
    		for (int p = 0; p < rkey.length; p++)
    		{
    			for (int o = 0; o < 100; o++)
    				fout.println (rkey[p][o].toString ());
    		}
    		fout.close ();
    
    		//DEBUGGING
    		System.out.println (key[10][5]);
    		System.out.println (key[theChars.length + 2][4]);//HERE IS WHERE THE PROGRAM CRASH
    
    		return rkey;
    	}
    PHP Code:
    	public static char[] theChars =
    					{'!', '\'', '#', '', '%', '&', '/', '(', ')',
    					 '=', '?', '`', '1', '2', '3', '4', '5', '6',
    					 '7', '8', '9', '0', '+', '', '', '', '@',
    					 '', '', '', '{', '[', ']', '}', '\\', 'q',
    					 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
    					 '', '', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U',
    					 'I', 'O', 'P', '', '^', '~', 'a', 's', 'd',
    					 'f', 'g', 'h', 'j', 'k', 'l', '', '', '\'',
    					 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
    					 '', '', '*', '<', 'z', 'x', 'c', 'v', 'b',
    					 'n', 'm', ',', '.', '-', '>', 'Z', 'X', 'C',
    					 'V', 'B', 'N', 'M', ';', ':', '_', '', '',
    					 '', '', '', '', '', '', '', ' ', '	',
    					 '', '', '*', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', '', '', '',
    					 '', '', '', '', '', '', ''};
    And the exception


    theChars arrays length is 205.
    Last edited by Pojahn_M; 08-02-2011 at 01:29 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    key is declared thusly:
    Java Code:
    StringBuilder[][] key = new StringBuilder[theChars.length + 2][100];
    and so it should be no surprise that it crashes with this:
    Java Code:
    System.out.println (key[theChars.length + 2][4]);
    If you declare an array of length n, you can only use indices 0 to (n - 1). Please read a tutorial on arrays to learn more about this.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Don't post a screenshot. People can either not see it (like me, just a red cross) or it is too small and unreadable.

    Copy and paste the full and exact error message and indicate on which line it occurs.

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Don't post a screenshot. People can either not see it (like me, just a red cross) or it is too small and unreadable.

    Copy and paste the full and exact error message and indicate on which line it occurs.
    okey, next time I wont post image(how can you not see photobucket pictures? o_0).
    Its an arrayindexoutofbounds execption.

    Fubarable: I know arrays start with index 0. I was just confused.
    Before I continue with this problem, I just spotted a new one.
    Check the comment that say "Here starts a check algorithm, to make sure there is no duplicated code". That algorithm is not working(there is duplicates in the arrray!). Some help?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by Pojahn_M View Post
    how can you not see photobucket pictures?
    <whisper>
    Using work computer. Certain links get blocked.
    </whisper>

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    for (int i = 0; i < key.length; i++) 
            { 
                FLoop: 
                for (int j = 0; j < 100; j++) 
                { 
                    for (int charPosition = 1; charPosition <= 10; charPosition++) 
                    { 
                        key[i][j].setCharAt (charPosition, encryptChar[(int) (3 * Math.random())]); 
                    } 
    
                    if (j == 0 && i == 0) 
                        continue FLoop;    //There is nothing to compare with if i and j is zero 
    
                    //Here starts a check algoritm, to make sure there is no duplicated code 
                    for (int iC = 0; iC < i; iC++) 
                    { 
                        for (int jC = 0; jC < j; jC++) 
                        { 
                            if (key[i][j].equals (key[iC][jC])) 
                            { 
                                j--; 
                                continue FLoop; 
                            } 
                        } 
                    } 
                } 
            }
    If you find yourself writing that many nested loops and/or if statements then you need to rethink your design. It can only lead to confusion as to what the code is doing and you can easily introduce errors. A better approach is to break down what the code is supposed to do into smaller individual steps. Then you can move each step into its own method.

  7. #7
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    basically, what I want to do is comparing and 2d array with an other 2d array. It sounds easy, but I cant even think of how to do it.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It would be really simple if you first created a method which compares a 1 d array, then a method that uses the first to test the 2d array.

    Java Code:
    public boolean compareArray(int[] first, int[] second){ ... }
    public boolean compare2D(int[][] first, int[][] second){
      for(each array in the 2d array)
        use compareArray
    }
    As junky said, anytime you work with multi dimensional arrays, it can be made simpler by breaking things into steps(this works with many other things as well). Rather than working on one huge task, why not split it into a few(+) small tasks?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Pojahn_M View Post
    Fubarable: I know arrays start with index 0....
    OK, well shut my mouth then.

Similar Threads

  1. RunTime Error
    By LuciferxFatality in forum New To Java
    Replies: 4
    Last Post: 07-04-2011, 04:41 AM
  2. passing arument to runtime.getRuntime() method
    By cool in forum AWT / Swing
    Replies: 1
    Last Post: 11-12-2010, 06:15 AM
  3. Runtime Error
    By mathfxr in forum New To Java
    Replies: 8
    Last Post: 09-17-2010, 08:26 PM
  4. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  5. runtime method
    By roo7 in forum Advanced Java
    Replies: 5
    Last Post: 11-30-2007, 04:35 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
  •