Advertisement
isabelleromhagen

Assignment1

Sep 18th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. package com.company;
  2. //package inputHandler;
  3. import java.util.Scanner;
  4. import java.util.Random;
  5.  
  6. public class Main {
  7.  
  8. private static Scanner sc = new Scanner(System.in);
  9.  
  10. public static void main(String[] args) {
  11.  
  12.  
  13. // Main menu (new text, new number and exit) message if you choose to exit.
  14. String userInput;
  15. //String remove;
  16.  
  17.  
  18. while (true) {
  19. System.out.println("Please enter 'new text', 'new number' or 'exit'."); //prompt user for input
  20.  
  21. userInput = sc.nextLine(); //get input
  22.  
  23.  
  24. switch (userInput) {
  25. case "new text":
  26. newText(); //call the method newText
  27. break;
  28. case "new number":
  29. newNumber(); //call the method newNumber
  30. break;
  31. case "exit":
  32. System.exit(0); //exit program
  33. break;
  34. default:
  35. System.out.println("Invalid input"); //print message to user
  36.  
  37.  
  38. }
  39.  
  40. }
  41.  
  42.  
  43. }
  44.  
  45.  
  46. private static void newText () {
  47. // loops until breaks.
  48. while (true) {
  49. System.out.println("Please choose between following: remove char, text value, randomize or write return to get back to main menu.");
  50.  
  51. // Wating to get user input.
  52. String userInput = sc.nextLine();
  53.  
  54. // Choosing between options from user input.
  55. switch (userInput) {
  56. case "remove char":
  57. System.out.println("Please enter a line of text");
  58. userInput = sc.nextLine();
  59. System.out.println("Please enter the char you want removed");
  60. String remove = sc.nextLine();
  61. System.out.println(removeChar(userInput, remove));
  62. break;
  63. case "text value":
  64. System.out.println("Please enter a line of text");
  65. userInput = sc.nextLine();
  66. System.out.println("Your text value is " + textValue(userInput));
  67. break;
  68. case "randomize":
  69. System.out.println("Please enter a line of text");
  70. userInput = sc.nextLine();
  71. randomize(userInput);
  72. break;
  73. case "return":
  74. //Back to menu
  75. break;
  76. // if no result, write text to user.
  77. default:
  78. System.out.println("Please enter valid input");
  79.  
  80. }
  81. }
  82. }
  83.  
  84.  
  85. //Method removeChar() this method replaces one char with another.
  86. private static String removeChar (String userInput, String remove){
  87. return userInput.replace(remove, "");
  88. }
  89.  
  90.  
  91. private static int textValue(String userInput) {
  92. int value = 0;
  93.  
  94. // loops trough all chars in the string.
  95. for (char c : userInput.toCharArray()) {
  96. // Failsafe, if user input is not any of english chars, break.
  97. if (getValueFromChar2(Character.toLowerCase(c)) < 0) {
  98. System.out.println("Input error. " + c + " do not return any points");
  99. break;
  100. }
  101. // adds for each char to int value.
  102. else {
  103. value += getValueFromChar2(Character.toLowerCase(c));
  104. }
  105. }
  106.  
  107. return value;
  108. }
  109.  
  110.  
  111. //Method randomize() this method shuffles characters within a string.
  112.  
  113. private static String randomize (String userInput){
  114. char[] characters = userInput.toCharArray(); //Converts the string to an array of chars.
  115. for (int i = 0; i < characters.length; i++) { //Replaces each char with a random one within the original string
  116. int randomIndex = randomWithRange(characters.length);
  117. char temp = characters[i];
  118. characters[i] = characters[randomIndex];
  119. characters[randomIndex] = temp;
  120. }
  121. return new String(characters); //Returns a string created from the chars
  122. }
  123.  
  124.  
  125. private static void newNumber() {
  126. // loops until breaks.
  127. while (true) {
  128. System.out.println("Please choose between following: remove char, text value, randomize or write return to get back to main menu.");
  129.  
  130. // Waiting to get user input.
  131. String userInput = sc.nextLine();
  132.  
  133. // Choosing between options from user input.
  134. switch (userInput) {
  135. case "get random number":
  136. System.out.println("Please write the bound of the number you want to random");
  137. // waiting for user input.
  138. userInput = sc.nextLine();
  139. System.out.println("Your random number is " + randomWithRange(Integer.parseInt(userInput)));
  140. break;
  141. case "print plus":
  142. //TODO add text value function
  143. break;
  144. case "create calculator":
  145. // TODO add randomzie function
  146. break;
  147. case "return":
  148. // Breaking the loop.
  149. break;
  150. // if no result, write text to user.
  151. default:
  152. System.out.println("Please enter valid input");
  153.  
  154. }
  155. }
  156. }
  157.  
  158. //Metoden printPlus - använd randommetoden som Ludvig gör
  159.  
  160. public static int printPlus ( int userInput){
  161.  
  162. String s = "";
  163.  
  164. while (true) {
  165.  
  166. System.out.println("Do you want to print a specific number or a random one? Please enter 'Specific' or 'Random'");
  167.  
  168. String input = sc.nextLine(); //Get user input to determine whether to randomize or not
  169.  
  170. if (input.equals("Specific")) {
  171.  
  172. int userNumber = sc.nextInt(); //Get a specific number from the user
  173.  
  174. for (int i = 0; i <= userNumber; i++) {
  175.  
  176. s = s + "+";
  177. System.out.println(s);
  178. }
  179.  
  180. } else if (input.equals("Random")) {
  181.  
  182. int range = 10;
  183.  
  184. double d = randomWithRange(range); //call the method getRandomNumber(int range)
  185.  
  186. for (int i = 0; i <= d; i++) { //loop the random number
  187.  
  188. s = s + "+";
  189. System.out.println(s); //print the plus
  190. }
  191.  
  192.  
  193. }
  194.  
  195.  
  196. }
  197.  
  198.  
  199. }
  200.  
  201.  
  202. private static int getValueFromChar2(char c) {
  203. // Making the string of the alphabet into chars in an array, starting with a space to make this value to zero.
  204. char alpa[] = " abcdefghijklmnopqrstuvwxyz".toCharArray();
  205. int atChar = 0;
  206. // looping the array and give +1 value at atChar each time the array is not the c value.
  207. for (atChar = 0; alpa[atChar] != c; atChar++) {
  208. if (atChar > alpa.length) {
  209. // the char value do not exist, returning -1 to make you see the error.
  210. return -1;
  211. }
  212. }
  213.  
  214. return atChar;
  215. }
  216.  
  217.  
  218.  
  219. private static int randomWithRange ( int bound){
  220. Random newRandom = new Random();
  221. int myNumber = newRandom.nextInt(bound);
  222. return myNumber;
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement