[英]Method to search a matrix of names, only prints 1st row
我正在尝试创建一种方法来搜索包含人名的二维数组。 例如,您输入某个字母,它应该将所有与其匹配的名称传递给一维数组并打印出来。 然后用户从匹配列表中选择一个名字,它会打印名字的信息,如年龄、性别、出生日期。 起初我创建了一个小矩阵来测试,它工作正常,但在我添加更多名称和信息后,它停止工作。 现在它只打印矩阵中名字的信息。
它应该如何运行的示例(当我有一个小的测试矩阵时):
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]);
}
}
只需替换String search = input.next();
with String search = input.nextLine();
问题是如果你只使用next()
换行符 (/n) 仍然被缓冲,之后你调用String name = input.nextLine();
并且返回的字符串是一个空字符串。 由于name = ""
,矩阵中的每个名称都包含 ""。 这就是为什么您要找到 Adam,搜索找到的第一个。
从next()
移动到nextLine()
您的变量将被正确分配。
——
我强烈建议您使用 IDE 并学习调试代码。 使用一些断点,您可以轻松找到这些错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.