# Help with Arrays!

• April 12th, 2013, 11:53 AM
le pencil
Help with Arrays!
Hey guys! I'm fairly new to java. For my course at uni I've got to design a caeser box that will encrypt a word and decrypt it. I pretty certain I've got the logic behind it so I won't need someone to give me the full code haha that would be cheating anyway. I like a challenge. Anyway.
I've started my code by reading the string, I've eliminated white spaces and its all lower case. After that I put the string in a char array with a total length of the string without whitespaces. after this I declared a grid with dimensions of x*x where x is the closest root of the length of the string without whitespaces.
So upto now its all good but this is where the problem starts ...
I've done 2 for loops to create the grid and then I'm increasing the rows and columns by 1 (ofc) and the values of each grid is equal to the elements of my char array that contains the string.
this might seem messy but I'll paste the problematic code part:
Code java:

```for(int row = 0; row<plainTextGrid.length; row++){ for(int column = 0; column <plainTextGrid.length; column++){ plainTextGrid[row][column]= plainTextArray[0];// this is where I want to set each element to a position on the grid. plainTextGrid is the grid, and plainTextArray is the array     System.out.print(plainTextGrid[row][column] + " "); }   System.out.println(); }```
My question is how can I increase each element of the char array by 1.
• April 12th, 2013, 11:59 AM
Norm
Re: Help with Arrays!
Quote:

how can I increase each element of the char array by 1.
You can do normal math with char values: 'a' + 1 = 'b'
• April 12th, 2013, 12:32 PM
le pencil
Re: Help with Arrays!
Quote:

Originally Posted by Norm
You can do normal math with char values: 'a' + 1 = 'b'

I don't actually get what you are trying to say. My char array is in the two for loops, I want to increment the value of it until it reaches the last char in the array. so for example I could do this:
Code java:

```plainTextGrid[row][column]= plainTextArray[0]; plainTextArray[0]=plainTextArray[1]; plainTextArray[1]=plainTextArray[2]; //etc...```

yeah so basically thats untidy and not useful for my situation

--- Update ---

Man Norm you are the best I can't believe how easy of a fix that was!!! cheers!
• April 12th, 2013, 12:34 PM
Norm
Re: Help with Arrays!
Please explain what you mean by " increment the value"
x= x+1 // increments the value of x by 1

The code at the end of the post looks like it is shifting the elements to the left by one slot. Does the shifting wrap around at the end: first element goes in last slot?
• April 12th, 2013, 12:40 PM
le pencil
Re: Help with Arrays!
So what I did was add an int above the for loops
Code java:

```int a = 32; for(int row = 0; row<plainTextGrid.length; row++){ for(int column = 0; column <plainTextGrid.length; column++){   plainTextGrid[row][column]= plainTextArray[32-a]; a--;     System.out.print(plainTextGrid[row][column] + " "); }   System.out.println(); }```

and output :
a l o n g t
i m e a g o
, i n a g a
l a x y f a
r , f a r a
w a y Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 33
at CaesarBox.encrypt(CaesarBox.java:30)
at CaesarBox.main(CaesarBox.java:8)
• April 12th, 2013, 01:05 PM
Norm
Re: Help with Arrays!
Quote:

ArrayIndexOutOfBoundsException: 33
Looks like a went to -1
33 = 32 - (-1)
How many times does the inner loop execute?

The code needs to check that the index is in bounds.