This Java program sorts and displays numbers entered by the user. It repeatedly calls a method to read 10 numbers from the user, returning 1 for success or 0 for failure. If reading fails, it tries again. Once numbers are read successfully, it sorts them in ascending order using a bubble sort algorithm. Finally, it displays the sorted numbers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
709 views
Ascending Java Code
This Java program sorts and displays numbers entered by the user. It repeatedly calls a method to read 10 numbers from the user, returning 1 for success or 0 for failure. If reading fails, it tries again. Once numbers are read successfully, it sorts them in ascending order using a bubble sort algorithm. Finally, it displays the sorted numbers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 1
import java.io.
*;
public class Sorting {
static int status=0; public static void main(String[] args) { int[] nums=new int[10]; //Call function readNumbers //Get return value: if 1 then success, if 0 then failure //Check if status=1(success), then proceeds to Sorting //If status=0(failure while reading), then again read the numbers while (status==0) { status=readNumbers(nums); } sort(nums); displayNumbers(nums); } public static int readNumbers(int[] nums) { //Reading ten numbers from the user System.out.print("Enter ten numbers. Press Enter after each number: "); for (int i=0; i<nums.length; i++) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { nums[i]=Integer.parseInt(br.readLine()); } catch (Exception e) { System.out.println("Error! Mistake during Entry of numbers. Try again"); //Return 0 to indicate failure return 0; } } //Return 1 to indicate success return 1; } public static void sort(int[] nums) { //Sorting the numbers in Ascending Order int lower, higher; for (int index1 = 0 ; index1 < nums.length ; index1++) for (lower = index1+1 ; lower < nums.length ; lower++) if (nums[index1] > nums[lower]) { higher = nums[index1]; nums[index1] = nums[lower]; nums[lower] = higher; } } public static void displayNumbers(int[] nums) { //Displaying the numbers System.out.println("Numbers in ascending order are: "); for (int index2 = 0 ; index2 < nums.length ; index2++) System.out.println(nums[index2]); } }