[英]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. 我用学生信息创建了一个数组,然后使用sort对其进行排序,并使用for循环将其输出,一切都很好。 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.
然后,我使用缓冲的阅读器请求用户输入,并进行了切换以缩小输入名称,然后使用if来查看是否在列表中。 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.
如您所见,我使用了布尔变量“ check”来尝试控制程序流,但是它不能正常工作。 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.
我的意思是,现在,在输出排序后的列表之后,该程序将让我键入内容,但是首先没有输出“输入名称到搜索列表”,而且,在我键入一个名称并按Enter键之后,程序将再次执行行来写,但不执行switch选项。
Java counts arrays from 0 to number - 1, so your loop never terminates. Java计算数组的范围是从0到数字-1,因此循环永远不会终止。 You need to change it to be
a < list.length
only. 您需要将其更改为仅
a < list.length
。 You also need to change how you set check
, cause you'll never get there with the fixed loop; 您还需要更改设置
check
,因为使用固定循环永远无法到达那里; it needs to be if (a == list.length - 1)
. 它必须是
if (a == list.length - 1)
。
However, a for loop would be much easier and more readable: 但是,for循环会更容易且更易读:
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. 我不太明白为什么仍然需要一个check变量,因为它将始终设置为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.