In: Computer Science
Write the program in java
Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by one, but Z goes back to A.
Example: Hello converts to Ifmmp
All other punctuation and spaces or symbols can stay as they are.
What should I name the input and output files? And where should they be saved?
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Place your input file in current working directory or the root directory of your project (copy the file from folder, right click on your project name, click paste), you can name your file anything you like. But make sure you provide the same file name when asked. Also provide any name for the output file, it will be created, or overwritten if the file already exists. If you face issues with current working directory or something, you can paste complete file path instead.
// Encrypt.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class Encrypt {
public static void main(String[] args) throws IOException {
// setting up a Scanner to read from keyboard
Scanner scanner = new Scanner(System.in);
// asking and reading input and output file names. input file must exist
// in the current working directory or the root directory of your
// project.
System.out.print("Enter input file name: ");
String in = scanner.nextLine();
System.out.print("Enter output file name: ");
String out = scanner.nextLine();
// creating a BufferedReader to read character by character from input
// file
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(in))));
// print writer to write into file
PrintWriter writer = new PrintWriter(new File(out));
// reading ascii value of first character
int i = reader.read();
// loops until i returns -1 (end of file flag)
while (i != -1) {
// casting i to character
char ch = (char) i;
// checking if ch is upper case
if (Character.isUpperCase(ch)) {
// incrementing the ascii value of ch by 1
ch++;
// if ch go beyond 'Z', wrapping around from 'A'
if (ch > 'Z') {
ch = 'A';
}
}
// doing the same for lower case
else if (Character.isLowerCase(ch)) {
ch++;
// if ch go beyond 'z', wrapping around from 'a'
if (ch > 'z') {
ch = 'a';
}
}
// writing to output file
writer.print(ch);
// reading ascii value of next character
i = reader.read();
}
// closing both files
reader.close();
writer.close();
// alerting user that the operation is completed successfully.
System.out.println("Done! Please check the output file.");
}
}
/*OUTPUT*/
Enter input file name: input.txt
Enter output file name: output.txt
Done! Please check the output file.
/*input.txt that I used*/
hello, how are you all doing?
I'm good. Good day
/*output.txt after running the program*/
ifmmp, ipx bsf zpv bmm epjoh?
J'n hppe. Hppe ebz