Question

For this code after case 0, I need it to ask the user if they would...

For this code after case 0, I need it to ask the user if they would like to see the menu again and some type of if statement or loop to ask them yes or no and if yes print again and if no end program. So it would ask if they would like to see the menu again then ask what their choice is. If yes print menu and if no end program.

import java.util.InputMismatchException;
import java.util.*;
import java.io.*;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Caesar Cipher!\nThis utility will let you encrypt and decrypt a message"
+ " from a file you provide.");
int userChoice = -1;
int cipher = 0;
String file;
String again = "yes";
String message = "";
String menu = "\n" + "Short messages\n" + "Option 1: Encrypt a Message\n" + "Option 2: Decrypt a Message\n"
+ "Long messages\n" + "Option 3: Encrypt a Text File\n" + "Option 4: Decrypt a Text File\n"
+ "Other Functions\n" + "Option 5: Letter Distribution Analysis\n" + "Option 0: Exit Program\n";
while (userChoice != 0) {
if (again.equalsIgnoreCase("yes") || again.equalsIgnoreCase("y")) {
System.out.print(menu);
}
System.out.print("What is your choice?: ");
try {
userChoice = in.nextInt();
in.nextLine();
} catch (InputMismatchException e) {
System.out.println("Input was not a number.");
in.nextLine();
userChoice = -1;
} // make a switch case for the menu

switch (userChoice) {
case 1:// encrypt message

System.out.println("Enter your short message: ");
message = in.nextLine();
cipher = get_num(in);
System.out.println(Caesar.encrypt(message, cipher));
break;

case 2:// decrypt message
System.out.println("Enter your short message: ");
message = in.nextLine();
cipher = get_num(in);
System.out.println(Caesar.decrypt(message, cipher));
break;
case 3:
// encrypt text
file = get_name(in);
if (file.equals("n")) {
break;
} else {
cipher = get_num(in);
message = ReadWrite.read(ReadWrite.open_file(file, in));
ReadWrite.write("Encrypted.txt", Caesar.encrypt(message, cipher));
System.out.println("Encrypted message saved to Encrypted.txt");
}
break;

case 4:// decrypt text
file = get_name(in);
if (file.equals("n")) {
break;
} else {
cipher = get_num(in);
message = ReadWrite.read(ReadWrite.open_file(file, in));
ReadWrite.write("Decrypted.txt", Caesar.decrypt(message, cipher));
System.out.println("Decrypted message saved to Decrypted.txt");
}
break;

case 5:
// letter distribution
String temp = "a";
while (!temp.equalsIgnoreCase("y") && !temp.equalsIgnoreCase("n")) {
System.out.print("Is the message in a file (y/n)?: ");
temp = in.nextLine();
if (temp.equalsIgnoreCase("n")) {
System.out.println("Enter your encrypted message: ");
message = in.nextLine(); // end enter message
} else {
file = get_name(in);
if (file.equals("n")) {
break;
} else {
message = ReadWrite.read(ReadWrite.open_file(file, in));
}
} // end file message
Caesar.printDistr(Caesar.letterDistr(message)); // exit message

}
break;
case 0:
System.out.println("Thank you for using the utility and goodbye~");
break;

default: // invalid input
System.out.println("That wasn't a valid menu option."); // end switch
if (userChoice != 0) {
System.out.print("Would you like to see the menu again?: ");
again = in.nextLine();
  
}
}
} // end while loop menu
in.close();
}// end main method

public static int get_num(Scanner in) {
int num = 0;
while (num < 2 || num > 26) {
try {
System.out.print("What is the cipher?: ");
num = in.nextInt();
in.nextLine();
if (num < 2 || num > 26) {
System.out.println("Cipher must be between 2 and 26 inclusive.");
System.out.print("What is the cipher?: ");
num = in.nextInt();
in.nextLine();
}
} catch (InputMismatchException e) {
System.out.println("You need to use a number for the cipher. Try again.");
break;
}
}
return num;
}// end get_num method

