java
i have a problem sollving this very simple question?
here goes
a programme in java is made to encrypt and decrypt messages.
using only letters. the programme should be done
using the ascii code. and instead of an a show b and b an c
for example "dog" should be encrypt as "eph". letter +1
what is the code that will allow me to do that?
[381 byte] By [
t123a] at [2007-10-2 1:39:39]

Well, if you're going to be looking at character values, you probably need a character array from the string, try str. toCharArray(). Next, all you need to know is that you're allowed to add numbers to characters, so loop over your array and add one to the letters. Now, any character that's z+1 goes to an 'a', and you can get a string with new String(myChars).
~Cheers
ok thanks for the help but i have one more thing i am new to programming so when you said loop and add onee to the letters how do i write that in javaalso the code you said tochararray() where do i need to write thatthanks in advance
t123a at 2007-7-15 19:03:09 >

I might suggest coming up with better "subject" descriptions, and not referring to character rotation as encryption.
public class Test {
public static String rotate(String s) {
char[] arr = s.toCharArray();
StringBuffer sb = new StringBuffer(arr.length);
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (c == 'z')
sb.append('a');
else if (c == 'Z')
sb.append('A');
else if (c >= 'a' && c < 'z' || c >= 'A' && c < 'Z')
sb.append((char) (c + 1));
else
throw new RuntimeException("Only letters a-z and A-Z can be rotated, not character: " + c);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(rotate(args[0]));
}
}
Actually, you don't need the toCharArray. You can use the String s.charAt(index) instead. I thought the StringBuffer class had a constructor that accepts a char[] array, but it doesn't.The reason to do this would be to avoid creating an extra array.
thanks for the help
you mentioned the string s.chatAt(index)
in the bracettes what do i need to write the programme is simple.
the computer ask to write a message to be encrypted.
example i wirte hello. then enter
the computer or the programme shoul change that in to letter +1 how do i set tha to happen would do i have to write in java to do that
thanks in advance
t123a at 2007-7-15 19:03:09 >

> thanks for the help
> you mentioned the string s.chatAt(index)
> in the bracettes what do i need to write the
> programme is simple.
> the computer ask to write a message to be encrypted.
> example i wirte hello. then enter
> the computer or the programme shoul change that in to
> letter +1 how do i set tha to happen would do i have
> to write in java to do that
> thanks in advance
Did you run the example rkippen gave you? You should be able to finish your assignment based on that example. Please post your code and tell where you're stuck.
> and not referring to character rotation> as encryption.Meh. It's all encryption, this kind just happens to be particularly poor...
use (char)(1+let.toCharArray()[n..]) the exact formulation.pls give me some $$ -:))thankspallab
here's the complete code:
String let = "Root";
char chars[] = let.toCharArray();
char flipChars[] = new char[let.length()];
for (int i=0;i < chars.length;i++)
{
if (1 + let.toCharArray() == 123) flipChars = (char) 97;
else
flipChars = (char) (1 + let.toCharArray());
}
System.out.println("" + new String(flipChars));
Console output==>> Sppu