0

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]);
    }
}
0

4 Answers 4

3

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]);
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Okay, that makes sense, but my question now is where exactly do I do that? It won't compile when I did it in the reverseInput method, so would I maybe do it in the main method?
You need to remove the line where you have "values[i];"
I removed that. Is that where I pass the array?
You pass the array in the main method from the getInput to the reverseInput like the first two lines of code above show.
2

You can use the ArrayUtils.reverse from Commons Lang:

ArrayUtils.reverse(values);

3 Comments

I think the idea is that he's supposed to write his own reverse function.
Yes I am supposed to write the array that reverses the input of the first array.
ahh! sorry! well, if you're doing it to learn, I suggest you do it step-by-step just in a piece of paper first, and THEN you try to code it.
1

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]?

Comments

1

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

2 Comments

But I have to do two separate arrays, the second one using the values of the first.
OK, I've updated the answer to fill "revValues" array with values of "values" array in reverse.

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.