简体   繁体   中英

How do I skip the first element from a String Array?

My Main.java

import java.io.IOException;
import java.util.List;


/**
 * Ein Programm in Java welches CSV Dateien einlesen kann.
 * Wichtige Daten in einer config.properties Datei liegen.
 * 
 * @author F821621
 * @version 1.0
 *
 */

public class Main {
    
    /**
     * Hauptprogramm
     * 
     * @param args
     * @throws IOException
     */

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

        PropertyManager config = new PropertyManager();
        
        CSV_Reader myReader = new CSV_Reader();
        
        
        //myReader.scannerRead(config.getProperty("path"));
        List<Beruf> berufe = myReader.fileRead(config.getProperty("path"));

        for(Beruf b : berufe) {
            System.out.println(b);
        }
    }

}

my CSV_Reader.java

package de.basler;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author F821621
 *
 */

public class CSV_Reader {

    /**
     * Methode welche eine CSV Datein einliest via BufferedReader
     * 
     * @throws IOException
     */
    
    public List<Beruf> fileRead(String path) throws IOException {
        
        List<Beruf> berufe = new ArrayList<>();
        String line = "";
            
        try {
            //Path in Variable schrieben 

            String filePath = path;
            FileReader fileReader = new FileReader(filePath);
                
            BufferedReader reader = new BufferedReader(fileReader);
            
            while ((line = reader.readLine()) != null) {
                String[] attributes = line.split(";");
                Beruf beruf = createBeruf(attributes);
                berufe.add(beruf);
            }
            reader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return berufe;
    
    }
    
    private static Beruf createBeruf(String[] metadata) {
        int id = Integer.parseInt(metadata[0]);
        System.out.println(id);
        int berufsschlüssel = Integer.parseInt(metadata[1]);
        String bezeichnung = metadata[2];
        
        return new Beruf(id, berufsschlüssel, bezeichnung);
    }

}

My CSV file

id;berufschlüssel;Bezeichnung
1;1104115;General
2;1104118;Hauptmann
3;1104121;Leutnant
4;1104122;Major

and my Berufe.java class

package de.basler;


public class Beruf {
    
    private int id;
    private int berufsschlüssel;
    private String Bezeichnung;
    

    public Beruf(int id, int berufsschlüssel, String Bezeichnung) {
        this.id = id;
        this.berufsschlüssel = berufsschlüssel;
        this.Bezeichnung = Bezeichnung;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBerufsschlüssel() {
        return berufsschlüssel;
    }

    public void setBerufsschlüssel(int berufsschlüssel) {
        this.berufsschlüssel = berufsschlüssel;
    }

    public String getBezeichnung() {
        return Bezeichnung;
    }

    public void setBezeichnung(String bezeichnung) {
        Bezeichnung = bezeichnung;
    }


}

And the problem is that it always takes the first row from my csv file and i got the error

Exception in thread "main" java.lang.NumberFormatException: For input string: "id"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at de.basler.CSV_Reader.createBeruf(CSV_Reader.java:72)
    at de.basler.CSV_Reader.fileRead(CSV_Reader.java:59)
    at de.basler.Main.main(Main.java:33)

If I delete the first row it works like a charm but how can I skip the first row so that I don't need to delte it bc I amnot allowed to do this.

The easiest way to remove the header line would be to just read it before you enter your while loop.

String filePath = path;
FileReader fileReader = new FileReader(filePath);
                
BufferedReader reader = new BufferedReader(fileReader);
String headers = reader.readLine(); //This removes the first line from the BufferedReader
            
while ((line = reader.readLine()) != null) {
      String[] attributes = line.split(";");
      Beruf beruf = createBeruf(attributes);
      berufe.add(beruf);
 }
reader.close();

If you use java 8 or higher and are allowed to use streams you could also use the lines method of the Files class

       Files.lines(Paths.get(filePath))
                .skip(1) // skipping the headers
                .map(line -> line.split(";"))
                .map(attributes -> createBeruf(attributes))
                .forEach(beruf -> berufe.add(beruf));

Another quick approach is to control the line reads through flag like below:

public List<Beruf> fileRead(String filePath) throws IOException {
    List<Beruf> berufe = new ArrayList<Beruf>();
    String line = "";
    try {
        FileReader fileReader = new FileReader(filePath);
        BufferedReader reader = new BufferedReader(fileReader);
        Boolean firstLine = Boolean.TRUE;
        while ((line = reader.readLine()) != null) {
            if(firstLine) {
                firstLine = Boolean.FALSE;
                continue;
            }
            String[] attributes = line.split(";");
            Beruf beruf = createBeruf(attributes);
            berufe.add(beruf);
        }
        reader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return berufe;
}

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