Convert String Array to int / float (loadStrings)

Hello there,

i have an csv file with a lot of numbers inside, which are seperated with “,” (. This file is supposed to make a map out of the numbers.
First problem i got is to convert this into String, because i wanna test if the current number in the file is for example higher than 2. I basically know how to convert a normal String into an int, so i feel stupid atm but i dont know how to do it in this case.

void setup(){
  
  size (800,600);
  
}

void draw(){
 
  
  // when opening window background is black
  background(0);
   
    
  }
    
 
void mousePressed(){
  
  // when mousePressed load file
    String [] lines = loadStrings("numbers.csv");
    
    // iterate over Array 
    for (String n: lines){
      
      // convert to int
      int zahl = int(n);
      println(n);
    
    }```

when i convert it, there is just 0 0 on the console, but i need all the numbers from this textfile in int instead of String

Thanks for any help! :)

You can use split() to split it up

println (zahl); maybe?

Post the first 10 lines of your csv

oh yes sure, i had (zahl) before but than the 0 was on the console… just forgot to change it before i have copied

the first two numbers are supposed to be the size of the window
and all the other numbers should make a map (depending on height of number if there is a mountain or not…)

800, 600
0.65882355, 0.65882355, 0.65882355, 0.65882355, 0.6509804, 0.6509804, 0.64705884, 0.64705884, 0.6392157, 0.6313726, 0.627451, 0.61960787, 0.6117647, 0.60784316, 0.6, 0.5882353, 0.5803922, 0.57254905, 0.5686275, 0.56078434, 0.54901963, 0.5411765, 0.53333336, 0.5294118, 0.52156866, 0.5137255, 0.50980395, 0.5019608, 0.49411765, 0.49019608, 0.48235294, 0.48235294, 0.4745098, 0.47058824, 0.47058824, 0.4627451, 0.4627451, 0.45490196, 0.45490196, 0.4509804, 0.4509804, 0.44313726, 0.44313726, 0.43529412, 0.43137255, 0.43137255, 0.42352942, 0.42352942, 0.41568628, 0.4117647, 0.40392157, 0.40392157, 0.39607844, 0.39215687, 0.38431373, 0.38431373, 0.3764706, 0.3764706, 0.37254903, 0.37254903, 0.3647059, 0.3647059, 0.3647059, 0.35686275, 0.35686275, 0.35686275, 0.35686275, 0.3529412, 0.3529412, 0.3529412, 0.3529412, 0.3529412, 0.34509805, 0.34509805, 0.34509805, 0.34509805, 0.34509805, 0.3372549, 0.3372549, 0.3372549, 0.33333334, 0.33333334, 0.33333334, 0.33333334, 0.3254902, 0.3254902, 0.3254902, 0.3254902, 0.31764707, 0.31764707, 0.31764707, 0.31764707, 0.3137255, 0.3137255, 0.30588236, 0.30588236, 0.30588236, 0.29803923, 0.29803923, 0.29411766, 0.28627452, 0.28627452, 0.28627452, 0.2784314, 0.27058825, 0.27058825, 0.26666668, 0.25882354, 0.25882354, 0.2509804, 0.24705882, 0.24705882, 0.23921569, 0.23137255, 0.22745098, 0.22745098, 0.21960784, 0.21960784, 0.21176471, 0.21176471, 0.20784314, 0.20784314, 0.20784314, 0.20784314, 0.20784314, 0.20784314, 0.20784314, 0.20784314, 0.20784314, 0.20784314, 0.21176471, 0.21176471, 0.21176471, 0.21176471, 0.21176471, 0.21176471, 0.21176471, 0.21960784, 0.21960784, 0.21960784, 0.21960784, 0.21960784, 0.21960784, 0.21960784, 0.21960784, 0.22745098, 0.22745098, 0.22745098, 0.22745098, 0.22745098, 0.23137255, 0.23137255, 0.23137255, 0.23137255, 0.23921569, 0.23921569, 0.23921569, 0.24705882, 0.24705882, 0.24705882, 0.2509804, 0.2509804, 0.25882354, 0.25882354, 0.25882354, 0.26666668, 0.26666668, 0.27058825, 0.27058825, 0.27058825, 0.27058825, 0.2784314, 0.2784314, 0.2784314, 0.2784314, 0.28627452, 0.28627452, 0.28627452, 0.28627452, 0.28627452, 0.28627452, 0.28627452, 0.28627452, 0.2784314, 0.2784314, 0.2784314, 0.2784314, 0.27058825, 0.27058825, 0.27058825, 0.27058825, 0.26666668, 0.26666668, 0.26666668, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.25882354, 0.26666668, 0.26666668, 0.27058825, 0.27058825, 0.2784314, 0.28627452, 0.28627452, 0.29411766, 0.29803923, 0.29803923, 0.30588236, 0.3137255, 0.3137255, 0.31764707, 0.31764707, 0.3254902, 0.3254902, 0.3254902, 0.3254902, 0.3254902, 0.3254902, 0.31764707, 0.31764707, 0.31764707, 0.31764707, 0.31764707, 0.31764707, 0.31764707, 0.3137255, 0.3137255, 0.3137255, 0.3137255, 0.3137255, 0.3137255, 0.3137255, 0.3137255, 0.3137255, 0.31764707, 0.31764707, 0.31764707, 0.3254902, 0.3254902, 0.3254902, 0.33333334, 0.33333334, 0.3372549, 0.3372549, 0.34509805, 0.34509805, 0.34509805, 0.34509805, 0.3529412, 0.3529412, 0.3529412, 0.3529412, 0.3529412, 0.3529412, 0.3529412, 0.3529412, 0.35686275, 0.35686275, 0.3529412, 0.35686275, 0.35686275, 0.35686275, 0.35686275, 0.35686275, 0.3647059, 0.3647059, 0.37254903, 0.37254903, 0.3764706, 0.3764706, 0.38431373, 0.39215687, 0.39215687, 0.39607844, 0.40392157, 0.4117647, 0.41568628, 0.42352942, 0.43137255, 0.43529412, 0.44313726, 0.4509804, 0.45490196, 0.4627451, 0.47058824, 0.4745098, 0.48235294, 0.49019608, 0.49019608, 0.49411765, 0.5019608, 0.50980395, 0.50980395, 0.5137255, 0.5137255, 0.52156866, 0.5294118, 0.5294118, 0.5294118, 0.53333336, 0.53333336, 0.5411765, 0.5411765, 0.54901963, 0.54901963, 0.5529412, 0.5529412, 0.56078434, 0.56078434, 0.56078434, 0.5686275, 0.5686275, 0.57254905, 0.57254905, 0.5803922, 0.5882353, 0.5882353, 0.5921569, 0.5921569, 0.6, 0.60784316, 0.6117647, 0.6117647, 0.61960787, 0.627451, 0.627451, 0.6313726, 0.6392157, 0.64705884, 0.64705884, 0.6509804, 0.65882355, 0.65882355, 0.6666667, 0.67058825, 0.67058825, 0.6784314, 0.6784314, 0.6862745, 0.6862745, 0.6901961, 0.6901961, 0.69803923, 0.69803923, 0.69803923, 0.69803923, 0.7058824, 0.7058824, 0.69803923, 0.69803923, 0.69803923, 0.69803923, 0.69803923, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.6862745, 0.6862745, 0.6784314, 0.6784314, 0.6784314, 0.67058825, 0.67058825, 0.67058825, 0.6666667, 0.6666667, 0.6666667, 0.6666667, 0.6666667, 0.65882355, 0.65882355, 0.65882355, 0.65882355, 0.6509804, 0.6509804, 0.6509804, 0.6509804, 0.64705884, 0.64705884, 0.64705884, 0.64705884, 0.64705884, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6313726, 0.6313726, 0.6313726, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.64705884, 0.64705884, 0.6509804, 0.6509804, 0.65882355, 0.65882355, 0.6666667, 0.6666667, 0.67058825, 0.67058825, 0.6784314, 0.6862745, 0.6862745, 0.6901961, 0.69803923, 0.69803923, 0.7058824, 0.7058824, 0.70980394, 0.7176471, 0.7176471, 0.7254902, 0.7254902, 0.7294118, 0.7294118, 0.7294118, 0.7372549, 0.7372549, 0.7372549, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.74509805, 0.7372549, 0.7372549, 0.7372549, 0.7294118, 0.7294118, 0.7294118, 0.7254902, 0.7254902, 0.7176471, 0.7176471, 0.7176471, 0.70980394, 0.70980394, 0.70980394, 0.7058824, 0.7058824, 0.69803923, 0.69803923, 0.69803923, 0.6901961, 0.6901961, 0.6901961, 0.6862745, 0.6862745, 0.6862745, 0.6784314, 0.6784314, 0.6784314, 0.67058825, 0.67058825, 0.67058825, 0.67058825, 0.6666667, 0.6666667, 0.6666667, 0.65882355, 0.65882355, 0.65882355, 0.6509804, 0.6509804, 0.6509804, 0.64705884, 0.64705884, 0.64705884, 0.64705884, 0.64705884, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.6392157, 0.64705884, 0.64705884, 0.64705884, 0.64705884, 0.64705884, 0.6509804, 0.6509804, 0.6509804, 0.65882355, 0.65882355, 0.6666667, 0.6666667, 0.6666667, 0.67058825, 0.67058825, 0.67058825, 0.67058825, 0.6784314, 0.6784314, 0.6784314, 0.6784314, 0.6784314, 0.67058825, 0.67058825, 0.67058825, 0.67058825, 0.6666667, 0.6666667, 0.65882355, 0.6509804, 0.6509804, 0.64705884, 0.6392157, 0.6392157, 0.6313726, 0.627451, 0.627451, 0.61960787, 0.6117647, 0.6117647, 0.60784316, 0.6, 0.6, 0.5921569, 0.5921569, 0.5921569, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5882353, 0.5921569, 0.5921569, 0.5921569, 0.5921569, 0.6, 0.6, 0.6, 0.6, 0.60784316, 0.60784316, 0.60784316, 0.6117647, 0.6117647, 0.61960787, 0.61960787, 0.627451, 0.627451, 0.6313726, 0.6313726, 0.6392157, 0.64705884, 0.64705884, 0.6509804, 0.65882355, 0.65882355, 0.6666667, 0.6666667, 0.67058825, 0.67058825, 0.6784314, 0.6784314, 0.6862745, 0.6862745, 0.6862745, 0.6862745, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.6901961, 0.69803923, 0.69803923, 0.69803923, 0.69803923, 0.7058824, 0.7058824, 0.7058824, 0.7058824, 0.7058824, 0.70980394, 0.70980394, 0.70980394, 0.70980394, 0.70980394, 0.70980394, 0.70980394, 0.70980394, 0.70980394, 0.70980394, 0.7058824, 0.7058824, 0.7058824, 0.69803923, 0.6901961, 0.6901961, 0.6862745, 0.6784314, 0.67058825, 0.67058825, 0.6666667, 0.65882355, 0.6509804, 0.64705884, 0.64705884, 0.6392157, 0.6313726, 0.6313726, 0.627451, 0.627451, 0.627451, 0.61960787, 0.61960787, 0.61960787, 0.627451, 0.627451, 0.627451, 0.6313726, 0.6313726, 0.6392157, 0.64705884, 0.64705884, 0.6509804, 0.65882355, 0.6666667, 0.67058825, 0.6784314, 0.6862745, 0.6901961, 0.69803923, 0.7058824, 0.70980394, 0.7176471, 0.7176471, 0.7254902, 0.7254902, 0.7294118, 0.7294118, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7372549, 0.7294118, 0.7294118, 0.7294118, 0.7294118, 0.7294118, 0.7254902, 0.7254902, 0.7254902, 0.7254902, 0.7254902, 0.7254902, 0.7254902, 0.7176471, 0.7176471, 0.7176471, 0.7176471, 0.7176471, 0.7176471, 0.7176471, 0.7176471, 0.70980394, 0.70980394, 0.70980394, 0.7058824, 0.7058824, 0.7058824, 0.69803923, 0.69803923, 0.6901961, 0.6901961, 0.6862745, 0.6862745, 0.6784314, 0.6784314, 0.67058825, 0.6666667, 0.6666667, 0.65882355, 0.6509804, 0.6509804, 0.64705884, 0.6392157, 0.6313726, 0.627451, 0.61960787, 0.6117647, 0.60784316, 0.5921569, 0.5882353, 0.5803922, 0.5686275, 0.56078434, 0.5529412, 0.5411765, 0.53333336, 0.5294118, 0.52156866, 0.5137255, 0.50980395, 0.5019608, 0.49411765, 0.49411765, 0.49019608, 0.48235294, 0.48235294, 0.48235294, 0.4745098, 0.4745098, 0.4745098, 0.4745098, 0.4745098, 0.4745098, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.47058824, 0.4627451, 0.4627451, 0.4627451, 0.4627451, 0.45490196, 0.45490196, 0.45490196, 0.4509804, 0.4509804, 0.4509804, 0.44313726, 0.44313726, 0.44313726, 0.43529412, 0.43529412, 0.43529412, 0.43137255, 0.43137255, 0.43137255, 0.43137255, 0.42352942, 0.42352942, 0.42352942, 0.42352942, 0.42352942, 0.42352942, 0.42352942, 0.41568628, 0.65882355, 0.65882355, 0.65882355, 0.65882355, 0.6509804, 0.6509804, 0.64705884, 0.64705884, 0.6392157, 0.6313726, 0.627451, 0.61960787, 0.6117647, 0.60784316, 0.6, 0.5882353, 0.5803922, 0.57254905, 0.5686275, 0.56078434, 0.54901963, 0.5411765, 0.53333336, 0.5294118, 0.52156866, 0.5137255, 0.50980395, 0.5019608, 0.49411765, 0.49019608, 0.48235294, 0.48235294, 0.4745098, 0.47058824, 0.47058824, 0.4627451, 0.4627451, 0.45490196, 0.45490196, 0.4509804, 0.44313726, 0.44313726, 0.44313726, 0.43529412, 0.43137255, 0.43137255, 0.42352942, 0.42352942, 0.41568628, 0.4117647, 0.40392157, 0.40392157, 0.39607844, 0.39215687, 0.38431373, 0.38431373, 0.3764706, 0.3764706, 0.37254903, 0.37254903, 0.3647059, 0.3647059, 0.3647059, 0.35686275, 0.35686275, 0.35686275, 0.35686275, 0.3529412, 0.3529412, 0.3529412, 0.34509805, 0.3529412, 0.34509805, 0.34509805, 0.34509805, 0.34509805, 0.34509805, 0.3372549, 0.3372549, 0.3372549, 0.33333334, 0.33333334, 0.33333334, 0.33333334, 0.3254902, 0.3254902, 0.3254902, 0.3254902, 0.31764707, 0.31764707, 0.31764707, 0.3137255, 0.3137255, 0.3137255, 0.30588236, 0.305882

You need to use split within the for loop

Use a 2nd for loop to loop over the result with n2, see Reference

Then use …int(n2.trim());

1 Like

thanks i will try tomorrow :slight_smile:

2 Likes

It’s also not int but float because of the dot

2 Likes

technically, you could make a 2D array (grid, see tutorials) of float, because you have

  • a lot of lines and
  • each of them contains several (different numbers of!) floats.

Remark

I looked at your csv and the 2nd line is VERY long.
You posted only 2 lines.

Here is my Sketch

Here is my Sketch. It’s like in the description above.
It splits each line (all lines) and shows each float “zahl” within the line.

Chrisir

//
String [] lines; 

void setup() {
  size (800, 600);

  lines = loadStrings("numbers.csv");
  println("length is "
    +lines.length);
}

void draw() {
  // when opening window background is black
  background(0);
}

void mousePressed() {

  // when mousePressed 

  // iterate over Array 
  for (String n : lines) {

    // split line
    String[] thisLine = n.split (","); 

    // loop over line
    for (String n2 : thisLine) {
      // convert to float
      float zahl = float(n2);
      print(zahl);
      print(" ");
    }//for 
    println("\n\n");
  } //for
  //
} //func 
//

You can also use Float.parseFloat() if you want to convert the strings into floats, then add them to a new array of floats.