I am a begginer and this is my first question. I've scoured the net for answers and I am coming up short. Any help any of you can provide will put a smile on my face!!
I am writing a program that loops over two arrays simultaneously. These are char arrays cast from user defined strings so they will likely be different lengths. Below is how things are currently set up in my code.
for(int i = 0; i < charArray1.length; i++)
{
char keyChar = charArray1[i];
char messageChar = charArray2[i];
}
Considering the example above. lets say that:
charArray1 = {'A','B','C','D'} and
charArray2 = {'1','2','3','4','5','6','7}
Currently this scenario tosses me an out of bounds exception, as it should. What I'd like to see happen is for a loop to return to the start of charArray1 while another loop continues to the end of charArray2.
If I were to print this it might look something like below.
A1, B2, C3, D4, A5, B6, C7
Any help would be greatly appreciated. I've been at this for a while now.
What you need to do is make a for loop inside a for loop
This loops over the inside loop and once it reached the end loops over again until the end of
charArray2is reached at which point the outter for loop will terminate thus exiting the loop. There are other ways to do this but this is one ways that makes it easier for a beginner like you to understand.Then if you want it to print out A1,B2,... exactly without two for loops you can do the following. Two for loops not be the best way to go if you are dealing with a large set of numbers since two for loops are
n^2while anifstatement is just a constant like1i think.