Find Pairs of Numbers


/ Published in: Java
Save to your folder(s)

Write a code that will find the sum of any pairs of numbers


Copy this code and paste it in your HTML
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class FindPair
  5. {
  6. public static Vector<MyPair> v = new Vector<MyPair> ();
  7. public static void main (String args[])
  8. {
  9. // System.out.println ("Hello world");
  10.  
  11. int target = -1;
  12. if (args.length != 0)
  13. {
  14. target = Integer.parseInt(args[0]);
  15. }
  16. else
  17. {
  18. target = 5;
  19. }
  20. Iterator it = v.iterator();
  21.  
  22.  
  23. //int []array = {0,1,2,3,4,5};
  24. int []array = new int[100];
  25. int size = PopulateArrayFromFile(array);
  26. PopulateArrayFromFile(array);
  27. // for(int x=0; x < size; x++)
  28. // {
  29. //System.out.print (array[x]+ "... ");
  30. // }
  31.  
  32. FindPairs(array,size, target);
  33.  
  34. // System.out.println ("Size: " + v.size());
  35.  
  36. Enumeration e = v.elements();
  37. System.out.print("Array elements:");
  38.  
  39. for(int n=0; n < size;n++ )
  40. {
  41. System.out.print (array[n]+ " ");
  42. }
  43. System.out.println();
  44.  
  45. System.out.println ("target: " + target);
  46.  
  47. while (e.hasMoreElements())
  48. {
  49. MyPair p = (MyPair)e.nextElement();
  50. System.out.println ("x: " + p.x + " y: " + p.y);
  51. }
  52.  
  53. }
  54.  
  55. public static int PopulateArrayFromFile (int []arr)
  56. {
  57. int size = -1;
  58. try
  59. {
  60. BufferedReader br = new BufferedReader (new FileReader ("data.txt"));
  61. String l;
  62. Vector <String> vFileContents = new Vector<String>();
  63. while ((l = br.readLine())!= null)
  64. {
  65. // System.out.println ("l: " + l);
  66. vFileContents.add(l);
  67. }
  68.  
  69. size = vFileContents.size();
  70.  
  71. int count = 0;
  72. for (String x:vFileContents)
  73. {
  74. // System.out.println ("x: " + x);
  75. arr[count] = Integer.parseInt (x);
  76. // System.out.println ("arr: " + arr[count]);
  77. count++;
  78. }
  79. //System.out.println ("here size: " + arr.length);
  80.  
  81. // for (int n=0; n < size; n++)
  82. // {
  83. // System.out.println ("here2: " + arr[n]);
  84. // }
  85. }catch (IOException io)
  86. {
  87. io.printStackTrace ();
  88. }
  89. return size;
  90. }
  91.  
  92. public static void FindPairs(int []array, int size, int target)
  93. {
  94. for (int i =0; i < size; i++)
  95. {
  96. for (int j = 1; j < size; j++)
  97. {
  98. int sum = array[i] + array[j];
  99.  
  100. if (sum == target)
  101. {
  102. //System.out.println ("Adding elem: " + array[i] + " " + array[j]);
  103. v.addElement(new MyPair(array[i],array[j]));
  104. }
  105. }
  106. }
  107. }
  108.  
  109. }
  110.  
  111.  
  112. class MyPair
  113. {
  114. int x = 0;
  115. int y = 0;
  116. public MyPair (int x, int y)
  117. {
  118. this.x = x;
  119. this.y = y;
  120. }
  121. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.