查找每行的整数和字符串,然后将它们放入数组中?

时间:2019-01-17 16:05:38

标签: java arrays buffered

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

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

这是dat文件的内容:

75242;乌普萨拉

90325;于默奥

96133;博登

23642;霍尔维肯

35243;Växjö

51000;延雪平

72211;韦斯特罗斯

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

还认为由于分号,我应该使用“ Character.is ....”之类的内容进行检查,然后从读取ch / string转换为int。但是要当时迈出一步,否则我什么都做不了!

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="200dp"
        android:top="200dp"
        android:right="200dp"
        android:bottom="200dp" />
    <corners android:radius="8dp" />
    <size android:width="200dp"
        android:height="5dp"/>
</shape>

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

23642霍尔维肯

35243韦克舍

51000延雪平

72211韦斯特罗斯

75242乌普萨拉

90325于默奥

96133博登

感谢所有评论,确实有帮助。

4 个答案:

答案 0 :(得分: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。另请注意,如果该行为空,则将其跳过。现在对它进行排序也很简单。

答案 1 :(得分: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
  }

答案 2 :(得分: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

答案 3 :(得分:0)

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

SELECT id FROM tablename WHERE
CAST(SUBSTRING(serialnumber, 4, 7) as int) >= 4110380 AND
CAST(SUBSTRING(serialnumber, 4, 7) as int) <= 4111317

此外,您还有定界数据集,因此您应该利用它!使用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(); } ,但这很简单。

相关问题