what you can do is read one line at a time (using NextLine), instead of one word at a time (Next)
while (scanner.hasNextLine()){
String line = scanner.nextLine();
//you then chop up your text using the split
String[] choppedUpText = line.split(",");
// you can then process according to your needs here
//e.g. convert it to a class object or add to an array.
myList.add ( new myCustomClass( choppedUpText));
}
edit: i see your object can stretch to multiple rows.
Assuming you have a csv like the following
a, b, c, ddd
ddd_continued
a, b, c, ddd
a, b, c, ddd
create a class called my object
class MyObjectEntity{
String a, b, c;
String d="";
public MyObjectEntity(String[] tokens){
this.a = tokens[0]; ... the rest of your constructor
}
public void appendD(String s) { d+=s; } //used for appending the next line.
}
then in your code.
MyObjectEntity object = null;
List<MyObjectEntity> list = new ArrayList<>();
while (scanner.hasNextLine()){
String line = scanner.nextLine();
if(object ==null) { //check if the object already exists?
//no previous object.
//you then chop up your text using the split
String[] choppedUpText = line.split(",");
if(choppedUpText.length ==4){ //has to have 4 parts
//we create the object first.
//then continue iterating to see if there are more parts
object = new MyObjectEntity( choppedUpText);
}else {
println("Error in line: "+ line);
}
}else {
//previous object exists
if(line.contains(","){ // check if it is a new object, or a continuation of d
//new object (save old copy to the list)
list.add(object)
//create a new object
String[] choppedUpText = line.split(",");
if(choppedUpText.length ==4){
object = new MyObjectEntity( choppedUpText);
}else {
println("Error in line: "+ line);
object = null; //reset it back to null
}
}else{
//continuation of d
object.append(line);
}
}
}
//end of loop (if object is not null, then add the last entry in)
if(object !=null) { list.add(object); }