-1

I can't believe i've gotten this far. My problem is my output. This program is suppose to take input from the user and increment each letter by 2. So after taking the String from the user I turn there message into a char array. then while outputing it I added 2 to each letter. and my ouput is acssii numbers. I need it to be the actual letter. how do I do this?

import java.util.*;
public class Encryption {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    String userMessage = " ";   
    Scanner input = new Scanner (System.in); 

    System.out.print ("Please enter your Message:");    
    userMessage = input.nextLine().toUpperCase();

    char arr[] = userMessage.toCharArray();
    for (int i=0; i< arr.length;i++){
        System.out.print(arr[i] + 2);
    }

    }


}

Example input : "Thank you" Example output : 867467807734918187

Please explain to me why this is happening.

gabi145
  • 7
  • 1
  • 6
  • If you want a "string-like" message output, you need to convert your character to a string, see (https://stackoverflow.com/questions/8172420/how-to-convert-a-char-to-a-string) – tangoal Apr 07 '18 at 15:37
  • not sure what you are trying to achieve here. if you want to map "A" to "C", "B" to "D"..etc you might want to store the mapping in a HashMap and do a lookup. – nitnamby Apr 07 '18 at 15:39
  • I want thank you to turn into vjcpm aqx – gabi145 Apr 07 '18 at 15:42
  • 1
    Possible duplicate of [add number to a character to make it another character](https://stackoverflow.com/questions/29459437/add-number-to-a-character-to-make-it-another-character) – Joe C Apr 07 '18 at 15:53
  • Note: you need to consider two cases: 1) what if an input character is not a letter? (You've already given an example of that [' '].) 2) What happens when the character code for a letter plus 2 is not the character code for a letter (e.g., 'y' + 2 and 'z' + 2)? – Tom Blodget Apr 08 '18 at 04:05

3 Answers3

1

You may try casting the incremented value to char. Also, we can use the modulus operator to make sure that letters wrap around appropriately, e.g. Z should become B:

String userMessage = "ALFXYZ";
char arr[] = userMessage.toCharArray();
for (int i=0; i< arr.length;i++) {
    int nextNum = 65 + (arr[i] + 2 - 65) % 26;
    System.out.print((char)nextNum);
}

Demo

Your current code, as you wrote it, is printing numbers because of the casting rules.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Java cannot know ahead of time that arr[i] + 2 will be within the bounds of char (0-65535) so it will treat that expression as a number.

If you're sure the value of arr[i]+2 will never exceed this range then you can safely cast back to a char:

System.out.print((char)(arr[i] + 2));
Eric Conner
  • 10,422
  • 6
  • 51
  • 67
0

Java converts some primitive types automatically.

If c is a char and i is an int, then c + i will be treated as an int.

PrintStream, the type of System.out, has many overloads of print; the one chosen depends on the type of the argument.

Cast your addition back to char and it should work:

(char)(arr[i] + 2)
Judge Mental
  • 5,209
  • 17
  • 22