1

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

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

这是 dat 文件的内容:

75242;乌普萨拉

90325;于默奥

96133;博登

23642;霍尔维肯

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 博登

谢谢大家的意见,真的有帮助。

4

4 回答 4

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中。现在第0个索引将包含数字,第1个索引将包含字符串。

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

于 2019-01-17T16:15:29.713 回答
0

您可以使用拆分功能并使用“;”将其拆分 因为 .dat 文件有;邮政编码和字符串之间。如果您希望邮政编码为整数,您可以解析以下部分 [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
  }
于 2019-01-17T16:16:42.367 回答
0

我建议您创建一个新类,例如存储数字和名称的位置:

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
于 2019-01-17T16:19:31.030 回答
0

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

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,但这很简单。

于 2019-01-17T16:25:38.863 回答