0
   int [] ary1 = new int [100];
   int [] ary2 = new int [100];


   System.out.println("Enter no more than 100 integers in ascending order. To end, enter a negative number.");
   int num1 = user.nextInt();
   int count1 = 0;
   while ((num1 > -1) && (count1 < ary1.length)){
   ary1[count1] = num1;
   count1++;
   num1 = user.nextInt();

this is the first array.

users can enter no more than 100 integers, will stop if user entered a negative number.

    if (num1 < 0){ 
    System.out.println("Enter your second list of integers from least to greatest. To end, enter a negative number.");
    int num2 = user.nextInt();
    int count2 = 0;
    while ((num2 > -1) && (count2 < ary2.length)){
    ary2[count2] = num2;
    count2++;
    num2 = user.nextInt();

this is the second array.

users can enter no more than 100 integers, will end if user entered a negative number.

    if (num2 < 0){
      for (int one = 0; one < count1; one++){
        System.out.print(ary1[one]+" ");}   

print out the first list of integers

      System.out.println("");     **//skip a line**

      for (int two =0; two < count2; two++){
        System.out.print(ary2[two]+" ");}   p

rint out the second list of integers

      System.out.println("");     **//skip a line**

the problem is here, it won't check if the arrays are in ascending order. ↓↓↓

       int inOrder = 0;    
      for (int check1 = 0; check1 < count1-1; check1++){
        if (ary1[check1] > ary1[check1++]){
          System.out.println("Error: Array not in correct order.");
          break;
        }else{
           inOrder = 1;
        }}

        for (int check2 = 0; check2 < count2-1; check2++){
        if (ary2[check2] > ary2[check2++]){
          System.out.println("Error: Array not in correct order.");
          break;
        }else{
          inOrder = 1;
        }} 



        }}
    }  }}}

if inOder = 1, i will need to merge the 2 arrays.

Sam
  • 7,252
  • 16
  • 46
  • 65

2 Answers2

1

Your problem is with how you are incrementing your check1 and check2 variables:

if (ary1[check1] > ary1[check1++]){ }
if (ary2[check2] > ary2[check2++]){ }

For these loops, you are using check1++ and check2++. Putting ++ at the end of a variable name evaluates to the expression value prior to the increment. Putting ++ BEFORE the variable name increments the variable first and then evaluates the expression. The fix is to just relocate your ++ like this:

if (ary1[check1] > ary1[++check1]){ }
if (ary2[check2] > ary2[++check2]){ }

Look at this post to understand prefix/postfix operators better:

Java: Prefix/postfix of increment/decrement operators?

Update: Other user noticed that the increment is actually happening twice due to loop control also doing ++. To fix, replace ++check1 with check + 1. I almost feel guilty having this accepted as correct!

Community
  • 1
  • 1
David Fleeman
  • 2,588
  • 14
  • 17
  • this would only check a pair each time as check1 or check2 is being incremented by 2 each loop – Geezer68 Jan 22 '14 at 22:05
  • Example 0,2,1,3 = first loop compares 0 and 2 which is valid, and then compares 1 and 3 which is also correct. It skips checking 2 and 1 so would not give correct result – Geezer68 Jan 22 '14 at 22:12
  • Good catch @Geezer68 -- did not look too closely at external loop code – David Fleeman Jan 22 '14 at 22:13
  • I'd help them with the inOrder check as well - should initially be set to a true value and only set to false before the break statement:) – Geezer68 Jan 22 '14 at 22:17
0

The ++ is the problem:

if (ary1[check1] > ary1[check1++])

Change this to:

if (ary1[check1] > ary1[check1 + 1])
Geezer68
  • 391
  • 2
  • 8