Question

In: Computer Science

Write the program in java Write a program that does basic encrypting of text. You will...

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?

Solutions

Expert Solution

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


Related Solutions

Needs to be in basic JAVA Write a program that does basic encrypting of text. You...
Needs to be in basic JAVA Write a program that does basic encrypting of text. You will ask the user the filename of a text file that 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...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher. A Caesar cipher shifts each letter a certain number of positions in the alphabet. The number of positions to shift will be given to your program as a command line argument. Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT