英文:
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(",");
	ArrayList < String > arrlist1 = new ArrayList < >();
	ArrayList < String > arrlist2 = new ArrayList < >();
	ArrayList < String > arrlist3 = new ArrayList < >();
	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("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));
 }
答案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<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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论