javajlistobject-to-string

How do I cut my sentence out to 4 sentences?


How do i cut this out to 4 sentences instead of 1. i need to cut it out to 4 sentence and only 4 because my jList only accept 4 "persons"

in the JList it stands like this

name,2,2,2,2
Lars,1,4,2,5
Peter,5,3,2,1

Code:

List listOfPersonNames = jListpersons.getSelectedValuesList();
    // your list of strings
    List<String> listOfNameStrings = new ArrayList<String>(listOfPersonNames.size());

    for(Object personName: listOfPersonNames) {
        listOfNameStrings.add(personName.toString());
    }

    // your string from the resulting list
    String listString = listOfNameStrings.toString();
    System.out.println(listString);
    //con.saveTeamListFile();

}

Solution

  • I am not entirely sure on the question so I am going to assume that you need to split that string into elements such as Lars,1,4,2,5. In which case you can split at the space

    String[] names = nameString.split(" ");
    

    You may though be better off separating the elements with another character in case in you want to use strings with spaces in future