user2033503
user2033503

Reputation: 1

Rearranging int values in an int array java

So I have to make a Java program in which a user says how many values they want to enter, enter that many values in an array, the array is printed. Then I have to reverse the elements within the array (not reverse the print or make a new array), and print the values once more. This is the following code I have:

package reversearray;

import java.util.*;

public class Swap_main {

    /**
     * Taylor Marino
     */
    public static void main(String[] args) {
        int arraysize = 0, junk, junk2;
        Scanner reader = new Scanner(System.in);
        System.out.println("How many values are you going to enter?");
        arraysize = reader.nextInt();
        int[] array = new int[arraysize];
        System.out.println("You will now be asked to enter your values, one at a time");
        for(int counter = 0; counter < arraysize; counter++){
            System.out.println("Enter next value");
            array[counter] = reader.nextInt();
        }
        System.out.println("The values you entered are: ");
        for(int counter2 = 0; counter2 < arraysize; counter2++)
            System.out.print(array[counter2] + ", ");
        for(int counter3 = 0, counter4 = arraysize; counter3 != counter4; counter3++, counter4--){
            junk = array[counter3];
            junk2 = array[counter4];
            array[counter4] = junk;
            array[counter3] = junk2;
        }
        System.out.println("The values you entered are (in reverse order): ");
        for(int counter5 = 0; counter5 < arraysize; counter5++)
            System.out.print(array[counter5] + ", ");
    }

}

However I receive an error in this loop:

        for(int counter3 = 0, counter4 = arraysize; counter3 != counter4; counter3++, counter4--){
            junk = array[counter3];
            junk2 = array[counter4];
            array[counter4] = junk;
            array[counter3] = junk2;
        }

I dont get what is wrong here, but it says there is an error with array[counter4] = junk; what am I doing wrong?

Upvotes: 0

Views: 261

Answers (4)

DangerDan
DangerDan

Reputation: 529

Could be slightly more consise and use only one loop variable

int maxIndex = array.length-1; // Zero based
int midIndex = (maxIndex/2);   // only swap half, otherwise will swap back to original
for(int counter3 = 0; counter3 <= midIndex; counter3++){
    junk = array[counter3];
    array[counter3] = array[maxIndex-counter3];
    array[maxIndex-counter3] = junk;
}

Upvotes: 0

user1760178
user1760178

Reputation: 6657

Try

for(int counter3 = 0, counter4 = arraysize -1 ; counter3 < counter4 ; counter3++, counter4--){

It works. The exception is gone.

Upvotes: 0

John B
John B

Reputation: 32949

Change

for(int counter3 = 0, counter4 = arraysize

to

for(int counter3 = 0, counter4 = arraysize-1

Upvotes: 0

G. Bach
G. Bach

Reputation: 3909

That'll give you an ArrayOutOfBoundsException since arrays are indexed from 0 to length-1. Start off with counter4 = arraysize-1.

EDIT: Also, you should change

counter3 != counter4

to

counter3 < counter4

since on arrays of odd length, the first condition will never give you true.

Upvotes: 3

Related Questions