1

I am trying to split a file I have read into an array. The file I am reading from is orderd like this.

  • Lauri Maranto;manager
  • Everett Flanders;manager
  • Rachel Cornforth;designer
  • Rocco Burks;designer
  • Larue Maddox;designer
  • Carmelo Grunden;tester

So here I put every line in an array, now im trying to put them in an arrayList and they have to be splitted.

try {                                                          
   String bestand = "C:\\Users\\admin\\Documents\\java oo labo's\\ProjectBeheer\\data\\personeel.csv";                             
   String[] lijnen = TextFile.readLines(bestand);
} catch (IOException io) {
    System.out.println("error" + io.getMessage());
}

This is what I got but it only shows the latest value and then I get an error.

String[] items = null; 
for (int i = 0; i < lines.length; i++) {
    items = lines[i].split(";");
}
List<String> itemList = Arrays.asList(items);
System.out.println(itemList);

Someane knows what I can do? thanks.

The Problem now is that only the latest data is in itemList. This is the printout.

[Carmelo Grunden, tester]
3
  • Post the error you mention, please. Commented Aug 11, 2018 at 8:32
  • my bad, i added it. Commented Aug 11, 2018 at 8:34
  • Sorry, I fixed the error but my arraylist only saves the last value and not the others. Commented Aug 11, 2018 at 8:53

1 Answer 1

3

In your for loop you create a new String array (items) every iteration. You only create the List after the for loop, however, which means only the last iteration will be taken into account. Try changing your code to something like:

List<String> itemList = new ArrayList<>();
for (int i = 0; i < lines.length; i++) {
    for (String token : lines[i].split(";")) {
        itemList.add(token);
    }
}
System.out.println(itemList);

Extended answer based on a comment.

If your goal was more like how @Maurice mentioned in the comments then you'd change your code to:

List<String[]> itemList = new ArrayList<>();
for (String line : lines) {
    itemList.add(line.split(";"));
}

If you want the name and job in separate lists you could do something like:

List<String> nameList = new ArrayList<>();
List<String> jobList = new ArrayList<>();
for (String line : lines) {
    String[] tokens = line.split(";");
    nameList.add(tokens[0]);
    jobList.add(tokens[1]);
}

Since each name corresponds to one job it might be better to use a Map instead.

Map<String, String> map = new HashMap<>();
for (String line : lines) {
    String[] tokens = line.split(";");
    map.put(tokens[0], tokens[1]);
}

This only connects names to jobs (not the other way around) and it also limits the use to unique names (since a Map cannot have two of the same key).

The best option is probably to create a class to hold this information.

public class Employee {
    private String name;
    private String job;

    public Employee(String name, String job) {
        this.name = name;
        this.job = job;
    }

    // Getters and setters omitted for brevity...
}

And then create the list like:

List<Employee> list = new ArrayList<>();
for (String line : lines) {
    String[] tokens = line.split(";");
    list.add(new Employee(tokens[0], tokens[1]));
}
Sign up to request clarification or add additional context in comments.

7 Comments

this is adding each word in a seperate element of the arraylist. Doesn't he want to store each splitted array in an arraylist?
@Maurice not according to how his or her code is written. The line Arrays.asList(items) indicates the goal is to split each line and add all the tokens to a List as separate elements.
@Slaw this also does work, thanks. But it would maybe be better if I also could split them in an seperate arraylist for name and job.
@floriandewulf added more to my answer
@Slaw awesome, thanks. but how can I take date from the second token? list.add(new Employee(tokens[0], tokens[1])); if I do list.get(1) or further it only gives tokens[0] back.
|

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.