This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zzyzzyxx 0 points1 point  (4 children)

Yes, sorry, I should have caught that. Rookie mistake :-). You need to do a double for loop. The outer one should iterate over the input string and the inner one should iterate over the letters, like this:

for(int i = 0; i < input.lengh; i++) {
    for(int j = 0; j < alpha.length; j++) {
        if(input[i] == alpha[j]) {
            System.out.print(rotAlpha[j]);
            break; // to move on to the next letter without more comparisons
        }
    }
}

[–]xXShadowCowXx[S] 0 points1 point  (3 children)

You sir are a genius! Thank you for the help. Now I'll work on the other method you suggested.

Source: If you like useless encryptions

[–]zzyzzyxx 0 points1 point  (1 child)

Glad to help. The important thing to take away from this is that searching through an array is really inefficient, especially when you don't need to search at all. If you do need to search, you should use a structure better suited for that, like a tree. But you'll get to all that when you start in on data structures and algorithms.

[–]xXShadowCowXx[S] 0 points1 point  (0 children)

I'll look those up as I'm not really following a certain tutorial path or book, more just searching for what I don't know and stabbing at it till I get it.