Advertisement
lrm2000

Array Merger Numerical Order 2016

Sep 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6.  
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. int[] firstArray = new int[10000];
  10.  
  11. System.out.println("Enter the values for the first array up to 10000 values, enter zero or a negative number to quit");
  12. int values = scan.nextInt();
  13. boolean check = true;
  14.  
  15. while (check) {
  16. for (int x = 0; x < firstArray.length; x++) {
  17. if (values > 0) {
  18. firstArray[x] = values;
  19. values = scan.nextInt();
  20. } else {
  21. check = false;
  22. }//end of if/else
  23. }//end of for loop
  24. }// end of while loop
  25.  
  26. System.out.println("Enter the values for the second array up to 10000 values, enter zero or a negative number to quit");
  27. int values2 = scan.nextInt();
  28. int[] secArray = new int[10000];
  29. boolean check2 = true;
  30.  
  31. while (check2) {
  32. for (int y = 0; y < secArray.length; y++) {
  33. if (values2 > 0) {
  34. secArray[y] = values2;
  35. values2 = scan.nextInt();
  36. } else {
  37. check2 = false;
  38. }//end of if/else
  39. }//end of for loop
  40. }// end of while loop
  41.  
  42. System.out.println("First Array:");
  43. for(int z = 0; z < firstArray.length; z++) {
  44. if(firstArray[z] > 0) {
  45. System.out.print(firstArray[z] + " ");
  46. }
  47. }//end of for loop
  48.  
  49. System.out.println("\n\nSecond Array:");
  50. for(int a = 0; a < secArray.length; a++) {
  51. if(secArray[a] > 0) {
  52. System.out.print(secArray[a] + " ");
  53. }
  54. }//end of for loop
  55.  
  56. int min = 0;
  57. int min2 = 0;
  58. boolean check3 = true;
  59. boolean check4 = true;
  60.  
  61. if(check3) {
  62. for(int b = 0; b <firstArray.length; b++) {
  63. if(firstArray[b] >= min) {
  64. min = firstArray[b];
  65. if(firstArray[b+1] < min && firstArray[b+1] > 0) {
  66. System.out.println("\n\nERROR: Array not in correct order");
  67. check3 = false;
  68. check4 = true;
  69. }//end of nested if
  70. } //end of if/else
  71. }//end of for
  72. for(int c = 0; c < secArray.length; c++) {
  73. if(secArray[c] >= min2) {
  74. min2 = secArray[c];
  75. if(secArray[c+1] < min2 && secArray[c+1] > 0){
  76. System.out.println("\n\nERROR: Array not in correct order");
  77. check3 = false;
  78. check4 = true;
  79. }//end of nested if
  80. }//end of if/else
  81. }//end of for
  82. }//end of for loop
  83.  
  84. System.out.print("\n\n");
  85.  
  86. int[] fin = new int[20000];
  87. int xx = 0;
  88.  
  89. for(int d = 0; d <10000; d++) {
  90. if(firstArray[d] > 0) {
  91. fin[d]+=firstArray[d];
  92. } else {
  93. fin[d] += secArray[xx];
  94. xx++;
  95. }//end of if/else
  96. }//end of for loop
  97.  
  98. Arrays.sort(fin);
  99. System.out.println("Merged Array:");
  100. for (int i = 0; i < fin.length; i++) {
  101. if(fin[i] >0) {
  102. System.out.print(fin[i] + " ");
  103. }//if loop
  104. }//for loop
  105.  
  106. }//end of program
  107. }//end of program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement