Results 1 to 8 of 8
  1. #1
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default charArray goes out of bounds

    Hello again!

    I've got a little problem.

    Goal this time is to take a charArray, copy it into another charArray while reversing the things in it.
    e.g. charArray["!ollaH"] into charArrayNew["Hallo!"]

    My first idea was to revert the stuff in the Array with a ! cause i saw earlier that u can work with that too revert booleans. Sadly i didnt happen to make it work.
    Next thing i thought of was a for loop to go trough the charArray and copy every section into charArrayNew just at the opposite end.

    Java Code:
    import java.util.Arrays;
    public class aufgabe43 {
    	public static void main(String[] asgr){
    		char[] charArray = (".hclim enrethchcs enie - ?iew tsi dnu muab menie retnih thets saw").toCharArray();
    		char[] charArrayNew = new char[charArray.length];
    		for(int i = 0; i < charArray.length;){
    			int h = charArray.length;
    			charArrayNew[h] = charArray[i];
    			h--;
    			i++;
    		}
    		System.out.println(Arrays.toString(charArrayNew));	
    		}
    }
    Eclipse doesnt show any errors, and as u told me last time i did include import java.util.Arrays; to output the array in the end.
    When i try to compile the code eclipse returns with an error
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 68
    	at aufgabe43.main(aufgabe43.java:8)
    which i frankly don't understand since the array.length is exactly the same.

    Any thoughts?

    regards Nando

  2. #2
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: charArray goes out of bounds

    charArry.length gives you the exact length. charrArrayNew[h] should be charrArryNew[h-1] and you wont have an out of bounds exception.

  3. #3
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: charArray goes out of bounds

    The reason behind this is an array is 0 based where as length counts from 1.

  4. #4
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: charArray goes out of bounds

    and i would define h outside of the for loop.
    int h = charArray.length;
    for(int i = 0; i < charArray.length;){

  5. #5
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: charArray goes out of bounds

    Java Code:
      int h = charArray.length;
    for(int i = 0; i < charArray.length; i++){
                charArrayNew[h-1] = charArray[i];
    h--;
            }
    This is what it would end up being.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: charArray goes out of bounds

    Actually, the length doesn't count from anywhere. It is simply the size of the array.

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

  7. #7
    NandoT is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: charArray goes out of bounds

    Awesome!
    Thank u for ur really fast answers! :)

    First i defined h outside of the loop then i added the -1.
    Is there a way to display the Array someway else? For me it looks this way
    [w, a, s, , s, t, e, h, t, , h, i, n, t, e, r, , e, i, n, e, m, , b, a, u, m, , u, n, d, , i, s, t, , w, e, i, , ?, , -, , e, i, n, e, , s, c, h, , c, h, t, e, r, n, e, , m, i, l, c, h, .]
    and thats kinda ugly ;)

    regards Nando

  8. #8
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: charArray goes out of bounds

    Instead of the System.out.print you could replace it with
    Java Code:
    StringBuilder sb = new StringBuilder();
    
    for(int y=0; y<charArrayNew.length; y++){
    sb.append(charArrayNew[y]);
    }
    
    System.out.println(sb.toString());

Similar Threads

  1. Arraylist to chararray
    By evanc in forum New To Java
    Replies: 1
    Last Post: 12-19-2011, 01:13 PM
  2. Out of bounds exception
    By MetalR0 in forum New To Java
    Replies: 9
    Last Post: 07-31-2011, 08:08 PM
  3. Change of index in charArray
    By kovalensue in forum New To Java
    Replies: 19
    Last Post: 11-10-2010, 12:34 PM
  4. Bounds problem...
    By licka in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 10:38 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
  •