1

i want parse a String into a Class Instance. My Data come from a .csv.

Code:

@Override
public List<Strasse> getByStadtId(Stadt stadtId){
    return repo.findByStadtId(stadtId);
}

@Override
public void saveStrassenData() {
    StringBuilder builder = new StringBuilder();
    try {
        BufferedReader bufferReader = new BufferedReader(new FileReader("src/main/resources/csv/strassen.csv"));
        while((line = bufferReader.readLine()) != null) {
            String [] data=line.split(",");
            Strasse strasse = new Strasse();
            Stadt stadt = new Stadt();
            strasse.setId(Long.parseLong(data[0]));
            strasse.setName(data[1]);
            strasse.setVerwaltungsKuerzel(data[2]);
            strasse.setStadt(data[3]);
            repo.save(strasse);
        } 
    
    }
    catch (IOException e) {
        //TODO Auto-generated catch block
        e.printStackTrace();
    }
}

In Picture 1 you can see my error.

Picture of the Code

My Class is in the pictures:

enter image description here

The .csv

enter image description here

4
  • create a function that parses the string and convert it to the object that you want. Are you passing json as a string? Commented Jan 4, 2021 at 23:13
  • Have you tried String.valueOf() ? Commented Jan 4, 2021 at 23:19
  • You're calling setStadt() which takes an instance of Stadt but you are passing in data[3] which is a String. Commented Jan 4, 2021 at 23:20
  • Yes. I want to write from my .csv in to a database. Commented Jan 4, 2021 at 23:24

2 Answers 2

2

setStadt doesn't take a String. You need to create an instance of Stadt and then you could set it's id.

Something like that:

Stadt stadt = new Stadt();
stadt.setId(data[3]);
strasse.setStadt(stadt);
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error -> java.lang.NumberFormatException: For input string: "1"
Thank you, i found the mistake :) I had a misspelling in the .csv ^^
0

The handling of Stadt and validation of Strasse is missing.

@Override
public void saveStrassenData() {
    Charset charset = StandardCharsets.ISO_8859_1; // Or UTF_8.
    try (InputStream in = getClass().getResourceAsStream("/csv/strassen.csv");
            BufferedReader bufferReader = new BufferedReader(
                new InputStreamReader(in, charset))) {
        while((line = bufferReader.readLine()) != null) {
            String[] data=line.split(",\\s*");
            Strasse straße = new Strasse();

            long stadtId = Long.parseLong(data[3]);
            Stadt stadt = new Stadt(); // repo.getStadtById(stadtId);
            stadt.setId(stadtId);      //

            straße.setId(Long.parseLong(data[0]));
            straße.setName(data[1]);
            straße.setVerwaltungsKuerzel(data[2]);
            straße.setStadt(stadt;     // <--
            repo.save(straße);
        } 
    
    }
    catch (IOException e) { // Better method with throws IOException.
        e.printStackTrace(); // Log.
    }
}

Also the old utility class FileReader uses the default charset, for a German Windows Cp-1252, a superset of ISO-8859-1. For a server likely UTF-8. Better state the charset explicitly, especially when you provide it yourself as read-only resource.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.