简体   繁体   中英

How to get data from a CSV file and place it in a list

I want to read data from a CSV file in Java and then put this data into a list. The data in the CSV is put into rows which looks like: Data, 32, 4.3 Month, May2, May 5

The code I have currently only prints the [32].

ArrayList<String> myList = new ArrayList<String>();

Scanner scanner = new Scanner(new File("\\C:\\Users\\Book1.csv\\"));
scanner.useDelimiter(",");
while(scanner.hasNext()){
    myList.add(scanner.next());
    for (int i = 0; i <= myList.size(); i++) {
        System.out.println(myList.toString());
    }
    scanner.close();
}

Maybe this code can help you, maybe this code is different from yours, you use arrayList while I use regular array.

Example of the data:

  • Farhan,3.84,4,72
  • Rajab,2.98,4,72
  • Agil,2.72,4,72
  • Alpin,3.11,4,73
  • Mono,3,6,118 K
  • imel,3.97,7,132
  • Rano,2.12,6,110
  • Kukuh,4,1,22

Placing data on each row in a csv file separated by commas into the array of each index

    int tmp = 0;
    String read;
    Mahasiswa[] mhs = new Mahasiswa[100];

    BufferedWriter outs;
    BufferedReader ins;
    BufferedReader br = new BufferedReader(new 
    InputStreamReader(System.in));
    Scanner input = new Scanner(System.in);

    try {
        ins = new BufferedReader(new FileReader("src/file.csv"));
        tmp = 0;
        while ((read = ins.readLine()) != null) {
            String[] siswa = read.split(",");
            mhs[tmp] = new Mahasiswa();
            mhs[tmp].nama = siswa[0];
            mhs[tmp].ipk = Float.parseFloat(siswa[1]);
            mhs[tmp].sem = Integer.parseInt(siswa[2]);
            mhs[tmp].sks = Integer.parseInt(siswa[3]);
            tmp++;
            i++;
        }
        ins.close();
    } catch (IOException e) {
        System.out.println("Terdapat Masalah: " + e);
    }

Print the array data

  tmp = 0; 
  while (tmp < i) {
  System.out.println(mhs[tmp].nama + "\t\t" + 
                      mhs[tmp].ipk + "\t\t" + 
                      mhs[tmp].sem + "\t\t" + 
                      mhs[tmp].sks);
                    tmp++;
                }
ArrayList<String> myList = new ArrayList<String>();
try (Scanner scanner = new Scanner(new File("C:\\Users\\Book1.csv"))) { 
    //here at your code there are backslashes at front and end of the path that was the
    //main reason you are not able to read csv file
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                myList.add(scanner.next());
            }
            for (int i = 0; i < myList.size(); i++) { //remember index is always equal to "length - 1"
                System.out.println(myList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

you also did not handle the FileNotFoundException

Hope this helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM