1

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 

2 Answers 2

2
System.out.println((char)((int)cUpper + shiftValue)); 
Sign up to request clarification or add additional context in comments.

Comments

0

You have to cast it back ti a char

replace

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

by

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.