-1

I am quite at a loss here, i am to create a java programm which takes a String and decodes/encodes the string with n.(It adds n to the chars of the string like n=3 a=c) I buitl the "shell" which takes the user to the action he would like the programm to perform, then i dont get how to encode/decode the string with the key, it has to consider upper/lowercase and ignore special symbols like )/!/&"$;:.,, there has to be something with "for" and a chararray which i never worked with before and do not understand...

All help appreciated!

heres the code so far! https://gist.github.com/fabiomim/070d1daeee4b604db720adf7c7dff240

(ignore the little rant in fachklasse)

Xadro
  • 1
  • There is a simple encryption algorithm called ROT-13 which basically does what you want to do, but with `n=13`. This answer: http://stackoverflow.com/a/8981312/3061857 may help you on your way. – nbokmans Nov 25 '16 at 14:22
  • but what is your question? (Hausaufgabe? [I am unable to see the code, company/firewall restriction]) – user85421 Nov 25 '16 at 14:23
  • @CarlosHeuberger a little pointer in the direction i would have to go to replace the characters of the string and put it back together, its an assignment for a class i never even had! since my prof does not give a damn if you didnt have programming experience in an entrance cource! – Xadro Nov 25 '16 at 14:29
  • @nbokmans that was how far i got, but the method afaics doesnt "go around" to A again once it hits Z and it does not treat aA differently?! but thanks anyways! – Xadro Nov 25 '16 at 14:33
  • Xadro, Add the code here insteand of github. @CarlosHeuberger, I know what you feel, damn restriction policy – AxelH Nov 25 '16 at 14:56

1 Answers1

1

Some hints:

you can get the characters by using the charAt(int) or toCharArray() methods of String:

String string = ...
char ch = string.charAt(i);
// or
char[] characters = string.toCharArray();
char ch = characters[i];

A char is a Integral Type, that is, an integer type and you can do arithmetic with it, like comparison, addition, subtraction:

char ch = ...
if (ch >= 'a' && ch <= 'z') {
    // do something it the char is between 'a' and 'z'
    ch += 3;  // 'a' will become 'd', 'z' will be '}'!!!!
    if (ch > 'z') {
        // handle overflow like subtracting 'z'+1 - 'a'
    }
}

To create a String from the char array, you can use:

String result = new String(characters);

Note that a char is not an int, you need a cast to assign it to a char variable:

ch = ch + 3;  // ERROR since "ch + 3" is an int
ch = ch + 'a';  // ERROR the result of + is still an int!
ch = (char) (ch + 3);
ch = (char) (ch + 'a');
user85421
  • 28,957
  • 10
  • 64
  • 87