编辑和搜索字符串数组

时间:2012-10-25 21:42:16

标签: java arrays string search edit

对于格式化灾难感到抱歉,我是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循环放入。第二个问题是如何从用户获取新信息并将其替换为名称数组(编辑功能)。

2 个答案:

答案 0 :(得分:0)

如果我很清楚你需要的是:

"string1".equalsIgnoreCase("string2");

这将比较2个字符串,如果它们是大写或更低则忽略。现在你需要做的是循环数组并将每个索引与你想要比较的String进行比较。

答案 1 :(得分: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上一直出现的常见问题,你需要使用nextLine跟随你的nextInt吃掉换行符:

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