Hello I am currently creating a program where it will ask the user 10 integers, and then will ask what sorting method will they use and will it be in ascending or descending order. This is the current progress I've made so far:
import java.util.*;
import BubbleSort;
import SelectionSort;
public class SortingAlgorithm{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
try{
String reply ="";
String choice = "";
System.out.println("Please Enter 10 numbers: ");
int[] array = new int[10];
//reading array elements from the user
for(int i=0; i<10; i++) {
array[i]=sc.nextInt();
}
System.out.println("What sorting method you need? U - Bubble Sort, S - Selection Sort");
reply = sc.nextLine();
System.out.println("Would you like it to be in A - Ascending or D - Descending order?");
choice = sc.nextLine();
if(reply.equals("U") || choice.equals("u")){
System.out.println("Your number will be sorted using Bubble Sort");
if(choice.equals("A") || choice.equals("a")){
BubbleSort.bubbleSortAscending(array);
}
if(choice.equals("D") || choice.equals("d")){
BubbleSort.bubbleSortDescending(array);
}
System.out.println(Arrays.toString(array));
}
if(reply.equals("S") || choice.equals("s")){
System.out.println("Your number will be sorted using Selection Sort");
if(choice.equals("A") || choice.equals("a")){
SelectionSort.selectionSortAscending(array);
}
if(choice.equals("D") || choice.equals("d")){
SelectionSort.selectionSortDescending(array);
}
System.out.println(Arrays.toString(array));
}
}
catch(InputMismatchException ime){
System.out.println("Can only accept integers.");
}
catch(ArrayIndexOutOfBoundsException ime){
System.out.println("Array is out of Bounds.");
}
}
}
The problem is that every time I run the program this is the result
Please Enter 10 numbers:
1
5
2
8765
1
698541
2
8
5
2
What sorting method you need? U - Bubble Sort, S - Selection Sort
Would you like it to be in A - Ascending or D - Descending order?
A
After asking for 10 integers it skips over asking what sorting method will be used and goes straight to asking if they want it in ascending or descending order, after inputting a value the program ends without showing the expected result, which is the arranged array. Is there anyway to fix or improve this?