i wrote a code using array and method that allow the user to enter any number of numbers and display the numbers sorted from the smallest number to the largest number however the program works but it doesn't show the numbers here is the code that i wrote

import java.util.Scanner;

public class Numbers {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("How many numbers you want to enter? ");
        int size = s.nextInt();
        int i;
        double[] numbers1 = new double[size];
        System.out.println("Enter " + numbers1.length + " numbers: ");
        getNumbers(numbers1);

        double[] numbers2 = new double[numbers1.length];
        for (i = 0; i < numbers1.length; i++) {
            numbers2[i] = numbers1[i];
        }
        displayNumbers(numbers1);
        System.out.println("The numbers after sorting are: ");
        sortNumbers(numbers2);
        displayNumbers(numbers2);
    }

    public static void getNumbers(double[] numbers) {
        Scanner s = new Scanner(System.in);
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = s.nextDouble();
        }
    }

    public static void sortNumbers(double[] numbers) {
        double temp;
        double pass;
        for (pass = 0; pass < numbers.length; pass++) {
            for (int i = 0; i < numbers.length - 1; i++) {
                if (numbers[i] > numbers[i + 1]) {
                    temp = numbers[i];
                    numbers[i] = numbers[i + 1];
                    numbers[i + 1] = temp;
                }
            }
        }
    }

    public static void displayNumbers(double[] numbers) {
        Scanner s = new Scanner(System.in);
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = s.nextDouble();
            System.out.print(numbers + " ");
        }

        System.out.println();
    }

}

2 Answers

1
Lino On

Your displayNumbers() method is wrong. In the loop you wrote:

numbers[i] = s.nextDouble();
System.out.print(numbers + " " );

You're trying to read again 4 doubles (everytime you call that method) and you're printing the whole array (which doesn't do what you'd expect). What you probably want is this:

System.out.print(numbers[i] + " " );
0
Community On

Your code is inefficient and unclear.

You don't need to create a new Scanner everytime and also why instead of println the array with Arrays.toString(double[]).

You should also make more cleaner instructions and variable names.

Here is an example of how the code should be

    public static void main(String[] args)
    {
        //create a new Scanner with a name that defines it
        Scanner scanner = new Scanner(System.in);
        //print your instructions
        System.out.println("How many numbers would you like to sort?");
        //print "> " to let the user to know he should be entering values
        System.out.print("> ");
        //read the number the user has entered which we will define as how many numbers he would enter next
        int totalNumbers = scanner.nextInt();
        //create a new array the size of the total numbers
        double[] unsortedNumbers = new double[totalNumbers];
        //tell the user to enter his unsorted numbers
        System.out.println("Enter your unsorted numbers: ");
        //loop totalNumbers times until the whole unsortedNumbers is full
        for(int index = 0; index < totalNumbers; index++)
        {
            System.out.print("> ");
            unsortedNumbers[index] = scanner.nextDouble();
        }
        //Print the numbers he entered
        System.out.println("You entered: ");
        //Arrays.toString prints the array in format [number, number, ...]
        System.out.println(Arrays.toString(unsortedNumbers));
        //sort the arrays with Arrays.sort which sorts in ascending numerical order
        Arrays.sort(unsortedNumbers);
        //Print the final result - sorted numbers
        System.out.println("Sorted: " + Arrays.toString(unsortedNumbers));
    }