0

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?

3
  • 2
    First thing done wrong is not saying what the error is :) Commented Feb 1, 2013 at 18:22
  • @user2033503. Guess it is working now for you after applying the change Commented Feb 1, 2013 at 18:36
  • @DaveNewton You're just educating him aren't you, you probably saw that in an instance :P Commented Feb 1, 2013 at 18:39

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Change

for(int counter3 = 0, counter4 = arraysize

to

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

Comments

0

Try

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

It works. The exception is gone.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.