 # How do i add characters in a string, i have 11 digits in my string and i am supposed to add the odd number positions

please format code with </> button * homework policy * asking questions

How do i add characters in a string, i have 11 digits in my string and i am supposed to add the odd number positions

Hello,

Tutorials \ Processing.org

Take a look at the String Functions in the references:
Language Reference (API) \ Processing 3+

`:)`

i did find a tutorial on displaying character by character but is it that i seperate then convert to integer then that is when i can add?

Hello,

Look through the references.
It is all in there.
Then piece it together.

This is your homework exercise and research is part of that.

`:)`

ohhh i saw what you mean, i found how to seperate them then converting, thank you!

import javax.swing.JOptionPane;
size(500,500);
String t= JOptionPane.showInputDialog(“Enter the first 11 digits of a UPC-A code:”); //this prompts the user to enter the first 11 didgits of a UPC-A code
char oddOne= t.charAt(0); //this is the odd-number position of 1
int oddOneOne=int(oddOne); // converting this character to an integer
char oddThree= t.charAt(2); //this is the odd-number position of 3
int oddThreeThree=int(oddThree); //converting this character to an integer
char oddFive=t.charAt(4); //this is the odd-number position of 5
int oddFiveFive=int(oddFive); //converting this character to an interger
char oddSeven= t.charAt(6); //this is the odd-number position of 7
int oddSevenSeven= int(oddSeven); //converting this character to an interger
char oddNine= t.charAt(8); //this is the odd-number position of 9
int oddNineNine= int(oddNine); //converting this character to an integer
char oddEleven=t.charAt(10); //this is the odd-number position of 11
int oddElevenEleven= int(oddEleven); //converting this character to an integer
char evenTwo=t.charAt(1);

this is what i have so far, im yet to add them, is it looking correct though?

https://discourse.processing.org/faq#format-your-code

https://discourse.processing.org/faq#homework

If you are using the Processing IDE to write code you can print to the console:

`53` is the correct value (decimal) for the ASCII character for `5`.

If you are converting a char to an int and want the value the ASCII character represents you will have to add an offset to it; take a look at the ASCII value for decimal 0 and notice they are sequential so the offset is the same.

The alternative is here if you want to convert a substring of a String directly to an int:
String \ Language (API) \ Processing 3+

`:)`

oh alright, i had manged to figure it out in the end, thank you so much for your help

1 Like

You are welcome.

It gets easier in time!

Every step you take, success or failure, is part of the learning process and is progress.

`:)`

i suppose you’re right, enjoy the rest of your day!

2 Likes