Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array Index Out of Bounds Exception !

    I re coded it with different names for my array and it worked, but when I cross check the two, I can't spot the error..
    Code:
    java.lang.ArrayIndexOutOfBoundsException: 25
    	at A3Q2.main(A3Q2.java:67)[/I]
    Can you guys please help me? I tried everything.

    The program is supposed to code an input of a word into an array

    Code:
    class A3Q2 
    {
        public static void main (String[] args)
        {
    
            char original[];  // Original Alphabet
           
           original = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z'};
            
            char  code[]; // Code corresponding to Alphabet
            code = new char [] {'t','m','e','s','f','k','j','a','x','n','o','v','u','l','c','h','z','g','y','b','w','p','d','r','i','q'};
            int n = 0;
            char [] wa  = new char[n];
            int C;
            int I;
            int C2;
            int I2;
            int p;
            
           // READ IN GIVENS  - use the class ITI1120
    
         System.out.println(" Please input a word");
                            
            wa = ITI1120.readCharLine();
              
    
    n = wa.length;
    
            char [] wa2 = new char[n];
            char [] wa3 = new char[n];
            
         
            
            // BODY
    
        
      C = 0;
      C2 = 0;
      
      
            {    
            while (C < n)
            {
              I = 0;
              while ( I <= 25)
              {
                if ( wa[C] == original[I])
                {
                  wa2[C] = code[I];
                
                }
                
                I = I + 1;
                
              }
              C =  C + 1;
            }
            }
             
           p =0;
        while (p < n)
        { 
          System.out.println ( wa2[p] );
          p = p+1;
          
        }
        }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'm too tired to debug it, but it looks like its a simple index in using 'i'. You don't appear to have an 'i', though you do have an 'I'.
    Looks like that's here:
    Code:
    if ( wa[C] == original[i])
    {
        wa2[C] = code[i];
                
    }
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Yeah he's right on your code. I'm not even sure how that compiled in order to get the ArrayIndexOutOfBounds Exception.

    Undoubtedly a Silly IDE is doing the work for you!
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

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