Question
Fix the code so the output is Your Message? Attack zerg at dawn! Encoding Key? 3 Your message: dwwdfnchujdwgdzq Your Message? 10 go forward Encoding
Fix the code so the output is
Your Message? Attack zerg at dawn! Encoding Key? 3 Your message: dwwdfnchujdwgdzq Your Message? 10 go forward Encoding Key? 5 Your message: 4443ltktwbfwi
import java.util.*; // for Scanner public class demo699 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Your Message? "); // asks for message String message = console.nextLine(); // store message in String variable System.out.print("Encoding Key? "); // asks for the key int key = console.nextInt(); // store key in integer variable Cipher.cipher(message, key); } public static void cipher(String message, int key) { // takes in 2 parameters String encodedMessage = ""; message = message.toLowerCase(); // converts all letters to lower case for(int i = 0; i < message.length(); i++) { // loop through all the characters of the string char c = message.charAt(i); // store each character if(Character.isLetter(c)) { // tests if stored character is a letter c += key; if(c > 122) { c -= 26; } encodedMessage += c; // add converted letter to encoded message } else if(Character.isDigit(c)) { // tests if stored character is a digit c -= key; // shift digit to left amount using the key int ascii = c; // get ascii value of the converted character encodedMessage += ascii; // add ascii value of converted character to encoded message } // all other characters are ignored } System.out.println("Your Message: " + encodedMessage); // display encoded message } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started