I need some help configuring my program. This is what i currently have, and belo
ID: 641960 • Letter: I
Question
I need some help configuring my program. This is what i currently have, and below that is what it has to turn out to be.
public static void main(String[] args) throws FileNotFoundException {
String filename = "message.txt";
File fileHandle = new File(filename);
Scanner inputFile = new Scanner (fileHandle);
String line;
while(inputFile.hasNextLine()){
line = inputFile.nextLine();
System.out.println(line);
}
inputFile.close();
}
}
create a text file (such as message.txt) that contains a brief text message of your choice (50-100 words). Your program is read the file, show the contents of the file to the user (print the contents of the file to the console), and then ask the user for an integer number to use as a rot-n encryption key.
Your program will be performing a rotational cipher over the entire set of UNICODE characters. Adjusting the value of each character in the message (by adding/subtracting the provided integer key) and output the results to BOTH the console and a file. Use the same file name as the input file, but append
Explanation / Answer
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class RotationalCipher
{
public static void main(String[] args) throws IOException
{
String filename = "message.txt";
File file=new File(filename);
if(!file.exists()){
System.out.println("Input File Does not exists");
return;
}//program exits or crashes if input file does not exist
//reading from file
String originalText="";
Scanner scanner=new Scanner(file);
while(scanner.hasNextLine()){
originalText+=scanner.nextLine()+" ";
}
scanner.close();
//printing file content
System.out.println("original content");
System.out.println(originalText);
String cipherText="";
System.out.println("Cipher file contents");
//printing cipher content
for(int i=0;i<originalText.length();i++)
{
char letter=originalText.charAt(i);
cipherText+=++letter;
}
System.out.println(cipherText);
//writing to another file
System.out.println(filename.replace(".", "-rot."));
file=new File(filename.replace(".", "-rot."));
if(!file.exists())
file.createNewFile();
System.out.println(file.getAbsolutePath());
FileWriter writer=new FileWriter(file);
writer.write(cipherText);
writer.close();
}//end of method main
}//end of class Rotational Cipher