# Convert String Array to int / float (loadStrings)

**URL:** <https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695>\
**Category:** Coding Questions\
**Created:** [February 9, 2021, 9:49pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695 "2021-02-09T21:49:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![librakl](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@librakl](https://discourse.processing.org/u/librakl)\
**Post date:** [February 9, 2021, 9:49pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/1 "2021-02-09T21:49:04Z")

</div>

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.

````auto
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! :)
````

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 9, 2021, 9:52pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/2 "2021-02-09T21:52:19Z")

</div>

> [@librakl](#):
>
> which are seperated with “,”

You can use split() to split it up

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 9, 2021, 9:54pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/3 "2021-02-09T21:54:18Z")

</div>

> [@librakl](#):
>
> ` println(n);`

println (zahl); maybe?

Post the first 10 lines of your csv

---

<div class="post-metadata">

**Author:** ![librakl](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@librakl](https://discourse.processing.org/u/librakl)\
**Post date:** [February 9, 2021, 10:08pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/4 "2021-02-09T22:08:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![librakl](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@librakl](https://discourse.processing.org/u/librakl)\
**Post date:** [February 9, 2021, 10:12pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/5 "2021-02-09T22:12:00Z")

</div>

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…)

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 9, 2021, 10:16pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/6 "2021-02-09T22:16:47Z")

</div>

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());

---

<div class="post-metadata">

**Author:** ![librakl](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@librakl](https://discourse.processing.org/u/librakl)\
**Post date:** [February 9, 2021, 10:19pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/7 "2021-02-09T22:19:19Z")

</div>

thanks i will try tomorrow 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 9, 2021, 10:36pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/8 "2021-02-09T22:36:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 10, 2021, 10:18am UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/9 "2021-02-10T10:18:18Z")

</div>

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

```auto
//
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 
//

```

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 19, 2021, 3:12pm UTC](https://discourse.processing.org/t/convert-string-array-to-int-float-loadstrings/27695/11 "2021-02-19T15:12:41Z")

</div>

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