【问题标题】:Find both integers and strings for each line and put them in an array?查找每一行的整数和字符串并将它们放入数组中?
【发布时间】:2019-06-11 21:13:53
【问题描述】:

所以我有这个 dat 文件(txt 文件),每个国家都有一个州和一个邮政编码。 在这个 dat 文件中,我有一个分号,用于分隔每行的字符串和每行的整数。我也为此使用了一个接口和一个主类,但这是应该完成大部分工作的类。下面是方法。

PS:我试图在这个网站上找到其他已经回答了我的问题的问题,但实际上没有一个问题有帮助!

这是 dat 文件的内容:

75242;乌普萨拉

90325;于默奥

96133;博登

23642;Höllviken

35243;韦克舍

51000;延雪平

72211;韦斯特罗斯

我的问题是我无法找到一种方法来以我希望它们工作的方式保留数组中的整数或字符串。试图只读取整数和字符串,但没有奏效。 另请注意,我已尝试读取 dat 文件中的每一行,然后读取每个字符,以尝试将值放入它们自己的数组中。 还尝试通过使用 if'ments 并说“if(Character.is..)”来转移它们。 在下面的方法中,我只是试图捕获整数。

还认为由于分号,我应该使用“Character.is....”之类的东西来检查它,然后从读取 ch/string 转到 int。但是我想一次迈出一步,否则我什么都做不了!

public void read() {

    try {
        ArrayList<Integer> itemArr = new ArrayList<Integer>();
        ArrayList<String> descArr = new ArrayList<String>();
        FileReader file = new FileReader("C:\\Users\\me\\Desktop\\places.dat");
        BufferedReader r = new BufferedReader(file);
        int r1;

        while ((r.readLine()) != null) {
            while ((r1 = r.read()) != -1) {
                char ch = (char) r1;
                if (Character.isDigit(ch)) {
                    itemArr.add(r.read());
                }
            }
        }

    } catch (IOException e) {

    }
}

这是预期的: 它们也已排序,但只要我能弄清楚如何正确地将它们存储在它们自己的每个数组中,我就可以处理。

23642 霍尔维肯

35243 韦克舍

51000 延雪平

72211 韦斯特罗斯

75242 乌普萨拉

90325 于默奥

96133 博登

感谢所有的cmets,真的有帮助。

【问题讨论】:

  • 不要使用单独的数组或列表,而只使用 one,其中包含具有两个字段的元素:邮政编码和名称。对这些元素进行排序就像正确实现 Comparable 或为 sort 方法提供 Comparator 的实现一样简单。
  • 拆分它,整数是5位所以使用拆分:P
  • 由于dat文件有“;”在它们之间,你为什么不把它们分开并放在一个列表中呢?您能否告诉我们是否需要存储在两个单独的列表中?
  • 我明白了,将尝试使用类似具有两个字段并拆分的数组的东西。谢谢。
  • @AxwellSmith:我在下面添加了 sn-p,它可以为您完成任务。看看吧!

标签: java arrays buffered


【解决方案1】:

您可以将逻辑更改为以下内容:

String currLine;
while ((currLine = r.readLine()) != null) {
    if (currLine.trim().length() > 0) {
        String[] split = currLine.split(";");
        itemArr.add(Integer.parseInt(split[0]));
        descArr.add(split[1]);
    }
}

这里我们根据;分割每一行(currLine)并将其存储到数组split中。现在 0th 索引将包含数字,而 1st 索引将包含字符串。

要将其添加到itemArr,您需要将其解析为int。另请注意,如果该行为空,则会跳过它。现在对其进行排序也很简单。

【讨论】:

    【解决方案2】:

    您可以使用拆分功能并使用“;”将其拆分因为 .dat 文件有;在邮政编码和字符串之间。如果你希望邮政编码是整数,你可以解析parts[0]如下给你一个想法/

     FileReader file = new FileReader("C:\\Users\\me\\Desktop\\places.dat");
     BufferedReader r = new BufferedReader(file);
    
      String st;
      while ((st = br.readLine()) != null) {
       String[] parts = st.split[";"] 
       parts[0] // will have zipcode
       parts[1] // will have other part
      }
    

    【讨论】:

      【解决方案3】:

      我建议你创建一个新的类,例如在哪里存储数字和名称:

      class NumberName {
          Integer number;
          String name;
      
          // constructor, setter, getter
      }
      

      然后在读取文件时,将它们放在一个列表中:

      FileReader file = new FileReader("C:\\Users\\me\\Desktop\\places.dat");
      BufferedReader r = new BufferedReader(file);
      String line;
      
      List<NumberName> list = new ArrayList<>();
      while ((line = r.readLine()) != null) {
          String[] split = line.split(";");
          list.add(new NumberName(Integer.parseInt(split[0]), split[1]));
      }
      

      那么排序就变得简单了:

      list.sort(Comparator.comparingInt(NumberName::getNumber)); // by number
      list.sort(Comparator.comparing(NumberName::getName)); // by name
      list.sort(Comparator.comparing(NumberName::getNumber).thenComparing(NumberName::getName)); // by number then by name
      

      【讨论】:

      • 我明白了,但我仍然有我的两个数组列表,并且我仍然从那里保留存储空间,可能会改变这一点。但是我不明白我怎么能把那些排序线放在那里......以前没有使用过比较器。
      【解决方案4】:

      使用两个列表是多余的。如果您知道每个邮政编码与每个国家/地区相关,您应该创建对象来存储它们,然后将这些对象放入列表中。

      public class MyLocation {
          private int zipcode;
          private String country;
          public MyLocation(int zipcode, String country) {
              this.zipcode = zipcode;
              this.country = country;
          }
          public int getZipcode() {
              return zipcode;
          }
          public String getCountry() {
              return country;
          }
      }
      

      此外,您有一个分隔的数据集,因此您应该利用它!使用String.split() 并在该分号上拆分以获得一个数组,其中两个元素都准备好进行提取。

      ArrayList<MyLocation> locations = new ArrayList<>();
      try {
          FileReader file = new FileReader("C:\\Users\\me\\Desktop\\places.dat");
          Bufferedreader r = new BufferedReader(file);
          try {
              String line;
              String[] lineValues;
      
              while ((line = r.readLine()) != null) {
                  lineValues = line.split(";");
                  MyLocation location = new MyLocation(Integer.valueOf(lineValues[0]), lineValues[1]);
                  locations.add(location);
              } 
          } finally {
              r.close();
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      

      此外,最好在使用完资源后关闭它。我已经编辑了我的代码来演示一种方法来做到这一点。

      正如其他人所说,如果您想要排序,您还需要实现 Comparable,但这非常简单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-29
        • 1970-01-01
        • 2018-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多