简体   繁体   中英

Read from CSV file with Java

I am trying to learn Java... And I am facing a (simple) problem.

==> I have a csv file:

This road;123;That place
Another road;456;Another place

==> I have some java code:

package tijdelijk;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ReadFromCSV {

    public static void main(String[] args) {
        String CSVadres = "src/tijdelijk/ReadFromCSV.csv";
        try (Scanner adresScanner = new Scanner(new File(CSVadres))) {
            adresScanner.useDelimiter(";");
            while (adresScanner.hasNext()) {
                String adresIter = adresScanner.next();
                int nummerIter = Integer.parseInt(adresScanner.next());
                String plaatsIter = adresScanner.nextLine();
                System.out.println("straat\t\t" + adresIter);
                System.out.println("huisnummer\t" + nummerIter);
                System.out.println("plaats\t\t" + plaatsIter);
                System.out.println("\n");
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ReadFromCSV.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

==> I have output:

run:
straat      This road
huisnummer  123
plaats      ;That place

straat      Another road
huisnummer  456
plaats      ;Another place

BUILD SUCCESSFUL (total time: 0 seconds)

==> I have a problem....

Why do I get the semicolon in teh "plaats" field? How should I write my code to fix this?

Thanks in advance for your help. Kind regards from The Netherlands, Pierre

The Scanner#useDelimiter method takes a regex string as input. So you could use it by defining two delimiters like the following

adresScanner.useDelimiter(";|\n");

The first one will be your inline delimiter and the second one is the newline delimiter.

Then, you don't need to use nextLine() anymore but only next()

Instead of using the scanner, you can read the entire line into a string and then split that string based on the semicolon and put the split string into an array:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) {
        String CSVadres = "C:/LCC Lev 4/test.csv";
        try (Scanner adresScanner = new Scanner(new File(CSVadres))) {
            while (adresScanner.hasNext()) {
                String fullAddress = adresScanner.nextLine();
                String [] addFields = fullAddress.split(";");
                String adresIter = addFields[0];
                String nummerIter = addFields[1];
                String plaatsIter = addFields[2];
                System.out.println("straat\t\t" + adresIter);
                System.out.println("huisnummer\t" + nummerIter);
                System.out.println("plaats\t\t" + plaatsIter);
                System.out.println("\n");
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

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