0

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.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • Instead of using `check` and `a` and a while loop, why don't you just use a for loop? `for (int i = 0; i < list.length; i++) { System.out.println(list[i]); }` – takendarkk Nov 24 '14 at 02:27

1 Answers1

2

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.

Robert
  • 7,394
  • 40
  • 45
  • 64
  • did what you said, it worked, after some more time working the program is now completely functional, still it bothers me how long the program is (202 lines), this is because of all the cases and ifs, i realized doing both a case and an if is repetitive, but the other option is doing 20 ifs (one for every name on the list), still this seems like too much, my question, do you know of another way to make the program print a students data from an input name?? – rodrigo villarreal Nov 24 '14 at 04:04
  • Yeah, I didn't understand your `case` and `if` construct with the hard-coded name. If your list always has a space after the name, you could just check for the input like this: for (String s : list) { if (s.startsWith(input + " ")) { /* found... */ } } – Robert Nov 24 '14 at 05:26