【问题标题】:Method to search a matrix of names, only prints 1st row搜索名称矩阵的方法,仅打印第一行
【发布时间】:2020-05-11 18:17:35
【问题描述】:

我正在尝试创建一个搜索包含人名的二维数组的方法。例如,您输入某个字母,它应该将与其匹配的所有名称传递给一维数组并打印出来。然后用户从匹配列表中选择一个名字,它将打印名字的信息,如年龄、性别、出生日期。起初我创建了一个小矩阵进行测试,它工作正常,但在我添加更多名称和信息后它停止工作。现在它只打印矩阵中名字的信息。

它应该如何运行的示例(当我有一个小的测试矩阵时):

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

现在如何运行:

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

请注意,虽然我还没有输入比赛信息,但 Adam 的信息已被打印出来。另外,尽管有多个以 A 开头的名称,但一维数组找不到任何匹配项。它还应该显示带有文本的图片,忽略该代码。这是我的代码:

 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]);


  }
}

【问题讨论】:

  • 你的第一个版本有同样的问题,打印空元素。如果您只想在retrieveByName 中打印结果,为什么不使用StringBuilder 或添加到String 变量中?

标签: java arrays multidimensional-array methods


【解决方案1】:

只需将String search = input.next(); 替换为String search = input.nextLine();

问题是如果你只使用next(),换行符(/n)仍然被缓冲,之后你调用String name = input.nextLine();并且返回的字符串是一个空字符串。从name = "" 开始,矩阵中的每个名称都包含“”。这就是为什么你会得到亚当,这是搜索找到的第一个。

next() 移动到nextLine() 您的变量将被正确分配。

--

我强烈建议您使用 IDE 并学习调试代码。使用一些断点可以轻松找到这些错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2023-02-24
    • 2015-01-10
    相关资源
    最近更新 更多