user3358884
user3358884

Reputation: 109

Reverse numbers using java arrays

Hello I am new to java and am trying to do this assignment but I can't get the needed output which is the reverse order of the numbers. Can any one help me what I am missing? Thank you so much! My Code's output looks like this:

How many floating point numbers do you want to type: 5 Type in 1. number: 5,4 Type in 2. number: 6 Type in 3. number: 7,2 Type in 4. number: -5 Type in 5. number: 2

Given numbers in reverse order:

Assignment

Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.

Program is written to a class called ReverseNumbers. Example output

How many floating point numbers do you want to type: 5 Type in 1. number: 5,4 Type in 2. number: 6 Type in 3. number: 7,2 Type in 4. number: -5 Type in 5. number: 2

Given numbers in reverse order: 2.0 -5.0 7.2 6.0 5.4

My code:

    import java.util.Scanner;  
public class apples {  
    public static void main(String[] args) {  
        Scanner reader = new Scanner(System.in);  

        double[] numbers;  

        System.out.print("How many floating point numbers do you want to type: ");  
        int size = reader.nextInt();  
        numbers = new double[size];  

        for (int i=0; i < numbers.length; i++) {  
            System.out.print("Type in "+(i+1)+". number:");  
            numbers[i-1] = reader.nextDouble();  

        }  

        System.out.println();  
        System.out.println("Given numbers in reverse order:");  

        for (int i=numbers.length-1; i <= 0; i--) {  

              System.out.println( numbers[i]);  
        }  
    }  
}  

Upvotes: 1

Views: 5514

Answers (3)

Gowsy
Gowsy

Reputation: 1

int[] numbers;
System.out.print("How many integer point numbers do you want to type: ");
int size = reader.nextInt();
numbers = new int[size];

for (int i=0; i < size; i++) {
    //System.out.print("Type in "+(i+1)+". number:");
    numbers[i] = reader.nextInt();
}

System.out.println();
System.out.println("Given numbers in reverse order:");

for (int i=size-1; i >= 0; i--) {
      System.out.print( numbers[i]);
}

Upvotes: 0

ItachiUchiha
ItachiUchiha

Reputation: 36722

In the first loop you are using i+1 and not i++, so the value of i is never incremented. So, your

numbers[i-1] becomes numbers[-1], which should throw an error. I am astonished, now your program is even giving you an output !

Try this

for (int i=0; i < numbers.length; i++) {  
        System.out.print("Type in "+(i+1)+". number:");  
        //numbers[i-1] = reader.nextDouble();  
        numbers[i] = reader.nextDouble(); 
    } 

and in second loop. You are checking from last to first so i>=0 instead of i<=0

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

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

The second for loop condition is wrong. It should be:

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

because i <= 0 won't be true if numbers.length - 1 is greater than 0, therefore your for loop won't run its body.

Also in the first for loop, the index of numbers are wrong. It should be:

numbers[i] = reader.nextDouble();

This is because if i = 0, then numbers[i - 1] will be numbers[-1], and that's an invalid index.

Upvotes: 3

Related Questions