QUOTE(lemlimlee @ 24 Aug, 2008 - 12:34 AM)

CODE
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
/**
* @param args the command line arguments
*/
public int first, middle, last;
public int[] list;
public Scanner lim = new Scanner(System.in);
public int binarysearch(int[] list, int searchTarget) {
last = list.length - 1;
first = 0;
// while there are still elements to search through
while (first <= last) {
middle = (first + last) / 2;
// if current middle value is the search target
if (list[middle] == searchTarget) {
return middle;
} // if current middle value is less than the search target
else if (list[middle] < searchTarget) {
first = middle + 1;
} // if current middle value is larger than the search target
else {
last = middle - 1;
}
}
// return 0 if search target not found
return 0;
}
public int linearSearch(int[] a, int first, int upto, int key) {
for (int i = first; i < upto; i++) {
if (key == a[i]) {
return i; // Found key, return index.
}
}
return -1; // Failed to find key
}
public void showWhatToDoMenu(int[] arrNum, int y) {
System.out.println("What do you want to perform choose :\n 1. Searching\n 2. Sorting\n 3. Exit\n ");
int result;
switch (lim.nextInt()) {
case 1:
System.out.println("1. Linear Searching\n2. Binary Searching\n");
switch (lim.nextInt()) {
case 1:
result=linearSearch(arrNum, 0, arrNum.length, y);
if (result!=-1) System.out.println("I found number "+y+" on index "+result+"."); else System.out.println("Number not found.");
break;
case 2:
result=binarysearch(arrNum, y);
if (result!=0) System.out.println("I found number "+y+" on index "+result+"."); else System.out.println("Number not found.");
break;
}
break;
case 2:
System.out.println("1. Bubble Sorting \n2. Selection Sorting\n3. Insertion");
switch (lim.nextInt()) {
case 1:
System.out.println("Bubble Sorting");
break;
case 2:
System.out.println("Insertion Sorting");
break;
case 3:
System.out.println("Selection Sorting");
break;
}
break;
case 3:
System.exit((lim.nextInt()));
break;
}
}
public void showMainMenu() {
System.out.println("Please Enter a Search Key: ");
int y = lim.nextInt();
int[] arrNum = new int[20];
Random randomGenerator = new Random();
for (int idx = 1; idx <= 20; ++idx) {
int randomInt = randomGenerator.nextInt(100);
arrNum[idx - 1] = randomInt;
}
System.out.println(Arrays.toString(arrNum));
System.out.println("\n");
showWhatToDoMenu(arrNum, y);
}
public static void main(String[] args) {
new Main().showMainMenu();
}
}
I having a hard time modifying this code im i want this program not to exit after performing the operation unless the user to exit. example if i chose searching and the program execute it will not exit it will go back to the menu over and over again until the user decides to exit the program.
First of all your program isnt workin with binary search and when u are done with sorting u should display a sorted array.Neways as far as your problem is concerned you just got to make a global variable like "wannaquit" and initialise it with false now just enclose the contents of showMainMenu() in a loop
CODE
public void showMainMenu() {
while(!wannaquit){
System.out.println("Please Enter a Search Key: ");
int y = lim.nextInt();
int[] arrNum = new int[20];
Random randomGenerator = new Random();
for (int idx = 1; idx <= 20; ++idx) {
int randomInt = randomGenerator.nextInt(100);
arrNum[idx - 1] = randomInt;
}
System.out.println(Arrays.toString(arrNum));
System.out.println();
showWhatToDoMenu(arrNum, y);
}
}
like this.And in main switch make following changes where case 3 is for Exit:
CODE
case 3:
wannaquit=true;
break;
this would solve your problem.