简体   繁体   English

搜索名称矩阵的方法,仅打印第一行

[英]Method to search a matrix of names, only prints 1st row

I'm trying to create a method that searches a 2d array that contains names of people.我正在尝试创建一种方法来搜索包含人名的二维数组。 You type in a certain letter for example and it should pass all the names that match it to a 1d array and print that.例如,您输入某个字母,它应该将所有与其匹配的名称传递给一维数组并打印出来。 Then the user selects a name from the list of matches and it will print the name's information like age, sex, DOB.然后用户从匹配列表中选择一个名字,它会打印名字的信息,如年龄、性别、出生日期。 At first I created a small matrix to test with and it was working fine, but after I added more names and information it stopped working.起初我创建了一个小矩阵来测试,它工作正常,但在我添加更多名称和信息后,它停止工作。 Now it just prints the information for the first name in the matrix.现在它只打印矩阵中名字的信息。

Example of how it should run (when I had the small test matrix):它应该如何运行的示例(当我有一个小的测试矩阵时):

Note: search is case sensitive.
Search: L
[null, Lee, null]
Enter the name of your match: Lee

Name: Lee
Sex: M
DOB: 4/4/1993
Height: 5’5”

Search again?
1 to search, 0 to exit

How it runs now:它现在如何运行:

Note: search is case sensitive.
Search: J
[null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
Enter the name of your match: 
Name: Adam  
Sex: M
DOB: (blank)
Height: 5’5”

Search again?
1 to search, 0 to exit

Note that Adam's information is printed although I hadn't yet entered the match information.请注意,虽然我尚未输入匹配信息,但仍会打印 Adam 的信息。 Plus, the 1D array doesn't find any matches although there are multiple names that begin with A. It's also supposed to show a picture with the text, ignore that code.另外,虽然有多个以 A 开头的名称,但一维数组找不到任何匹配项。它也应该显示带有文本的图片,忽略该代码。 Here is my code:这是我的代码:

 public static void main(String[] args)
  {
    String[][] matrix =

    {
      //has much more data, but it looks like this:
      {"Adam","M", "", "5’5”"}, 
      {"Adonis", "M", "", "5’0”"}, 
      {"Aeruna", "F", "", "5’4”"}, 
      {"Aja", "F", "", "5’2”"}}
        };
    int i = -1;
    while (i != 0) // sentinel value
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Note: search is case sensitive.");
      System.out.print("Search: ");
      String search = input.next();

      retrieveByName(search, matrix);

      System.out.print("Enter the name of your match: ");
      String name = input.nextLine();
      int k = positionFinder(name, matrix);

      printInfo(k, matrix);

      System.out.println("\nSearch again?\n1 to search, 0 to exit");
      i = input.nextInt();
      if (i == 0)
        System.exit(0);

    }

  }

  public static int positionFinder(String name, String[][] matrix)
  {
    int pos = 0;

    for (int idx = 0; idx < matrix.length; idx++)
    {

      if (name.equals(matrix[idx][0]))
        pos = idx;

    }

    return pos;
  }

  public static void retrieveByName(String str, String[][] matrix)
  {

    String[] matches = new String[matrix.length];
    for (int k = 0; k < matrix[0].length - 1; k++)
    {
      if ((matrix[k][0]).contains(str))
      {

        matches[k] = matrix[k][0];
      }
    }

    System.out.println(Arrays.toString(matches));

  }

  public static void printInfo(int k, String matrix[][])
  {
    String[] images = {"Untitled_Artwork 9.png", "Untitled_Artwork 9.png"};
    JFrame frame = new JFrame();
    ImageIcon icon = new ImageIcon((images[k]));

    JLabel label = new JLabel(icon);
    int width = icon.getIconWidth();
    int height = icon.getIconHeight();
    icon =
      new ImageIcon(icon.getImage().getScaledInstance(width / 2, height / 2,
        Image.SCALE_DEFAULT));
    label = new JLabel(icon);
    frame.add(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    System.out.print("\n");
    System.out.println("Name: " + matrix[k][0]);
    System.out.println("Sex: " + matrix[k][1]);
    System.out.println("DOB: " + matrix[k][2]);
    System.out.println("Height: " + matrix[k][3]);


  }
}

Just replace String search = input.next();只需替换String search = input.next(); with String search = input.nextLine(); with String search = input.nextLine();

The problem is that if you use just next() the line-break character (/n) is still buffered, after that you are calling to String name = input.nextLine();问题是如果你只使用next()换行符 (/n) 仍然被缓冲,之后你调用String name = input.nextLine(); and the string returned is an empty string.并且返回的字符串是一个空字符串。 Since name = "" , every name in your matrix contains "".由于name = "" ,矩阵中的每个名称都包含 ""。 That's why you are getting Adam, the first one the search founds.这就是为什么您要找到 Adam,搜索找到的第一个。

Moving from next() to nextLine() your variables will be assigned properly.next()移动到nextLine()您的变量将被正确分配。

-- ——

I highly recommend you to use an IDE and learn to debug code.我强烈建议您使用 IDE 并学习调试代码。 Using some breakpoints you can easily find these bugs.使用一些断点,您可以轻松找到这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM