Hi this is my method that will be called if I want to get a number from user. but if the user also enter a right number just the "else" part will be run ,why? please help me tahnsk.
public static int chooseTheTypeOfSorting() {
System.out.println("Enter 0 for merge sorting OR enter 1 for bubble sorting");
int numberFromConsole = 0;
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(s);
numberFromConsole = n.intValue();
} catch (ParseException ex) {
Logger.getLogger(DoublyLinkedList.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DoublyLinkedList.class.getName()).log(Level.SEVERE, null, ex);
}
return numberFromConsole;
}
and in my main method:
public static void main(String[] args) {
int i = 0;
i = getRandomNumber(10, 10000);
int p = chooseTheTypeOfSorting();
DoublyLinkedList list = new DoublyLinkedList();
for (int j = 0; j < i; j++) {
list.add(j, getRandomNumber(10, 10000));
if (p == 0) {
//do something....
}
if (p == 1) {
//do something.....
} else {
System.out.println("write the correct number ");
chooseTheTypeOfSorting();
}