public static String get_name(Scanner in) {
String file;
String temp;
String response;
System.out.print("Enter the name of the text file: ");
file = in.nextLine();
String ext = file.substring(file.length() - 4);
boolean txt = ext.equals(".txt");
if (!txt) {
temp = file + ".txt";
System.out.print("Entered file name doesn't end with \'.txt\' " + "attempting to ammend file extension.\n"
+ "New File name: " + temp + "\n" + "Is this correct? ");
response = in.nextLine();
if (response.equalsIgnoreCase("yes") || response.equalsIgnoreCase("y")) {
return temp;
} else {
System.out.println("Program can only use txt files. Try again.");
return "n";
}
} else {
return file;
}
}//end get_name method
} //end Main

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.InputMismatchException;
import java.util.*;
import java.io.*;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Caesar Cipher!\nThis utility will let you encrypt and decrypt a message"
+ " from a file you provide.");
int userChoice = -1;
int cipher = 0;
String file;
String again = "yes";
String message = "";
while (again == "yes") //Added a while loop for repition of the menu untill the user enters no
{
   String menu = "\n" + "Short messages\n" + "Option 1: Encrypt a Message\n" + "Option 2: Decrypt a Message\n"
   + "Long messages\n" + "Option 3: Encrypt a Text File\n" + "Option 4: Decrypt a Text File\n"
   + "Other Functions\n" + "Option 5: Letter Distribution Analysis\n" + "Option 0: Exit Program\n";
   while (userChoice != 0) {
   if (again.equalsIgnoreCase("yes") || again.equalsIgnoreCase("y")) {
   System.out.print(menu);
   }
   System.out.print("What is your choice?: ");
   try {
   userChoice = in.nextInt();
   in.nextLine();
   } catch (InputMismatchException e) {
   System.out.println("Input was not a number.");
   in.nextLine();
   userChoice = -1;
   } // make a switch case for the menu
  
   switch (userChoice) {
   case 1:// encrypt message
  
   System.out.println("Enter your short message: ");
   message = in.nextLine();
   cipher = get_num(in);
   System.out.println(Caesar.encrypt(message, cipher));
   break;
  
   case 2:// decrypt message
   System.out.println("Enter your short message: ");
   message = in.nextLine();
   cipher = get_num(in);
   System.out.println(Caesar.decrypt(message, cipher));
   break;
   case 3:
   // encrypt text
   file = get_name(in);
   if (file.equals("n")) {
   break;
   } else {
   cipher = get_num(in);
   message = ReadWrite.read(ReadWrite.open_file(file, in));
   ReadWrite.write("Encrypted.txt", Caesar.encrypt(message, cipher));
   System.out.println("Encrypted message saved to Encrypted.txt");
   }
   break;
  
   case 4:// decrypt text
   file = get_name(in);
   if (file.equals("n")) {
   break;
   } else {
   cipher = get_num(in);
   message = ReadWrite.read(ReadWrite.open_file(file, in));
   ReadWrite.write("Decrypted.txt", Caesar.decrypt(message, cipher));
   System.out.println("Decrypted message saved to Decrypted.txt");
   }
   break;
  
   case 5:
   // letter distribution
   String temp = "a";
   while (!temp.equalsIgnoreCase("y") && !temp.equalsIgnoreCase("n")) {
   System.out.print("Is the message in a file (y/n)?: ");
   temp = in.nextLine();
   if (temp.equalsIgnoreCase("n")) {
   System.out.println("Enter your encrypted message: ");
   message = in.nextLine(); // end enter message
   } else {
   file = get_name(in);
   if (file.equals("n")) {
   break;
   } else {
   message = ReadWrite.read(ReadWrite.open_file(file, in));
   }
   } // end file message
   Caesar.printDistr(Caesar.letterDistr(message)); // exit message
  
   }
   break;
   case 0:
   System.out.println("Thank you for using the utility and goodbye~");
   break;
  
   default: // invalid input
   System.out.println("That wasn't a valid menu option."); // end switch
   if (userChoice != 0) {
   System.out.print("Would you like to see the menu again? (yes/no): "); //ADDED (yes/no)
   again = in.nextLine();
  
   }
   }
   } // end while loop menu
   in.close();
}
  
}
// end main method

public static int get_num(Scanner in) {
int num = 0;
while (num < 2 || num > 26) {
try {
System.out.print("What is the cipher?: ");
num = in.nextInt();
in.nextLine();
if (num < 2 || num > 26) {
System.out.println("Cipher must be between 2 and 26 inclusive.");
System.out.print("What is the cipher?: ");
num = in.nextInt();
in.nextLine();
}
} catch (InputMismatchException e) {
System.out.println("You need to use a number for the cipher. Try again.");
break;
}
}
return num;
}// end get_num method

public static String get_name(Scanner in) {
String file;
String temp;
String response;
System.out.print("Enter the name of the text file: ");
file = in.nextLine();
String ext = file.substring(file.length() - 4);
boolean txt = ext.equals(".txt");
if (!txt) {
temp = file + ".txt";
System.out.print("Entered file name doesn't end with \'.txt\' " + "attempting to ammend file extension.\n"
+ "New File name: " + temp + "\n" + "Is this correct? ");
response = in.nextLine();
if (response.equalsIgnoreCase("yes") || response.equalsIgnoreCase("y")) {
return temp;
} else {
System.out.println("Program can only use txt files. Try again.");
return "n";
}
} else {
return file;
}
}//end get_name method
} //end Main

Add a comment
Know the answer?
Add Answer to:
For this code after case 0, I need it to ask the user if they would...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • How would I edit this Java program to end if the user inputs 0 or less?...

    How would I edit this Java program to end if the user inputs 0 or less? public static void main(String[] args) { Scanner sc = new Scanner(System.in); // to read input String Message; int m=0; int i=1; System.out.print("Please enter the message you would like displayed: "); Message=sc.nextLine(); while(true) { System.out.print("How many times would you like your message displayed?"); m = sc.nextInt(); break; } do { System.out.println(Message); //When I enter 0 or a negative number it still runs at least once,...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is...

    Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is the completed program. Pseudocode: DISPLAY Login information PROMPT for username/password            output "Enter Username"            input userName          output "Enter password: "         input password //If successful, display information pertaining to the specific user, as well as prompt for logout    IF User type 'quit'      Exit Program    VALIDATE User Credentials   IF NOT Validated      Check number of Invalid Attempts         IF user attempts equal three THEN            DISPLAY error message...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT