getting number from console!
Posted
by Johanna
on Stack Overflow
See other posts from Stack Overflow
or by Johanna
Published on 2010-05-30T15:38:20Z
Indexed on
2010/05/30
15:42 UTC
Read the original article
Hit count: 293
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();
}
© Stack Overflow or respective owner