rahme24
rahme24

Reputation: 161

How do I reverse the integers in one array using another array?

I have gotten 10 values from the user, and I am suppose to put that into an array and call it into a method, and then call another method which contains an array that reverses the integers of the first array without using global variables. For example, the user gives me values 1,2,3, etc.. and I store that in the first array, then in the second array I output 3,2,1.

I have part of the code that gets the integers from the user, but I don't know how to reverse the array without using global variables. The code doesn't run when it gets to the second method at values[i];

Here is what I have so far:

public static void main(String[] args) {

    getInput();
    reverseInput();
    System.exit(0);
}  

public static void getInput() {

    Scanner keyboard = new Scanner(System.in);

    int[] values;
    values = new int[10];

    //Ask the user to enter 10 integers
    System.out.println("Please enter ten numbers.");
    for (int i = 0; i<values.length; i++) {//for loop to display the values entered
        values[i] = keyboard.nextInt();
        System.out.println("Value" + (i +1) + " is " + values[i]);
    }
}

public static void reverseInput() { 

    System.out.println("Now we are going to reverse the numbers.");
    Scanner keyboard = new Scanner(System.in);

    int [] values;
    values = new int[10];

    for (int i = (values.length -1); i>= 0; i--) {//for loop to display integers in reversed order
        values[i];
        System.out.println(values[i]);
    }
}

Upvotes: 0

Views: 5868

Answers (4)

user497849
user497849

Reputation:

use the "getInput" as basis to get the 10 numbers, then after the "for" loop, just do another for loop in reverse

  public static void reverseInput()
  {

    Scanner keyboard = new Scanner(System.in);

    int[] values;
    values = new int[10];

    //Ask the user to enter 10 integers
    System.out.println("Please enter ten numbers, we are going to reverse the numbers");
    for (int i = 0; i<values.length; i++) //for loop to display the values entered
    {
      values[i] = keyboard.nextInt();
    };
    int[] revValues;
    revValues = new int[10];
    for (int i = (values.length -1); i>= 0; i--) //for loop to display integers in reversed order
    {
      revValues[ revValues.length -1 -i ] = values[ i ];
      System.out.println( revValues[ i ] );
    }
}

Upvotes: 1

Elias Dorneles
Elias Dorneles

Reputation: 23806

You can use the ArrayUtils.reverse from Commons Lang:

ArrayUtils.reverse(values);

Upvotes: 2

QuantumMechanic
QuantumMechanic

Reputation: 13946

Ok, you understand how to display the elements in reference order. As you wrote:

for (int i = (values.length - 1); i >= 0; i--) {
    System.out.println(values[i]);
}

So given that, how would you put the elements of values (say it has 5 elements) in another array (call it reversedValues) such that values[4] was put into reversedValues[0] and so on up to values[0] being put into reversedValues[4]?

Upvotes: 1

Michaeldcooney
Michaeldcooney

Reputation: 1445

First, if you are not going to be using global variables, then you will need to pass the array from the get input method to the reverseInput method. Something like this:

int[] values = getInput();
reverseInput(values);

Then to output the array in your reverseInput method, it would have to look like this:

    public static void reverseInput(int[] values) {
        for(int i = (values.length - 1); i >= 0; i--){
            System.out.println(values[i]);
        }
    }

Upvotes: 3

Related Questions