如何在将文件读取到哈希映射时修复此逻辑错误



我正在传入

String str = "First Name: Johnn Last Name: GrannySmithnBirthday: January 1 2014n First Name: Georgen Last Name: SmithnBirthday: January 2 2014 ";

进入以下方法

public static void sortMap(String str) {
    String[] parsedInput = str.split("n");
    Set<String> information = new HashSet<String>();
    Map<String, Set<String>> myMap = new HashMap<String, Set<String>>();
    for (int i = 0; i < parsedInput.length; i++) {
        String firstName = "";
        if (parsedInput[i].startsWith("First Name")) {
            firstName = parsedInput[i].split(": ")[1];
        } else {
            while (!(parsedInput[i].startsWith("First Name"))) {
                information.add(parsedInput[i].split(": ")[1]);
            }
        }
        myMap.put(firstName, information);
        System.out.println(myMap.get(firstName));
    }
}

我正在尝试做的是将我的数据存储到一个哈希图中,如果用户输入他们的名字,那么我可以显示他们的整个信息。信息存储在文件中。正在读取文件。我已经将文件拆分到有下一行的地方。如果下一个喜欢碰巧是名字,那么我知道我想要一个新的键和值,这就是我有 if 语句的原因。while 循环是为了确保该人的所有信息都针对该特定密钥存储(这样,如果我添加地址、id 等,它将全部存储在该特定密钥中)。当我运行这个时,我得到"[]"的结果。而不是信息。我不确定我的逻辑在哪里出错?我似乎也陷入了一个无限循环。

你的方法有很多错误,这样的事情可能会有更好的运气

// Removes whitespace (if in not null), otherwise returns "".
private static String safeTrim(String in) {
  if (in != null) {
    return in.trim();
  }
  return "";
}
public static class Info {
  private String firstName;
  private String lastName;
  private String birthDay;
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = safeTrim(firstName);
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = safeTrim(lastName);
  }
  public String getBirthDay() {
    return birthDay;
  }
  public void setBirthDay(String birthDay) {
    this.birthDay = safeTrim(birthDay);
  }
  public Info(String firstName, String lastName,
      String birthDay) {
    setFirstName(firstName);
    setLastName(lastName);
    setBirthDay(birthDay);
  }
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("First Name: ").append(firstName)
        .append('n');
    sb.append("Last Name: ").append(lastName)
        .append('n');
    sb.append("Birthday: ").append(birthDay);
    return sb.toString();
  }
}
// Create and return a Map of the Object type above.
public static Map<String, Info> sortMap(String str) {
  // start parsing at first name.
  String[] firstNames = str.split("First Name");
  Map<String, Info> map = new HashMap<String, Info>();
  for (String nameStr : firstNames) {
    nameStr = safeTrim(nameStr);
    if (nameStr.length() > 0) {
      // add back the key name - 
      nameStr = "First Name" + nameStr;
      String[] fields = nameStr.split("n");
      // The three fields to find...
      String firstName = "";
      String lastName = "";
      String birthDay = "";
      for (String f : fields) {
        // split name-value.
        String[] nv = safeTrim(f).split(":");
        if (nv.length != 2) {
          continue;
        }
        nv[0] = safeTrim(nv[0]);
        if (nv[0].equalsIgnoreCase("First Name")) {
          firstName = safeTrim(nv[1]);
        } else if (nv[0]
            .equalsIgnoreCase("Last Name")) {
          lastName = safeTrim(nv[1]);
        } else {
          birthDay = safeTrim(nv[1]);
        }
      }
      map.put(firstName, new Info(firstName,
          lastName, birthDay));
    }
  }
  return map;
}
public static void main(String[] args) {
  String str = "First Name: Johnn Last Name: GrannySmithn"
      + "Birthday: January 1 2014n First Name: Georgen "
      + "Last Name: SmithnBirthday: January 2 2014 ";
  Map<String, Info> map = sortMap(str);
  System.out.println(map.get("John"));
}

当我运行它时,它会输出

First Name: John
Last Name: GrannySmith
Birthday: January 1 2014

相关内容

  • 没有找到相关文章

最新更新