Guest User

Untitled

a guest
Dec 10th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class FindPair {
  5. int totalNum; //Total number
  6. Integer[] list; //The list of integers
  7. int size;
  8.  
  9. //Test functions
  10. public static void main(String args[]){
  11. FindPair demo = new FindPair(4);
  12. demo.readUserInput();
  13. demo.displayResult();
  14. }
  15.  
  16. //Constructor
  17. public FindPair(int listSize){
  18. totalNum = 0;
  19. size = listSize;
  20. list = new Integer[size];
  21. }
  22.  
  23. /**
  24. * Validate the total number that entered by user
  25. * @param num the number that inputed by user
  26. * @return true for valid number, false for invalid number
  27. */
  28. private boolean NumVal(String num)
  29. {
  30. //Check whether the input is integer
  31. try {
  32. Integer.parseInt(num);
  33. }
  34. catch(NumberFormatException | NullPointerException e) {
  35. System.out.println("Please enter a valid integer");
  36. return false;
  37. }
  38. //Check whether the number is greater than 0
  39. if(Integer.parseInt(num) < 1) {
  40. System.out.println("Please enter a positive number: ");
  41. return false;
  42. }
  43.  
  44. else
  45. return true;
  46. }
  47.  
  48. /**
  49. * Read user input for the list and total number
  50. */
  51. public void readUserInput()
  52. {
  53. Scanner sc = new Scanner(System.in);
  54. //Catch the exception when user input the number
  55. System.out.println("Please enter the first numer as the sum: ");
  56. String num = sc.nextLine();
  57. //valid the number, if the result is false than let the user enter the number again
  58. while(!NumVal(num)) {
  59. num = sc.nextLine();
  60. }
  61. totalNum = Integer.parseInt(num);
  62. //Read user input for list
  63. for(int i = 0; i < size; i++)
  64. {
  65. System.out.println("Please enter the " + (i+1) + " number in the list: ");
  66. num = sc.nextLine();
  67. //Validation of number
  68. while(!NumVal(num)) {
  69. num = sc.nextLine();
  70. }
  71. list[i] = Integer.parseInt(num);
  72. }
  73. sc.close();
  74. }
  75.  
  76. /**
  77. * Find pairs from the list
  78. * @return the array list of pairs
  79. */
  80. private ArrayList<Integer> findPair()
  81. {
  82. ArrayList<Integer> result = new ArrayList<Integer> ();
  83. //Find pairs by two loops
  84. for(int i = 0; i < size; i++)
  85. {
  86. for(int j = i + 1; j < size; j++)
  87. {
  88. if (list[i] + list[j] == totalNum && list[i] != list[j] && !result.contains(list[i]))
  89. {
  90. result.add(list[i]);
  91. result.add(list[j]);
  92. break;
  93. }
  94. }
  95. }
  96. return result;
  97. }
  98.  
  99. /**
  100. * Display the pairs that have the sum of total number
  101. */
  102. public void displayResult()
  103. {
  104. //Invoke the method of findPair
  105. ArrayList<Integer> result = findPair();
  106. System.out.println("Result(s): ");
  107. for(int i = 0; i < result.size() - 1; i+=2)
  108. {
  109. System.out.print("[" + result.get(i) + ", " + result.get(i+1) + "] ");
  110. }
  111. }
  112. }
Add Comment
Please, Sign In to add comment