jinRisse
2009-03-11 08:52:28 UTC
my program is about Sorting. it asks inputs from the user but it's not supposed to accept characters or Strings of charaters from the user. but when it does, it should display a message that the user has keyed in a wrong input. the program should not be terminated, but rather, it would ask the user to give a new input. what should i do?
here's my code:
for my Main Class:
// start of code
package trylang2;
public class Main {
public static void main(String[] args) {
SortChoice pumili = new SortChoice();
pumili.choice();
}
} // end of code
for my SortChoice Class:
// start of code
package trylang2;
import java.util.*;
public class SortChoice extends Main {
int option; // refers to the choice of sorting to be used
int size; // refers to the size or number of content of the array
long[] array; // refers to the array
long input; // refers to the numbers to be sorted
long min;
long temp;
Scanner scan = new Scanner(System.in);
SelectionDaw select = new SelectionDaw();
InsertionDaw insert = new InsertionDaw();
ExchangeDaw exchange = new ExchangeDaw();
public void choice() { //displays the types of Sorting to choose from
do {
System.out.println("Types of Sorting available: ");
System.out.println("\n1: Selection Sorting");
System.out.println("2: Insetion Sorting");
System.out.println("3: Exchange Sorting");
System.out.println("4: Quick Sorting");
System.out.println("5: Heap Sorting");
System.out.println("Or press 6 to exit.");
//asks the user to choose which type of Sorting to use
System.out.println("\nEnter your choice: ");
option = scan.nextInt();
switch (option) {
case 1:
System.out.println("\nYou chose Selection Sorting");
getArraySize();
getUserInput();
System.out.println("\n");
select.selSort(array, size);
System.out.println("\n");
break;
case 2:
System.out.println("\nYou chose Insertion Sorting");
getArraySize();
getUserInput();
System.out.println("\n");
insert.insertionSort(array, size);
System.out.println("\n");
break;
case 3:
System.out.println("\nYou chose Exchange Sorting");
getArraySize();
getUserInput();
System.out.println("\n");
exchange.exchange_sort(array);
System.out.println("\n");
break;
case 4:
// syntax not yet finished - jinrisse
break;
case 5:
// syntax not yet finished - jinrisse
break;
}
if ((option < 1) || (option > 6)) {
System.out.println("Out of range. Please choose again.");
}
} while (option != 6); //program will be terminated if 6 is pressed.
System.out.println("\nThank you for using this program.");
}
public int getArraySize() {
//asks the user to input the integers
System.out.println("Enter number of inputs: ");
size = scan.nextInt();
array = new long[size];
return size;
}
public long getUserInput() {
for (int i = 0; i < array.length; i++) {
System.out.println("\nEnter the integer: ");
input = scan.nextLong();
array[i] = input;
}
System.out.println("\n\nOriginal Line - up: ");
for (int i = 0; i < array.length; i++) { //for each element
System.out.print(array[i] + " "); //displays them
}
return input;
}
} // end of code
for my SelectionDaw class:
// start of code
package trylang2;
public class SelectionDaw {
public void selSort(long[] array, int size) {
for (int x = 0; x < array.length - 1; x++) {
int index_of_min = x;
for (int y = x; y < array.length; y++) {
if (array[index_of_min] > array[y]) {
index_of_min = y;
}
}
long temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
for(int i =0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("\n");
}