2

There is a slight part of my program that is not working correctly and I just cannot work out why

String KEY = "hello"
for (int h = 0; h < message.length(); h++)
{
    keyWord += KEY.charAt(h);
}

Once I run the loop the keyWord string does not hold the letters it should of hello, but if I bypass it and just use KEY in my program it will work fine.

Could someone shed some light as to what I am doing wrong please?

4
  • is message.length() always <= KEY.length? Commented Nov 17, 2011 at 18:50
  • Hint: Strings are immutable. But share more code please... Commented Nov 17, 2011 at 18:51
  • Please give a short but complete program. At the moment there's far too much that we're guessing at. Commented Nov 17, 2011 at 18:51
  • keyWord is type string and message.length is so that i can create a string that is the same length as an incoming message so I want to have it repeating i.e if the KEY is hello and the message is goodbye I want keyWord to be hellohe. Commented Nov 17, 2011 at 18:54

3 Answers 3

1

Change

for (int h = 0; h < message.length(); h++)

to

for (int h = 0; h < KEY.length(); h++)

You are trying to append KEY to keyword... so you need to check its length and not of some other string.

Sign up to request clarification or add additional context in comments.

1 Comment

This could be the problem, but make sure your type for keyWord is correct.
0

I now see what you are trying to do, you are creating a cipher.

public class Test {

     public static void main(String[] args){
           String key = "hello";
           String keyWord = "";
           String message = "asdfghj";
           for (int i = 0; i < message.length();i++)
           {
               keyWord += key.charAt(i %key.length());             
           }
           System.out.println(keyWord);
}
}

For anyone who is not sure the % or modulo operator returns the remainder of division. For instance 4 % 5 = 4 as 4 / 5 leaves a remainder of 4 because it goes into it 0 times. Now 6%5 = 1 as 6/5 = 1 R1 .

3 Comments

I should probably have explained this a bit better, my code doesn't really reflect what I was trying to do when it gets to the end of the keyword length i want it to go back to the start of the keyword and start again so if a message length was 10 i would want hellohello for the keyword is there a way to change this loop to do that?
That is exactly what the code above would do. The modulo operator will ensure you dont go beyond the bound of the key length.
That is perfect thank you, could you just explain to me what the % before key.length does?
0

I'm assuming that keyWord is a String. The problem is that when you do this:

keyWord += KEY.charAt(h);

The expression KEY.charAt(h) is of type char, which is an integer value. When you add an integer to a String using + (or +=), the compiler generates code to add the string representation of the integer value. It does not append the character itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.