Welcome to Dream.In.Code
Become a Java Expert!

Join 150,170 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,298 people online right now. Registration is fast and FREE... Join Now!




Searching, sorting , Exit

 
Reply to this topicStart new topic

Searching, sorting , Exit

lemlimlee
23 Aug, 2008 - 11:34 PM
Post #1

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 2


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.
User is offlineProfile CardPM
+Quote Post

saumya9888
RE: Searching, Sorting , Exit
24 Aug, 2008 - 02:35 AM
Post #2

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

lemlimlee
RE: Searching, Sorting , Exit
24 Aug, 2008 - 03:00 AM
Post #3

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 2

thanks yes, im tooking a hard time making this code. and i want the option 1. Searching 2.Sorting 3. Exit to came out first before the "please enter search key" but i cant do it... may you please help me..
User is offlineProfile CardPM
+Quote Post

thenovices
RE: Searching, Sorting , Exit
24 Aug, 2008 - 08:37 AM
Post #4

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
i didn't really look through your code, but i noticed in your binary search you return '0' if its not found. Remember, its possible that something is at index 0, so usually people would return -1 to indicate that the element is not found.

This post has been edited by thenovices: 24 Aug, 2008 - 08:38 AM
User is offlineProfile CardPM
+Quote Post

saumya9888
RE: Searching, Sorting , Exit
24 Aug, 2008 - 10:21 AM
Post #5

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 22


My Contributions
QUOTE(lemlimlee @ 24 Aug, 2008 - 04:00 AM) *

thanks yes, im tooking a hard time making this code. and i want the option 1. Searching 2.Sorting 3. Exit to came out first before the "please enter search key" but i cant do it... may you please help me..


First of all you got to make the program logic clear.If you want display options first put the enter search key in relevant switch case.I mean u would not need this key if u are just sorting this array.put the key neter dialog in case 1 where u actually want to search.OK if u are a noob and finding lot of difficulty in making this code which is probably your homework tell me i'll complete it for you.But give it your best effort,its a simple program and you will learn only when u make mistakes and correct them yourself...
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:09AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month