rodvn
rodvn

Reputation: 49

Array Sorting and Searching Java

I'm trying to do a program from a student info sheet. The program is supposed to order the info by student name alphabetically and then give the option for the user to search a student's individual info by name.

I created an array with the student info, then used sort to order it and a for loop to output it, all was good. Then i used the buffered reader to ask for user input, and a switch in order to narrow the input name, then an if to see if/else the searched name is in the list or not. As usual, here is part of my code:

        System.out.println("Welcome to the student list program");
        System.out.println("This is the list in alphabetical order:");
        System.out.println("");

        String[] list = new String[20];
        list[0] = "Andrew    ID: 8374  Age: 19  Grade: 88";
        list[1] = "Ronald    ID: 8953  Age: 17  Grade: 72";
        list[2] = "Kate      ID: 0071  Age: 15  Grade: 97";
        list[3] = "Eve       ID: 7348  Age: 17  Grade: 97";
        list[4] = "Barney    ID: 4704  Age: 15  Grade: 70";
        list[5] = "James     ID: 6259  Age: 20  Grade: 51";
        list[6] = "Tiberius  ID: 8090  Age: 18  Grade: 94";
        list[7] = "George    ID: 5059  Age: 18  Grade: 96";
        list[8] = "William   ID: 2057  Age: 20  Grade: 72";
        list[9] = "Rose      ID: 4977  Age: 17  Grade: 75";
        list[10] = "Kylie     ID: 4407  Age: 17  Grade: 85";
        list[11] = "Mary      ID: 9642  Age: 19  Grade: 62";
        list[12] = "Joey      ID: 3437  Age: 20  Grade: 54";
        list[13] = "Ross      ID: 5040  Age: 20  Grade: 64";
        list[14] = "Chandler  ID: 7931  Age: 18  Grade: 78";
        list[15] = "Monica    ID: 9238  Age: 19  Grade: 92";
        list[16] = "Rachel    ID: 0682  Age: 20  Grade: 99";
        list[17] = "Phoebe    ID: 3456  Age: 18  Grade: 64";
        list[18] = "Bart      ID: 0638  Age: 17  Grade: 73";
        list[19] = "Lisa      ID: 5233  Age: 16  Grade: 52";

        boolean check = false;

        Arrays.sort(list);
        int a = 0;
        while (a <= list.length) {
            if (a == list.length){
                check = true;
            }
            else {
                System.out.println(list[a]);
                a = a+1;
            }
        }

        if (check) {

            System.out.println("input name to search list");
            System.out.println("");

            InputStreamReader inStream = new InputStreamReader(System.in);
            BufferedReader stdIn = new BufferedReader(inStream);

            String input =stdIn.readLine();             
            input = input.toLowerCase();
            char n = input.charAt(0);

            switch (n){
            case 'a':
                if (input.equals("andrew")) {
                    System.out.println(list[0]);
                }
                else{
                    System.out.println("Input name is not in the list");
                }
                break;

The problem is that the program will output the sorted list but it will not give the option to input a name. As you can see, I used the boolean variable "check" to try and control the program flow but it did not work correctly. By this I mean that now, after outputting the sorted list, the program will let me type, but without the output "input name to search list" coming first, also, after I type a name and hit enter the program will just do another line for me to write, but not execute the switch option.

Upvotes: 0

Views: 542

Answers (1)

Robert
Robert

Reputation: 8618

Java counts arrays from 0 to number - 1, so your loop never terminates. You need to change it to be a < list.length only. You also need to change how you set check, cause you'll never get there with the fixed loop; it needs to be if (a == list.length - 1).

However, a for loop would be much easier and more readable:

for (String s : list) {
    System.err.println(s);
}

I don't quite understand why you need a check variable anyway, cause it will always be set to true.

Upvotes: 2

Related Questions