Guest User

Untitled

a guest
Jun 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Map.Entry;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Firstly, read an positive integer from user. Secondly, read a list of positive
  8. * integers one by one from user. Finally, find all pairs of numbers in the list
  9. * that their sum is equal to the first number.
  10. *
  11. * When user input numbers, the program will keep asking user to input until get a
  12. * valid integer.
  13. *
  14. * Instruction to run the code:
  15. * 1. Compile the code use following command: javac PairFinder.java
  16. * 2. Run complied use following command: java PairFinder
  17. *
  18. * Java version: 1.8
  19. *
  20. * @author tzha0024@student.monash.edu
  21. *
  22. */
  23. public class PairFinder {
  24. private static int firstNumber; // User input first number
  25. private static ArrayList<Integer> theList; // User input a list of numbers
  26. private static HashMap<Integer, Integer> pairs; // Pairs found
  27. private static Scanner sc; // User input reader
  28.  
  29. /**
  30. * Main method.
  31. *
  32. * @param args
  33. */
  34. public static void main(String[] args) {
  35. sc = new Scanner(System.in);
  36.  
  37. readFirstNumber();
  38.  
  39. readTheList();
  40.  
  41. findPairs();
  42.  
  43. sc.close();
  44. }
  45.  
  46. /**
  47. * Read an positive integer number from user. The program will keep asking
  48. * user to input until get a valid integer.
  49. */
  50. private static void readFirstNumber() {
  51. boolean flag = true;
  52. while (flag)
  53. {
  54. System.out.println("Please input a positive integer: ");
  55. String numberStr = sc.nextLine();
  56.  
  57. if (isValidNumber(numberStr))
  58. {
  59. int tempNum = Integer.parseInt(numberStr);
  60. if (tempNum > 0) {
  61. firstNumber = tempNum;
  62. flag = false;
  63. }
  64. else
  65. {
  66. System.out.println("[ERROR] Your input is not a positive integer!");
  67. }
  68. }
  69. }
  70. }
  71.  
  72. /**
  73. * Read a list of positive integer numbers one by one from user. The program will
  74. * keep asking user to add numbers to the list. When user input '0', the program
  75. * will stop asking and the list is complete.
  76. */
  77. private static void readTheList() {
  78. theList = new ArrayList<Integer>();
  79. System.out.println("Please input add numbers to the list one by one. Input '0' to finish the list.");
  80. boolean flag = true;
  81. while (flag) {
  82. // Show user the current numbers of the list
  83. printList();
  84.  
  85. System.out.println("Please add your next positive integer to the list. Input '0' to finish the list. ");
  86. String numberStr = sc.nextLine();
  87.  
  88. if (isValidNumber(numberStr))
  89. {
  90. int tempNum = Integer.parseInt(numberStr);
  91. if (tempNum > 0) {
  92. theList.add(tempNum);
  93. }
  94. else
  95. {
  96. // User input 0, stop the loop
  97. flag = false;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. /**
  104. * Find all pairs of numbers in the list that their sum is equal to
  105. * the first number.
  106. * 1. Duplicates such as [4,4] are not considered.
  107. * 2. Same numbers in different order such as [4,8] and [8,4] will
  108. * be consider as a single pair.
  109. *
  110. * The algorithm is nested loop. Outer loop find a number1 which is
  111. * less than the firstNumber and not equals to the half of firstNumber.
  112. * Then, inner loop iterate from the next index of outer loop to the end of
  113. * the list to find number2 which satisfy: number1 + number2 = firstNumber.
  114. */
  115. private static void findPairs() {
  116. pairs = new HashMap<Integer, Integer>();
  117. int listSize = theList.size();
  118.  
  119. // iterate the list
  120. for (int i = 0; i < listSize - 1; i++)
  121. {
  122. int number1 = theList.get(i);
  123.  
  124. // check the number less than firstNumber and not duplicates
  125. if (number1 < firstNumber && number1 != (firstNumber - number1))
  126. {
  127. // iterate the rest of the list
  128. for (int j = i; j < listSize; j++)
  129. {
  130. int number2 = theList.get(j);
  131.  
  132. // Check if number1 + number2 = firstNumber
  133. if (number2 == (firstNumber - number1))
  134. {
  135. // Check duplicates
  136. if (!pairs.containsKey(number2) && !pairs.containsValue(number2))
  137. {
  138. pairs.put(number1, number2);
  139. }
  140. }
  141. }
  142. }
  143. }
  144.  
  145. // print the pairs
  146. System.out.println("=============================");
  147. System.out.println("First number: " + firstNumber);
  148. printList();
  149. System.out.println("All pairs of numbers are: ");
  150. for (Entry<Integer, Integer> e : pairs.entrySet())
  151. {
  152. System.out.println("[" + e.getKey() + "," + e.getValue() + "]");
  153. }
  154. }
  155.  
  156. /**
  157. * Show the current numbers of the list.
  158. */
  159. private static void printList() {
  160. System.out.print("Current list: [ ");
  161. if (theList.size() > 0) {
  162. for(Integer i : theList)
  163. {
  164. System.out.print(i + " ");
  165. }
  166. }
  167. System.out.print("]");
  168. System.out.println();
  169. }
  170.  
  171. /**
  172. * Check whether user's input string is a valid integer or not.
  173. * @param numberStr User's input string
  174. * @return True if user's input string is a valid integer
  175. */
  176. private static boolean isValidNumber(String numberStr) {
  177. boolean flag = false;
  178. if (numberStr.trim().length() > 0)
  179. {
  180. if (numberStr.length() < 10)
  181. {
  182. flag = true;
  183. for (int i = 0; i < numberStr.length(); i++)
  184. {
  185. char c = numberStr.charAt(i);
  186. if (c < '0' || c > '9')
  187. {
  188. flag = false;
  189. System.out.println("[ERROR] Your input is not a valid integer!");
  190. break;
  191. }
  192. }
  193. }
  194. else
  195. {
  196. System.out.println("[ERROR] Please only input within 10 digits.");
  197. }
  198. }
  199. else
  200. {
  201. System.out.println("[ERROR] Please do not input empty space.");
  202. }
  203. return flag;
  204. }
  205. }
Add Comment
Please, Sign In to add comment