130k views
5 votes
Write a java programme that asks the user to put any number of numbers. The programme receives all numbers and prints them out sorted in ascending order.​

1 Answer

1 vote

import java.util.Arrays;

import java.util.Scanner;

public class SortNumbers {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

// Prompt the user to input any number of numbers

System.out.print("Enter any number of numbers, separated by spaces: ");

String numbers = input.nextLine();

// Split the numbers string into an array of strings

String[] numStrings = numbers.split(" ");

// Convert the array of strings to an array of integers

int[] nums = new int[numStrings.length];

for (int i = 0; i < numStrings.length; i++) {

nums[i] = Integer.parseInt(numStrings[i]);

}

// Sort the array of integers in ascending order

Arrays.sort(nums);

// Output the sorted array of integers

System.out.println("The numbers in ascending order are:");

for (int num : nums) {

System.out.print(num + " ");

}

}

}

User Jamagas
by
7.9k points