-1

Looking for a way to store a csv and then take the first row and put the data values in certain spots of my text file and then go to next row. Do not want to use external libraries or JARs.

My CSV looks like: Date, Code, Time, Code2 with the values 5546, 3333, May3, 9999;0003;2234. (yes there is multiple inputs for the last column. It will have multiple rows.

My code for my Csv is:

   Scanner scanner = new Scanner(new File ("C:\\Users\\Documents\\order.csv\\"));
   scanner.useDelimiter("\n");

   ArrayList<String> data = new ArrayList<String>();
   while (scanner.hasNext()){
       scanner.useDelimiter(",");
       data.add(scanner.next());
   }

   scanner.close();

Wasn't sure how to loop through the array with all the elements being from the csv and then to write those elements in a text file.

val
  • 11
  • 4
  • what is the output that you are getting? *Do not want to use external libraries or JARs* - **why?** csv processing can get real complicated – Scary Wombat Apr 17 '18 at 02:05
  • you should be using NextLine instead of next, followed by a split. – Angel Koh Apr 17 '18 at 02:07
  • Iterate over a list (https://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/), and write to a text file (https://stackoverflow.com/questions/22859453/what-is-the-simplest-way-to-write-a-text-file-in-java). – jarmod Apr 17 '18 at 02:08

1 Answers1

1

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);    }
Angel Koh
  • 12,479
  • 7
  • 64
  • 91