关于对CSV文件中的行元素进行排序并将其存储到不同的ArrayList中。

huangapple go评论72阅读模式
英文:

About sorting elements of a line in a csv file and storing it into different Arraylists

问题

所以我有一个类似这样的CSV文件:

LS1312, AWFXQA, -12107909
LS1313, VEDSBJV, -55003726
LS1314, UCRQSE, -1711111

...

因此,我想知道如何将每行的第一个元素(例如LS1312、LS1313)存储到我的第一个ArrayList中,将第二个元素(AWFXQA、VEDSBJV)存储到第二个ArrayList中,将最后一个元素(-12107909、-55003726)存储到第三个ArrayList中?
以下是我目前的代码:我成功读取了文件,但不确定如何将元素存储在各自的ArrayList中。

public static void main(String[] args) throws FileNotFoundException {

    Scanner scanner = new Scanner(new File(args[0]));

    scanner.useDelimiter(",");

    ArrayList<String> arrlist1 = new ArrayList<>();
    ArrayList<String> arrlist2 = new ArrayList<>();
    ArrayList<String> arrlist3 = new ArrayList<>();

    while (scanner.hasNext()) {
        arrlist1.add(scanner.next());
        arrlist2.add(scanner.next());
        arrlist3.add(scanner.next());
    }
    scanner.close();
}
英文:

So I have a CSV file that kinda looks like this :

LS1312, AWFXQA, -12107909  
LS1313, VEDSBJV, -55003726  
LS1314, UCRQSE, -1711111

....

Therefore, I'm wondering how I can store the first element of the line (for example LS1312,
LS1313) into my first ArrayList and the second element(AWFXQA, VEDSBJV) into the second ArrayList and the last element (-12107909, -55003726) into the third ArrayList?
This is what I have right now: I managed to read the file but am not sure how to store elements in its respective Arraylist.

public static void main(String[] args) throws FileNotFoundException {

	Scanner scanner = new Scanner(new File(args[0]));

	scanner.useDelimiter(&quot;,&quot;);

	ArrayList &lt; String &gt; arrlist1 = new ArrayList &lt; &gt;();
	ArrayList &lt; String &gt; arrlist2 = new ArrayList &lt; &gt;();
	ArrayList &lt; String &gt; arrlist3 = new ArrayList &lt; &gt;();

	while (scanner.hasNext()) {
		arrlist1.add(scanner.next());
	}
	scanner.close();
}

答案1

得分: 2

你可以使用一个通用的CSV库:

简单示例:

Reader in = new FileReader("yourFile.csv");
Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
for (CSVRecord record : records) {
    arrlist1.add(record.get(0));
    arrlist2.add(record.get(1));
    arrlist3.add(record.get(2));
}
英文:

You can use a Common CSV Library:

Simple:

 Reader in = new FileReader(&quot;yourFile.csv&quot;);
 Iterable&lt;CSVRecord&gt; records = CSVFormat.RFC4180.parse(in);
 for (CSVRecord record : records) {
      arrlist1.add(record.get(0));
      arrlist2.add(record.get(1));
      arrlist3.add(record.get(2));
 }

答案2

得分: 1

你应该使用split()方法来分割行,并赋值给相应的数组

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(args[0]));
    ArrayList<String> arrlist1 = new ArrayList<>();
    ArrayList<String> arrlist2 = new ArrayList<>();
    ArrayList<String> arrlist3 = new ArrayList<>();

    String[] split;
    while (scanner.hasNextLine())  {
        split= scanner.nextLine().split(",");
        arrlist1.add(split[0]);
        arrlist2.add(split[1]);
        arrlist3.add(split[2]);
    }
    scanner.close();
}
英文:

You should use split() method to split the line and assign to the equivalent array

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(args[0]));
    ArrayList&lt;String&gt; arrlist1 = new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; arrlist2 = new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; arrlist3 = new ArrayList&lt;&gt;();

    String[] split;
    while (scanner.hasNextLine())  {
        split= scanner.nextLine().split(&quot;,&quot;);
        arrlist1.add(split[0]);
        arrlist2.add(split[1]);
        arrlist3.add(split[2]);
    }
    scanner.close();
}

huangapple
  • 本文由 发表于 2020年5月4日 08:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61583278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定