-1

对格式化灾难感到抱歉,我是 stackoverflow 和 Java 的新手。我在应该包含两个名称列表和相应目录#s 的代码中有两个问题。在代码中,我应该能够搜索、显示、编辑和退出。显示和退出已经工作,但我在搜索和编辑时遇到问题。我已经强调了问题所在的信息。我将不胜感激任何帮助。

public static void Search(String[] arr, String find) {

    for (int i = 0; i < 10; i++) {

//首要问题!!这是我稍后调用的在字符串数组中搜索代码位的方法。我需要能够在数组中搜索某些字符(数组是名称,所以“mith”会调出“smith”。稍后在代码中我在开关内部调用它。我想我需要类似“ arr[i].equals(find)" 但我不知道语法

   }
}

public static void main(String[] args) {

    Scanner inp = new Scanner(System.in);
    String userInput1 = "";
    String userInput2 = "";
    String userInput3 = "";

    // Ask User to chose between faculty and students
    System.out.println("Are you searching for Students or Faculty? ");
    userInput1 = inp.nextLine().toLowerCase();


    if (userInput1.startsWith("f")) {

        //Block 1 for faculty
        do {

            String[] Faculty = {"0. Jack Kerouac", "1. Les Claypool", "2. Tom Waits", "3. Kurt Vonnegut",
                "4. Hunter Thompson", "5. Princess Bubblegum", "6. John Smith", "7. Jack Thompson", "8. Jeff Dahmer", "9. Martin King"};


            System.out.println("What would you like to do?");
            System.out.println("S: Search");
            System.out.println("E: Edit");
            System.out.println("D: Display");
            System.out.println("Q: Quit");

            // user input
            userInput2 = inp.nextLine().toLowerCase();
            System.out.println("");

            // Respond based on user input
            switch (userInput2.charAt(0)) {
                case 's':
                    System.out.println("Type part of the persons name: ");
                    String x = inp.nextLine().toLowerCase();
                    String[] y = Faculty;
                    Search(y, x);
                break;
                case 'e':

//这是我的编辑数组问题。我尝试了几种不同的方法来使其工作,但是在重新分配 Faculty[q] 时,每一种方法都遇到了问题。当您运行程序时,它会从“输入新目录信息”跳回主循环。如何更改数组中的字符串?

                    System.out.println("Enter the catalog number of the entry trying to edit: ");
                    int q = inp.nextInt();
                    System.out.println(Faculty[q]);
                    System.out.println("Enter new catalog information: ");                       
                    String NewFac = inp.nextLine();
                    Faculty[q] = String NewFac;
                    System.out.println(Faculty[q]);

                    break;
                case 'd':
                    System.out.println("Enter a catalog number between 0-9: ");
                    int o = inp.nextInt();
                    System.out.println(Faculty[o]);
                    break;

            }

            // Blank line, for formatting
            System.out.println("");

        } while (!userInput2.equals("q"));
        System.out.println("Done.");


    } else {
        //block 2 for students
        do {

            String[] Student = {"0. Alice Johnson", "1. Johnny Marr", "2. Johnny Johnson", "3. Robert Smith",
                "4. Ian Curtis", "5. Luke Shapiro", "6. David Newman", "7. Ren Newman", "8. Camille Kaslan", "9. Alexander Shulgin"};

            System.out.println("What would you like to do?");
            System.out.println("S: Search");
            System.out.println("E: Edit");
            System.out.println("D: Display");
            System.out.println("Q: Quit");

            // user input
            userInput3 = inp.nextLine().toLowerCase();
            System.out.println("");

            // Respond based on user input
            switch (userInput3.charAt(0)) {
                case 's':
                    System.out.println("Type part of the persons name: ");
                    String x = inp.nextLine().toLowerCase();
                    String[] y = Student;
                    Search(y, x);
                break;
                case 'e':
                    System.out.println("Enter the catalog number of the entry trying to edit: ");
                    int r = inp.nextInt();
                    System.out.println(Student[r]);
                    System.out.println("Enter new catalog information: ");
                    Student[r] = inp.nextLine();
                    System.out.println(Student[r]);
                    break;
                case 'd':
                    System.out.println("Enter a catalog number between 0-9: ");
                    int n = inp.nextInt();
                    System.out.println(Student[n]);
                    break;

            }

            // Blank line, for formatting
            System.out.println("");

        } while (!userInput3.equals("q"));
        System.out.println("Done.");



    }

如果不清楚,第一个问题是在我的搜索方法中的 for 循环中放入什么,称为“搜索”。第二个问题是如何从用户那里获取新信息并将其替换为名称数组(编辑功能)。

4

2 回答 2

0

如果我很好理解你需要的是:

"string1".equalsIgnoreCase("string2");

这将比较 2 个字符串,忽略它们是大写还是小写。现在您要做的是循环数组并将每个索引与您要比较的字符串进行比较。

于 2012-10-25T21:47:03.490 回答
0

第一个问题,也许是这样的?

public static void Search(String[] arr, String find) {
    for (int i = 0; i < 10; i++) {
        if (arr[i].toLowerCase().contains(find.toLowerCase())) {
             System.out.println(String.format("found name: %s", arr[i]));
             return;
        }
    }
    System.out.println("Name not found!");
}

第二个问题是 SO 上一直出现的一个常见问题,您需要在 nextInt 之后使用 nextLine 来吃换行符:

System.out.println("Enter the catalog number of the entry trying to edit: ");
int q = inp.nextInt();
inp.nextLine();
System.out.println(Faculty[q]);
于 2012-10-25T21:54:59.000 回答