Steve C
Steve C

Reputation: 11

simple ciphering in java

I'm very new to java and to programing in general and I'm trying to figure out how to get this code to work.

What I need is to input a string then convert it to ascii and have the ascii values of each character shift by the value that was input and then convert it back to characters and print out the coded message. The last part is what I am having trouble with, I cant figure out how to get it back to characters.

P.S. this is my first posting so if I'm putting my code up wrong please let me know.

import java.util.Scanner;



public class 
    public static void main(String[] args)
    {    
        Scanner stdIn = new Scanner(System.in);


    System.out.println("Please enter text to encrypt");
    String orignalText = stdIn.nextLine();
    System.out.println("Please enter shift value");
    int shiftValue = stdIn.nextInt();




    for (int i=0; i<orignalText.length(); i++)
    {
            char c = orignalText.charAt(i);
            char cUpper = Character.toUpperCase(c);
        System.out.print((cUpper) + shiftValue);


    }


}//end main 
}//end class

Upvotes: 1

Views: 1174

Answers (2)

saremapadhasa
saremapadhasa

Reputation: 106

System.out.println((char)((int)cUpper + shiftValue));

Upvotes: 2

MrSmith42
MrSmith42

Reputation: 10151

You have to cast it back ti a char

replace

    System.out.print((cUpper) + shiftValue);

by

    System.out.print((char)(cUpper + shiftValue));

Upvotes: 0

Related Questions