# Convert String to Integer

**URL:** https://discourse.processing.org/t/convert-string-to-integer/2430
**Category:** Coding Questions
**Created:** [August 7, 2018, 1:24am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430 "2018-08-07T01:24:27Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![jarlook](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jarlook](https://discourse.processing.org/u/jarlook)
#### Post date: [August 7, 2018, 1:24am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/1 "2018-08-07T01:24:27Z")

</div>

How can I convert a string to integer?

I have a string that contains a set of values, some are text, but some are actually numeric values, all separated by commas. I can use split(string,",") to get any particular value, but can’t seem to find the way to convert that value if it’s numeric.

New to Processing, so perhaps I just didn’t find it in the Reference section.

Thanks for any help.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [August 7, 2018, 1:34am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/2 "2018-08-07T01:34:52Z")

</div>

[Processing.org/reference/intconvert\_.html](http://Processing.org/reference/intconvert_.html)

---

<div class="post-metadata">

### Author: ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)
#### Post date: [August 7, 2018, 1:49am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/3 "2018-08-07T01:49:41Z")

</div>

So I made a little sketch to show you what I think it is you are asking about:

```auto
String word, number; 

void setup()
 {
   size(100,100);   
 }
 
 void draw()
 { 
   word = "hello";
   number = "123";
   mixed = "hello123";
  
  //next two lines will not work, you will get a NumberFormat Exception
  System.out.println("Let's try to convert this string that is only words to an int: "+Integer.valueOf(word));
  System.out.println("Let's try to convert this string that is both to an int: "+Integer.valueOf(mixed));
 //This will work
  System.out.println("Let's try to convert this string that is only numbers to an int: "+Integer.valueOf(number));

  }

```

These lines of code will **not** work:

`System.out.println("Let's try to convert this string that is only words to an int: "+Integer.valueOf(word));`

`System.out.println("Let's try to convert this string that is only words to an int: "+Integer.valueOf(mixed));`

Because the variables word and mixed contain letters, they will not be converted to ints. But because number is a pure number of 123, it will be converted to an int. Furthermore, you can make a new int, and set it like this:

`int newNumber = Integer.valueOf(number);`

Hopefully this helps,

EnhancedLoop7

---

<div class="post-metadata">

### Author: ![jarlook](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jarlook](https://discourse.processing.org/u/jarlook)
#### Post date: [August 7, 2018, 2:42am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/4 "2018-08-07T02:42:31Z")

</div>

Integer.valueOf(some-string) works perfectly and sublimely easy.

Fabulous, thanks.

But one additional question: where is this in the [Processing.org](http://Processing.org) Reference page(s)? I feel like I’m missing a lot of Processing features/functions/methods.

---

<div class="post-metadata">

### Author: ![jarlook](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jarlook](https://discourse.processing.org/u/jarlook)
#### Post date: [August 7, 2018, 3:07am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/5 "2018-08-07T03:07:16Z")

</div>

Sorry, but I don’t think that reference is the answer.  
See EnhancedLoop7’s response to this question, which does solve it.

Thanks, though, for responding.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 7, 2018, 9:14am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/6 "2018-08-07T09:14:43Z")

</div>

> [@jarlook](#):
>
> Sorry, but I don’t think that reference is the answer

Actually, @GoToLoop answer is both correct and better. `int(someString)` gets turned into `parseInt(someString)`. I would use the latter personally, as you’ll get less confused by normal Java code later. `parseInt` calls `Integer.parseInt` which is what you want here. `Integer.valueOf` returns an Integer not an int.

---

<div class="post-metadata">

### Author: ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)
#### Post date: [August 7, 2018, 2:56pm UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/7 "2018-08-07T14:56:37Z")

</div>

@jarlook I personally don’t know too much about int(someString) but you learn something new everyday I guess! After looking at the reference in Processing I do see that int(someString) in fact is code native to the Processing language that works as well! From what I believe though, all three:

```auto
int(someString); //this is the best for Processing 
Integer.valueOf(someString); //this is more Java related, and as @neilcsmith said, will return an Integer object 
Integer.parseInt(someString); //also Java related, but returns an actual primitive int object

```

So really in the end of the day, it just depends on what it is you specifically need. All three should somehow get you in the ballpark of your expected result. Hope that helps,

EnhancedLoop7

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 7, 2018, 7:22pm UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/8 "2018-08-07T19:22:33Z")

</div>

parseInt is also part of the Processing API.

---

<div class="post-metadata">

### Author: ![terryz](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@terryz](https://discourse.processing.org/u/terryz)
#### Post date: [December 1, 2020, 5:20pm UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/9 "2020-12-01T17:20:02Z")

</div>

I had same issue as original poster. I am sending to Processing both a variety of Sensor values to be used by Objects like meter, or Text to display on the canvas. I am also ending simple Arduino debug messages that can be displayed in the console that are not used by any Processing object. So that my Processing code can distinguish these apart and filter the data to be sent to the various canvas objects I will add identifying characters in front of the sensor data. For example a sensor value meant for meter object m1 may have appended in front of the value “v1m\*”, so the arduino would send via Serial.println(“v1m123”); if the value where say 123. Then using the various string functions parse out the integer data and assign the int() value of that to the meter instance m1.  
I am sure there is a more elegant way to do this but this is what I have come up with.

In order to get the int() function to work I had to remove 2 characters from the received data and I do not understand why when I expected it to be only one.  
Please help me to understand why?

import processing.serial._;  
import meter._;

Meter m;  
int sensorValue;

Serial myPort; // The serial port  
int whichKey = -1; // Variable to hold keystoke values  
int inByte = -1; // Incoming serial data  
String Adata = “0”;  
String debugMsg;  
String deviceData;  
String astr = “\*”;

void setup() {  
size(800, 600);  
m = new Meter(this, 50, 175, false);  
m.setUp(0, 212, 0.0, 212.0, 180.0, 360.0);  
String[] scaleLabels = {“0”, “20”, “40”, “60”, “80”, “100”, “120”, “140”, “160”, “180”, “200”, “220”};  
m.setScaleLabels(scaleLabels);  
m.setDisplayDigitalMeterValue(true);  
m.setTitleFontColor(color(0, 0, 255));  
m.setTitle(“Temperature”);

// create a font with the third font available to the system:  
PFont myFont = createFont(PFont.list()[2], 14);  
textFont(myFont);

// List all the available serial ports:  
printArray(Serial.list());

String portName = Serial.list()[Serial.list().length - 1];  
myPort = new Serial(this, portName, 57600);  
myPort.clear();  
myPort.bufferUntil(’\n’); //Sets # bytes to buffer before calling serialEvent()  
}

void draw() {

background(55);  
//text("Debug: " + debugMsg, 10, 160);  
text("Adata Received: " + Adata, 10, 130);  
text("Last Sent: " + (char)whichKey, 10, 100);  
// Input for testing.

// Update the sensor value to the meter.  
m.updateMeter(sensorValue);  
// Use a delay to see the changes.  
delay(700);  
}

void serialEvent(Serial myPort) {  
String delim = “v1m”;  
int l1;

```
if(myPort.available() > 0) {
  //println(deviceData);
  deviceData = myPort.readStringUntil('\n');
  //The * must be the first char in the received String so we know we have a string meant for 
  //Processing, otherwise it is a debug
  //statement to just display in the console or in a TEXT object.
  if(deviceData.indexOf(delim) == 0) { 
    l1 = deviceData.length();
    Adata = deviceData.substring(3, l1-2); //Extract only the numerical values, I do not know why I 
   //have to subtract 2 characters from the length instead of just 1 in order to get int() to work?????
    println(Adata);
    sensorValue = int(Adata); 
    println(sensorValue);
  }
  else 
  {
    debugMsg = deviceData;
    print(debugMsg);
  }
}

```

//sendData(“Sending data to host\n”);  
}

void keyPressed() {  
// Send the keystroke out:  
myPort.write(key);  
whichKey = key;  
myPort.write(’\n’);  
//delay(5000);  
}

void sendData(String Sdata) {

myPort.write(Sdata);

}

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 1, 2020, 8:10pm UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/10 "2020-12-01T20:10:22Z")

</div>

Hello,

Take a look here:

_[https://processing.org/reference/trim\_.html](https://processing.org/reference/trim_.html)_

I always trim received data.

`:)`

---

<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: [December 1, 2020, 9:25pm UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/11 "2020-12-01T21:25:49Z")

</div>

Yeah, sometimes there is a line feed at the end of the data.

trim() helps.

Hey, and welcome to the forum!

Great to have you here!

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 2, 2020, 11:40am UTC](https://discourse.processing.org/t/convert-string-to-integer/2430/12 "2020-12-02T11:40:31Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

> [@terryz](#):
>
> In order to get the int() function to work I had to remove 2 characters from the received data and I do not understand why when I expected it to be only one.

I used simulated data here and only had to remove 1 character which was the line feed ( ‘\n’).

I may be going off topic here.  
Start another one if you want to continue this.

Please provide a small snippet of the Arduino code and please format properly for the forum.

Some test code without the Arduino sending:

> **Test Code**
>
> ```auto
> String s;
> 
> void setup() 
> {
> //s = "v1m1v1m2v1m3v1m4/n";
> char LF = '\n'; // 1 character
> //Serial.println(“v1m123”); 
> s = "v1m123" + LF; // 6 characters + 1 character for LF (line feed)
> testData(s);
> }
> 
> void testData (String s)
> {
> String delim = "v1m";
> int l1;
> 
> //String deviceData = trim(s);
> String deviceData = s;
> 
> if(deviceData.indexOf(delim) == 0) 
> {    
> l1 = deviceData.length();
>     
> String Adata = deviceData.substring(3, l1-1); //-1 for LF
> //Extract only the numerical values, I do not know why I 
> //have to subtract 2 characters from the length instead of just 1 in order to get int() to work?????
> println(Adata);
> int sensorValue = int(Adata); 
> println(sensorValue);
> }
> else 
> {
> String debugMsg = deviceData;
> print(debugMsg);
> }
> }
> 
> ```

Here is another topic that may be of interest:

_[Serial plotter over bluetooth - #11 by glv](https://discourse.processing.org/t/serial-plotter-over-bluetooth/22378/11)_

Arduino can send data that is comma delimited (or other delimiter) and terminated with a line feed (or other [terminator](https://en.wikipedia.org/wiki/The_Terminator)) and Processing can receive and split it.

`:)`
