Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will decode a secret message (file on BlackBoard) You shoul

ID: 3606324 • Letter: W

Question

Write a program that will decode a secret message (file on BlackBoard) You should read the secret message character by character. To decode, you will need to add 5 to each ASCII character read; for instance if you read ASCII 81 ("Q") you should advance it to ASCII 86 ("V"). The valid range of ASCII characters should be from 32 to 122. If your addition of 5 places an ASCII character out of this range, it will need to "loop around" to the start of the range, i.e., if you get a new ASCII value of 123, you should instead decode as ASCII 32. As you read each character from H:secretMessage.txt, you should write the decoded character to H:decodedMessage.txt. Use a try with resources! If you encounter an ASCII character outside the range (32 - 122) throw an IndexOutOfBoundsException with a descriptive message. When you catch the IOException, write the Exception object to the console. When you catch the IndexOutOfBoundsException, write that nice descriptive message to the console.

secret message is

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class Decode {

public static void main(String[] args) throws FileNotFoundException{

Scanner scan = new Scanner( new File("C:/Users/Acer/Desktop/secretMessage.txt"));

char input;

char output;

int ascii,res;

String ans;

StringBuilder sb = new StringBuilder();

while( scan.hasNext() ){

input = scan.findInLine(".").charAt(0);

ascii = (int) input;

if( ascii < 32 || ascii > 122){

throw new IndexOutOfBoundsException("The character" + input

+ " in secret code"

+ " has accii code out of bounds 32-122");

}else{

res = ascii + 5;

//To cycle through if new ascii > 122

if(res > 122){

res = res - 123 + 32;

}

output = (char) res;

// Append output to string builder to write to file later

sb.append(output);

}

}

scan.close();

ans = sb.toString();

try {

FileWriter fw = new FileWriter("C:/Users/Acer/Desktop/decodedMessage.txt");

fw.write(ans);

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote