Reputation: 1
I have written a program to sort numbers in both Ascending and Descending order. Here is the output:
"Enter numbers to sort" input numbers {4 3 2 6}
"Type 'A' for Ascending order 'B' for descending order" input A output: 2 3 4 6
This program runs giving the correct output except that it doesn't exit automatically in command prompt. When I press ctrl-c I get a message "exception in thread "main"".
I am trying to find where I went wrong for a while now but with no success.
Code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter numbers to sort");
String str = in.nextLine();
String[] splitted = str.trim().split("\\s+");
int len = splitted.length;
int a[] = new int[len];
for (int i = 0; i < len; i++) {
a[i] = Integer.parseInt(splitted[i]);
}
boolean TryAgain = true;
System.out.println("How to sort: Type 'A' for Ascending and 'B' for Descending order");
while (TryAgain=true) {
char SortType = in.next().charAt(0);
if (SortType == 'A' || SortType == 'a' || SortType == 'B' || SortType == 'b') {// first if
if (SortType == 'A' || SortType == 'a') {// second if loop
// code for sorting in Ascending order
for (int j = 0; j < len - 1; j++) {
for (int k = 0; k < len - j - 1; k++) {
if (a[k] > a[k + 1]) {
int n = a[k];
a[k] = a[k + 1];
a[k + 1] = n;
} // for
} // for
} // if
System.out.println("Numbers sorted in Ascending order:");
for (int m = 0; m < len; m++) {
System.out.print(a[m] + " ");
} // for
TryAgain = false;
} // second if loop
else {// second else
// code for sorting in Dscending order
for (int j = 0; j < len - 1; j++) {
for (int k = 0; k < len - j - 1; k++) {
if (a[k] < a[k + 1]) {
int n = a[k];
a[k] = a[k + 1];
a[k + 1] = n;
} // for
} // for
} // if
System.out.println("Numbers sorted in Dscending order:");
for (int m = 0; m < len; m++) {
System.out.print(a[m] + " ");
} // for
TryAgain = false;
} // second else loop
} // first if loop
else {// first else
TryAgain = true;
System.out.println("Invalid input");
System.out.println("Type 'A' for Ascending and 'B' for Descending order:");
} // first else loop
} // while loop
}// main
}// class
Upvotes: 0
Views: 107
Reputation: 357
When you type
while(tryAgain=true)
It assigns true to tryAgain irrespective of what was assigned previously. This means that the loop will remain true no matter what. In your code we see that you have already initialized the boolean variable tryAgain to true. So, there is no need to do so again. This is the mistake you are making in the while loop thereby keeping I continuously active. Further in your code when you make the value of tryAgain false, it reinitialises itself in the while loop and keeps the loop going on. Instead when you write
while(tryAgain)
It gives you the flexibility to change the value of tryAgain. So when you change its value to false, it remains false. Also if you are if you want, you could change your code to
while(tryAgain==true)
Just to save the trouble of deleting ;). The above snippet will work in the same way. It will compare if the value of tryAgain is true or false and if it is true, it will execute the while loop else come out of it
Upvotes: 1
Reputation: 423
Closing parentheses are missing for most IF statements. Indent your code properly to find missing parentheses.
Upvotes: 0
Reputation: 48258
if you do this.
while(TryAgain=true){
then your while
loop is running for ever...
modify it
Upvotes: 0
Reputation: 75062
Change while(TryAgain=true)
to while(TryAgain)
so that true
won't be assigned to TryAgain
there and have the while
statement see the value of TryAgain
.
Upvotes: 3