-2

I've been trying to write a simple Caesar Shift in Java that assumes the user will only enter uppercase letters with no spaces. A becomes Z, B becomes Y and so on. I need to learn how to do this on my own so please dont just write out the code for me, just nudge me in the correct direction. Here's the bit of my code thats causing the problems I think.

System.out.print("Please enter you word: ");
        String code = in.nextLine();
        for (int i=0; i < code.length(); i++){
            System.out.print ((char)(code.charAt(i)+'A'+26) %26 );
John0121
  • 1
  • 3
  • What is wrong with the code you have now? Not the desired output? (If so please give an example input/output and what the code is currently giving). Are you getting an error? (If so please post the full stacktrace of the error and what line is causing the error) – GBlodgett Sep 19 '18 at 02:04
  • If I type in A it returns 0, B returns 1, and so forth – John0121 Sep 19 '18 at 02:32
  • :) It's funny coincident that A+A is dividable by 26. I suggest you start from scratch – Alexander Pavlov Sep 19 '18 at 02:44

2 Answers2

3

To use % 26 you must ensure your data is within 0..25 interval. A must be 0, Z must be 25. Imagine, first character is B. What you're doing is (B+A+26)%26 - it just makes no sense.

To make it correct

  1. Think how to make 0 from A, 1 from B etc
  2. Think how to shift 0 to 25, 1 to 0, 2 to 1 etc
  3. Think how to make A from 0, B from 1 etc
Alexander Pavlov
  • 2,264
  • 18
  • 25
  • This is the right way to answer this question. The OP needs to work it out for himself, and this provides the necessary guidance. – Stephen C Sep 19 '18 at 02:16
  • I think you meant `('B'+'A'+26)%26` – Andreas Sep 19 '18 at 02:19
  • Either you didn't get the question completely or he/she asked that wrongly. The question states: _`A` becomes `Z`, `B` becomes `C`, and so on_...meaning that, you would shift to the left (or `25` times to the right), and then only one time to the right, and so on (alternatively...or what?). More likely should have been: _`Z` becomes `A`, `B` becomes `C`, and so on_. – x80486 Sep 19 '18 at 02:30
  • With my Current code it outputs 0 for A, B for 1 and so on, so I hope im on the right track – John0121 Sep 19 '18 at 02:33
  • @x80486 - I don't think that matters. The gist of the answer is that >>the OP<< needs to think about this. Alexander is (deliberately) NOT trying to spoon-feed the OP with an answer that he can apply without thinking about it. That would defeat the purpose of the OP's homework. (And note that the OP isn't asking to be spoon-fed!!) – Stephen C Sep 19 '18 at 02:33
  • My apologies, I ment A becomes Z, B becomes Y and so on. – John0121 Sep 19 '18 at 02:34
  • Thanks for spotting. I think OP meant `A becomes Z, C becomes B` so 25 times right. Caesar Shift is simple circular shift so it is not supposed to have complex rules – Alexander Pavlov Sep 19 '18 at 02:35
  • A becomes Z, B becomes A, C becomes B, D becomes C, etcetera. It is so simple a Roman could do it. ( For those of you old enough to remember the Geiko ads :-) ) – Stephen C Sep 19 '18 at 02:39
  • 1
    Well, @John0121, now things are getting confused! "A becomes Z, B becomes Y" isn't a simple _shift_ (more like a _transpose_, I guess). and your original statement of "A becomes Z, B becomes C" is two contradictory shifts. You'd better decide what you _really_ want to do here. But the general idea will still be the same: turn the letters into numbers 0-25, transform those numbers into other numbers 0-25 (by whatever transformation you ultimately settle on), and turn the massaged numbers back into letters. – Kevin Anderson Sep 19 '18 at 02:44
  • John - if this is really Caesar's cypher you are supposed to be implementing, then you need to understand what Caesar's cypher is. It is a shift / rotation, NOT a transpose; see https://en.wikipedia.org/wiki/Caesar_cipher – Stephen C Sep 19 '18 at 03:12
  • I made improvements! My program is now giving me the correct ASCII values! When I enter ZZ I get 6565, or AA i get 9090! Now I just need to convert the decimal values to ASCII – John0121 Sep 19 '18 at 03:32
0

I'm not sure that A to Z, B to C corresponds to a Caesar Shift does it?

Let's take the example from Wikipedia, a left shift of 3 places, or equivalently a right shift of 23 places:

Plain:    ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher:   XYZABCDEFGHIJKLMNOPQRSTUVW

To get the position of the cipher character we add the right shift, 23, to the position of the plain character. This is fine for A,B,C, but by the time we get to D we've gone beyond the end of the alphabet. To fix this we need to "wraparound" to the beginning, and the technique for doing this is to use the modulo (%) operator, with a modulus equal to the number of elements, in this case 26. You said you just wanted a nudge, so hopefully this is enough, but not too much, information.

One more point. You say you're code is outputting 0 for 'A', 1 for 'B' etc. With 'A' as input your code is equivalent to:

 System.out.print ((char)('A'+'A'+26) % 26) 

'A' has an integer value of 65, so this is equivalent to:

System.out.print ((char)(65+65+26) % 26)

You're casting (65+65+26) to a char, but then the % operator, with an int operand (26), produces an int, so you're actually printing:

System.out.print (0)

Once you've worked out the correct expression for the Caesar Shift you need to cast the result back to a (char) when you print it, as in:

System.out.print ((char)(expression))

Otherwise you'll get the int value of the expression.

RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16