stringfor-loopcharcharat

Why is my code not turning a string into char?


I'm making a cryptographer and as a step trying to turn the string into char to be able to replace each char with a "crypto char". However I can't get this code to work and I have no idea what the problem is.

My code:

String encrypt(String text) {
int strLength = text.length();
    for (int k=0, k < strLength, k++) {
    letter = text.charAt(k);
   }
}

I'm getting these errors:

Syntax error, insert "; ; ) Statement" to complete ForStatement
Duplicate local variable k
Syntax error on token(s), misplaced construct(s)
k cannot be resolved to a variable
Syntax error on token ")", ; expected
k cannot be resolved to a variable
at inl1.Cryptographer.encrypt(Cryptographer.java:25)

Line 25 is the "for" line.

Help is very appreciated!


Solution

  • I don't speek Java, but I think your for cycle should use semicolons ; instead of commas , :-)

    for (int k=0; k < strLength; k++) {
      ...
    }