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?