簡體   English   中英

搜索名稱矩陣的方法,僅打印第一行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM