how to load array of strings with loadString

This file.txt contains:

"filename","5020","bw","10" "filename2","2048","bw","11" "filename3","1024","wb","12"

String address[]=loadStrings("file.txt");
 for(i=0;i<file.length;i++)
  println(address[i]);

prints to console:

"filename","5020","bw","10"
"filename2","2048","bw","11"
"filename3","1024","wb","12"

I want to create a 2D array where

list[0][0]="filename"
list[0][1]="5020"
list[0][2]="bw"
list[0][3]="10"

list[1][0]="filename2"
list[1][1]="2048"
etc.

But can't figure out sytntax to get list[][]=split(address[0-3],',') to put elements in array

String list[]=split(address[0],','); works for one line of address
but can't get the 2D array to read in
Tryed String list[][]=split(address[0-2],',') in for loop but get
can't convert String[] to String[][]

thanks for any help

Answers

  • whats the difference between String [] list and String list[] or String [][] list and String list[][]?

  • ok, got loaded string in to String list[] with split(file[i],',') then in array of [10][4] 10 lists of 4 items per index in array Now wanna convert select strings to int and tryed strthresh=fileitem[i][1].substring(0,2); and get null pointer error!

  • have array of strings list[10][4] list[0][1]="10%" want just the "10" so list[0][1].substring(0,2) should give "10" but get null pointer error!

  • Answer ✓

    Since it seems a ".csv" file, why don't you simply use loadTable() in place of loadStrings()?
    https://Processing.org/reference/loadTable_.html

  • I got it working except for string to int conversion strthresh="10" This print(strthresh) prints "10" But... thresh=int(strthresh) thresh=parseInt(strthresh) thresh=Integer.parseInt(strthresh) All the above give 0 valuse for thresh!

  • I got it. substring was 0,2 needed to be 1,3 never mind

Sign In or Register to comment.