Advertisement
Guest User

problem

a guest
Oct 20th, 2016
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lab08b
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. Scanner sc = new Scanner (System.in);
  9. String input = getText(sc);
  10.  
  11. if (input.equals("!!!"))
  12. {
  13. System.out.println("Goodbye!");
  14. }
  15. while (!input.equals("!!!"))
  16. {
  17. String encoded = rot13(input);
  18. displayResults(input, encoded);
  19. input = getText(sc);
  20. if (input.equals("!!!"))
  21. {
  22. System.out.println("Goodbye!");
  23. }
  24. }
  25.  
  26. }
  27.  
  28.  
  29. private static String getText(Scanner inScanner)
  30. {
  31. System.out.print("Enter your text to encrypt (!!! to quit): ");
  32. String original = inScanner.nextLine();
  33. while (original.equals(""))
  34. {
  35. System.out.println("ERROR! String must not be empty!");
  36. System.out.print("Enter your text to encrypt (!!! to quit): ");
  37. original = inScanner.nextLine();
  38. }
  39. return original;
  40. }
  41.  
  42.  
  43. private static String rot13(String input)
  44. {
  45. String str = "";
  46.  
  47. for (int i = 0; i < input.length(); i++)
  48. {
  49. char ch = input.charAt(i);
  50. if (ch >= 'A' && ch <= 'Z')
  51. {
  52. ch = (char) (ch + 13);
  53. if (ch > 'Z')
  54. {
  55. ch = (char)(ch - 26);
  56. }
  57. }
  58. else if (ch >= 'a' && ch <= 'z')
  59. {
  60. ch = (char)(ch + 13);
  61. if (ch > 'z')
  62. {
  63. ch = (char)(ch - 26);
  64. }
  65. }
  66. str = str + ch;
  67. }
  68. return str;
  69. }
  70.  
  71. private static void displayResults(String inText, String encText)
  72. {
  73. System.out.print("+");
  74. for (int i = 13 + encText.length(); i > 0; i--)
  75. {
  76. System.out.print("-");
  77. }
  78. System.out.println("+");
  79.  
  80. System.out.println("| ORIGINAL: " + inText + " |");
  81. System.out.println("| ENCRYPTED: " + encText + " |");
  82.  
  83. System.out.print("+");
  84. for(int i = 13 + encText.length(); i > 0; i--){
  85. System.out.print("-");
  86. }
  87. System.out.println("+");
  88. }
  89.  
  90. /*private static String shiftMessage(String input, int n)
  91. {
  92.  
  93. }*/
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement