简体   繁体   中英

processing single dimensional array java

I'm trying to create an array with user input from a dialog box. It's supposed to ask how many numbers the user wants to enter, then ask the user to input the numbers. The code is then supposed to output the numbers in reverse order. Below is the code I have so far.. it doesn't work. I did something wrong with trying to initialize the array with user input. I'm pretty new to java so any advice is welcome. Thanks in advance.

 public static void main(String[] args)
{
    String input;
    int space;
    double [] numbers;
    double count;
    String numberInput;
    double number;

    input = JOptionPane.showInputDialog
            (null, "How many numbers would you like to enter?");
    space = Integer.parseInt(input);

    numbers = new double[space];

    count = 0;
    while (count < space)
    {
        numberInput = JOptionPane.showInputDialog
                (null, "Enter a number to be sorted: ");
        number = Double.parseDouble(numberInput);

    for (int i = 0; i < numbers.length; i++)
    numbers[i] = number;
        count++;
    }

   double[] numbers2 = swapArray(numbers);
    JOptionPane.showMessageDialog(null, numbers2);
}
public static double[] swapArray(double[] array)
{
    double[] result = new double[array.length];

    for (int i = 0, j = result.length - 1;
            i < array.length; i++, j--)
    {
        result[j] = array[i];
    }
    return result;
}

}

Here is my take on your assignment. It should give you some ideas on how to deal with the problem, without giving you a copy-paste solution, nor the best-possible (to my own ability) structure/logic (but still giving you tips to point you in their direction):

package so_q33405148;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

public class main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            //TIP: You can use 'Scanner' instead of a 'Reader' here, to avoid having to parse strings into ints (scanner can do it for you)
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("How many numbers would you like to enter?");
            int count = Integer.parseInt(br.readLine());
            int input[] = new int[count];
            for (int i = 0; i < count; i++) {
                System.out.print("Enter a number to be sorted: ");
                //TIP: With some clever math, you can invert the array right as it's still filling up here, by manipulating a new int (so 'i' is unchanged, as it's the for-loop's index) using 'i' and 'input.length'...Or, with a little creativity and insight, you may even achieve the same result manipulating 'i' directly somewhere else...
                input[i] = Integer.parseInt(br.readLine());
            }

            System.out.println("Original array:\n" + Arrays.toString(input));

            //TIP: Better methods to reverse arrays and/or collections exist.
            //Take a look at SO question #3962766 (puritan solution without as much memory-footprint) and also Google about 'Arrays.asList()' and 'Collections.reverse()' (learn about collections-sorting)
            int reversedInput[] = new int[input.length];
            for (int i = 0; i < count; i++) {
                reversedInput[i] = input[count - i - 1];
            }

            System.out.println("Reversed array:\n" + Arrays.toString(reversedInput));
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM