0

So I have to write a program that accepts 10 numbers (ints) from the keyboard. Each number is to be stored in a different element of an array. Then my program must then display the contents of the array in reverse order.

int [] array = new int [10];
    for(int i = array.length -  1;i >= 0; i--)
    {
    int number = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter number " + (i+1)));
        array[i] = number;

            }
                JOptionPane.showMessageDialog(null, array[i]);


}

I tried to put the JOPtionPane.showMessageDialog outside of the loop but then the program can't find the integer "i". I don't know what to do here :/ Please help :P

2
  • You need two for loops. The first iterates from 0 to 9 and asks for the number and puts it in the array. The second iterates from 9 to 0 and prints the numbers in the array. Commented Mar 17, 2016 at 20:06
  • Okay, then you so much :) Commented Mar 17, 2016 at 20:11

4 Answers 4

2

You need to enter your data first, then display it thereafter in the order you desire...

int [] array = new int[10];
for (int i = 0; i < array.length - 1; i++) {
    int number = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter number " + (i + 1)));
    array[i] = number;
}

for (int i = array.length - 1; i >= 0; i--) {
    JOptionPane.showMessageDialog(null, array[i]);
}

I'd also be tempted to simply construct a StringBuilder for your final results and then just show the message dialog once only, rather than for every element of the array, but that's up to yourself :)

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

Comments

1

i belongs to the loop scope, that's why you can't use it outside of the loop.

To print the reversed array use another loop

// insert the data to the array
int [] array = new int [10];
for(int i = array.length -  1 ; i >= 0 ; i--) {
    int number = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter number " + (i+1)));
    array[i] = number;
}

// print the array
for (int i = 0 ; i < array.length ; ++i) {
    JOptionPane.showMessageDialog(null, array[i]);
}

3 Comments

Thanks for this, I have another question, I input numbers 10,9,8,7,6,5,4,3,2 (which is 9th number) and then it transfers me to another loop where I get output of 0,2,3,4,5,6,7,8,9,10. Why does it cut 1 number? You have any idea?
@PatrykMichta I'm not sure, I ran it in my computer and it was fine.
I got it, it was my bad. Thanks anyway :)
0

That's because you've declared in in for loop, so it has loop scope. Declare it before loop to reuse it after loop finishes

int i;
for(i = array.length -  1;i >= 0; i--)

After that, you can make another loop:

for(i = 0; i < array.length; i++)

to print it in reverse order.

2 Comments

Thanks for this, I have another question, I input numbers 10,9,8,7,6,5,4,3,2 (which is 9th number) and then it transfers me to another loop where I get output of 0,2,3,4,5,6,7,8,9,10. Why does it cut 1 number? You have any idea?
Oh, that's because second for prints whole table (which size was 10, so it print from index 0 to index 10) but you only have filled from index 9 to index 1, so the unfilled fields in array was 0 (initialization value)
0

You need two for loops. The first iterates from 0 to 9 and asks for the number and puts it in the array. The second iterates from 9 to 0 and prints the numbers in the array

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.