fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class lab3{
  5. public static Scanner scanner = new Scanner(System.in);
  6. public static int selection = 0;
  7. public static void main(String[] args){
  8. while(true){
  9. selection = 0;
  10. display();
  11. scanner = new Scanner(System.in);
  12. if(selection == 1)decode();
  13. if(selection == 2)encode();
  14. if(selection == 3)alphabet();
  15. if(selection == 4)System.exit(0);
  16. //end of if statements
  17. }
  18.  
  19. }//end of main method
  20.  
  21. public static void display(){
  22. while(true){
  23. System.out.println(" ");
  24. System.out.println("1. Decode a message.");
  25. System.out.println("2. Encode a message.");
  26. System.out.println("3. Display the alphabet.");
  27. System.out.println("4. Exit the program.");
  28. System.out.println(" ");
  29. System.out.print("Please enter your selection: ");
  30. selection = scanner.nextInt();
  31. System.out.println(" ");
  32. if(selection > 4 || selection < 1){
  33. System.out.println("please pick a proper number");
  34. System.out.println(" ");}
  35. else{
  36. break;
  37. }
  38. }
  39. }// end of diplay
  40. public static void alphabet(){
  41. System.out.println("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  42. System.out.println(" ");
  43. }// end of alphabet
  44. public static void decode(){
  45. String userinputdecode = "";
  46. System.out.println("Enter a Phrase to decode: ");
  47. userinputdecode = scanner.nextLine().toUpperCase();
  48. System.out.println(userinputdecode);
  49.  
  50. char arrUIdecode[] = userinputdecode.toCharArray();
  51. for(int i = 0; i < arrUIdecode.length; i++){
  52. if(arrUIdecode[i]=='A'){
  53. System.out.print("Z");
  54. continue;
  55. }
  56. if(arrUIdecode[i]==' '){
  57. System.out.print(" ");
  58. continue;
  59. }
  60. System.out.print(Character.toChars(arrUIdecode[i]-1 ));
  61. }
  62. }// end of decode
  63. public static void encode(){
  64. String userinputencode = "";
  65. ///////User input/////////
  66. System.out.println("Enter a Phrase to encode: ");
  67. userinputencode = scanner.nextLine();
  68. userinputencode = userinputencode.toUpperCase(); //converting to uppercase
  69. System.out.println(userinputencode);
  70. char arrUserInput[] = userinputencode.toCharArray();
  71. for(int i=0; i<arrUserInput.length;i++){
  72. if(arrUserInput[i]=='Z'){
  73. System.out.print("A");
  74. continue;
  75. }
  76. if(arrUserInput[i]==' '){
  77. System.out.print(" ");
  78. continue;
  79. }
  80. System.out.print(Character.toChars(arrUserInput[i]+1));
  81. }
  82. System.out.println(" ");
  83. }// end of encode
  84. }//end of class
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty