Write a program to decode secret messages, using the numeric key from data5a.txt
ID: 3729139 • Letter: W
Question
Write a program to decode secret messages, using the numeric key from data5a.txt, described below. Each line in the data file, data5b.txt will be a string to be converted from the garbled characters into readable characters. The numeric key is the positive integer value that was added to the ascii character’s numeric value to get the coded message. You should subtract that value to decode(remember to keep the character values in the range of ‘a’-‘z’ have numeric value between 97(a) and 122(z). If a character is outside after conversion, force that character back into the range). Any characters that do not start in that range should just be passed straight through, for instance the space(character = 32). All letters will be lower-case, punctuation and spaces should be passed straight through.
The sample out put I am getting is
Spring 2017 Project 5 Due 3/10/17 by Your Name
the code is - *ftq azxk fuyq bqabxq puexuwq saeeub ue itqz kag saeeub mnagf ftqy
decoded is -*the only time people dislike gossip is when you gossip about them
the code is - *kag'dq zqhqd idazs fa pa ftq dustf ftuzs
decoded is -*you5re never wrong to do the right thing
the code is - *dqmxufk ue ftq ygdpqd ar m nqmgfurgx ftqadk nk m smzs ar gsxk rmofe
decoded is -*reality is the murder of a beautiful theory by a gang of ugly facts
the code is - *zqhqd oazrgeq yahqyqzf iuft mofuaz
decoded is -*never confuse movement with action
the code is - *kag wzai qhqdknapk ue uszadmzf, azxk az purrqdqzf egnvqofe
decoded is -*you know everybody is ignorant: only on different subjects
the code is - *fa qdd ue tgymz, ngf fa dqmxxk ragx ftuzse gb dqcgudqe m oaybgfqd
decoded is -*to err is human: but to really foul things up requires a computer
the code is - *iuepay ue itmf'e xqrf mrfqd iq'hq dgz agf ar bqdeazmx abuzuaze
decoded is -*wisdom is what5s left after we5ve run out of personal opinions
the code is - *imetngdz uotmnape dgxq wmzeme eymxx oaxxqsqe
decoded is -*washburn ichabods rule kansas small colleges
sample input I am putting in is:
12
ftq azxk fuyq bqabxq puexuwq saeeub ue itqz kag saeeub mnagf ftqy
kag'dq zqhqd idazs fa pa ftq dustf ftuzs
dqmxufk ue ftq ygdpqd ar m nqmgfurgx ftqadk nk m smzs ar gsxk rmofe
zqhqd oazrgeq yahqyqzf iuft mofuaz
kag wzai qhqdknapk ue uszadmzf, azxk az purrqdqzf egnvqofe
fa qdd ue tgymz, ngf fa dqmxxk ragx ftuzse gb dqcgudqe m oaybgfqd
iuepay ue itmf'e xqrf mrfqd iq'hq dgz agf ar bqdeazmx abuzuaze
imetngdz uotmnape dgxq wmzeme eymxx oaxxqsqe
I need it to print to a file and not the terminal. I also need help correcting the punctuation that is carried over wrong. My work so far is:
/*The java program EncryptDecrypt that read an input file
* dataSample5a.txt and decode the message and print the decoded
* message to the console and output file dataSample5b.txt*/
//EncryptDecrypt.java
import java.io.*;
import java.util.Scanner;
public class project05
{
public static void main(String[] args)
throws FileNotFoundException,StringIndexOutOfBoundsException
{
String line="";
Scanner filescanner = null;
PrintWriter filewriter= null;
try {
//Open file for reading and writing data
filescanner = new Scanner(new File("dataSample5a.txt"));
filewriter= new PrintWriter(new File("dataSample5b.txt"));
int a = Integer.parseInt(filescanner.nextLine());
System.out.printf("Spring 2017 Project 5 Due 3/10/17 by Your Name ");
filewriter.printf("Spring 2017 Project 5 Due 3/10/17 by Your Name ");
//read till end of file
while (filescanner.hasNextLine())
{
line = filescanner.nextLine();
System.out.printf("the code is - *%s", line);
filewriter.printf("dhe code is - *%s ", line);
System.out.printf(" decoded is -*");
filewriter.printf(" decoded is -*");
for (int i = 0; i<line.length(); i++)
{
char ch = line.charAt(i);
int ascii = (int) ch;
if (ch == 32 )
{
System.out.print(ch);
filewriter.print(ch);
}
else if (ch <109)
{
ascii = 123 + (ch - 97) - a;
System.out.print((char) ascii);
filewriter.print((char) ascii);
}
else
{
ascii -= a;
System.out.print((char) ascii);
filewriter.print((char) ascii);
}
}
filewriter.print(" ");
System.out.printf(" ");
}
} catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}
I am still relatively new to this and some of the commands I am unfamiliar with and was wondering if there was a way to "dumb" the program down. I am unfamiliar with the try command, and string line. Whenever I try to switch filescanner and file writer respectfully to inF and outF, as I only know that, I cant fix any of the bugs that come afterwards. Thanks for any help.
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class project05
{
public static void main(String[] args)
throws StringIndexOutOfBoundsException, IOException
{
String line="";
Scanner filescanner = null;
BufferedWriter filewriter= null;
try {
//Open file for reading and writing data
filescanner = new Scanner(new File("dataSample5a.txt"));
filewriter = new BufferedWriter(new FileWriter("dataSample5b.txt"));
int a = Integer.parseInt(filescanner.nextLine()) % 26;
System.out.printf("Spring 2017 Project 5 Due 3/10/17 by Your Name ");
filewriter.write("Spring 2017 Project 5 Due 3/10/17 by Your Name ");
//read till end of file
while (filescanner.hasNextLine())
{
line = filescanner.nextLine();
System.out.printf("the code is - *%s", line);
filewriter.write("dhe code is - * ");
filewriter.write(line);
System.out.printf(" decoded is -*");
filewriter.write(" decoded is -*");
for (int i = 0; i<line.length(); i++)
{
char ch = line.charAt(i);
int ascii = (int) ch;
if(ch >= 97 && ch <= 122) {
ascii = ch - a;
if(ascii < 97)
ascii = ascii + 26;
}
System.out.print((char) ascii);
filewriter.write((char) ascii);
}
filewriter.write(" ");
System.out.printf(" ");
}
} catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.