We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › convert string to int
Page Index Toggle Pages: 1
convert string to int (Read 4864 times)
convert string to int
Feb 24th, 2010, 10:41pm
 
I'm working with a set of strings that represent numerical values from 1-22. These values were loaded from a text file. I want to convert the strings into ints so that I can perform some logic so, I wrote the following method. However, the conversion does not happen. Any idea?
Many Thanks!

Code:
int stringToPosInt(String number){
 int j=0;
 for(int i=0; i < 23; i++){
   String temp = Integer.toString(i);
   println(number+temp+"*");
   if(number.equals(temp)){
j = i;
   }
 }
 return j;
}
Re: convert string to int
Reply #1 - Feb 24th, 2010, 10:58pm
 
I have three remarks about your code/method:
- Your code works. I am not sure why you tell otherwise. My test code:
Code:
void setup()
{
println("==> " + stringToPosInt("15"));
exit();
}

- In such search loop, I advise to put a break; after the assignment, to avoid superfluous extra iterations. Small speed gain.
- Your code is original, but a bit specialized and totally inefficient... Smiley
In a real world sketch, you can replace my test with:
println("==> " + int("15"));
Roll Eyes
Re: convert string to int
Reply #2 - Feb 25th, 2010, 8:59am
 
Perhaps some more info could help. The strings I've loaded are stored in an array of ArrayLists. I have no problem referencing each stored string, as long as I am performing some string operation. .e.g. println() or text(). Before calling the method I posted earlier, I cast the object, returned from the ArrayList as a String in a for loop index by ints i and j:


String num = (String)arrayN[i].get(j)


Could there be some issue in converting an object cast as String in the method I'm proposing from above?

Re: convert string to int
Reply #3 - Feb 25th, 2010, 9:23am
 
No.
Unless get() returns a null, but then you would have a NullPointerException.
Re: convert string to int
Reply #4 - Mar 6th, 2010, 3:26am
 

Hello,

yes, I think andrew.madison is right, there is an issue - or I ran into the same trap as he?   Cry

When I try

println ('A');
println (int('A'));

I get A and 65.

With
println (int('1'));
println (int("1"));
I get
49 and 1.

So the trouble might be that
an int from a char returns the
ascii and not the number.

I assume andrew.madison ran into this....

To avoid this I prepared a little function until someone shows me the light...  Wink

Greetings, Chrisir  



int CharFromArray(String MyTestArray, int i){

 return(MyTestArray.charAt(i));

} // int

int IntegerFromArray(String MyTestArray, int i){

 int Buffer = 0;

 switch (MyTestArray.charAt(i)) {
 case '0':
   Buffer = 0;
   break;

 case '1':
   Buffer = 1;
   break;

 case '2':
   Buffer = 2;
   break;

 case '3':
   Buffer = 3;
   break;  

 case '4':
   Buffer = 4;
   break;  

 case '5':
   Buffer = 5;
   break;  

 case '6':
   Buffer = 6;
   break;  

 case '7':
   Buffer = 7;
   break;  

 case '8':
   Buffer = 8;
   break;  

 case '9':
   Buffer = 9;
   break;  

 default:
   //
   break;

 } // switch

 return ( Buffer ) ;

}





convert char to int
Reply #5 - Mar 6th, 2010, 3:33am
 
Or you could go straight to:


int integerFromChar(char MyChar){

 int Buffer = 0;

 switch (MyChar) {
 case '0':
   Buffer = 0;
   break;

 case '1':
   Buffer = 1;
   break;

 case '2':
   Buffer = 2;
   break;

 case '3':
   Buffer = 3;
   break;  

 case '4':
   Buffer = 4;
   break;  

 case '5':
   Buffer = 5;
   break;  

 case '6':
   Buffer = 6;
   break;  

 case '7':
   Buffer = 7;
   break;  

 case '8':
   Buffer = 8;
   break;  

 case '9':
   Buffer = 9;
   break;  

 default:
   //
   break;

 } // switch

 return ( Buffer ) ;

}

Re: convert string to int
Reply #6 - Mar 6th, 2010, 3:39am
 
You could also use:

int IntegerFromArray(String MyTestArray, int i){

 return (MyTestArray.charAt(i) - '0');

}

Although, this does not catch non-numeric characters.

Re: convert string to int
Reply #7 - Mar 6th, 2010, 3:49am
 
println(Character.toString('1'));
Re: convert string to int
Reply #8 - Mar 6th, 2010, 3:56am
 
Chrisir wrote on Mar 6th, 2010, 3:26am:
yes, I think andrew.madison is right, there is an issue - or I ran into the same trap as he

No, the problems are different. He wanted to convert a string holding numbers to int. You want to convert a char holding a digit to int. String is made of chars, but they are different beasts.
NoahBuddy's solution is OK but addresses a different need (sticking to original goal of String parameter).
In your case, it would be just:
Code:
int integerFromChar(char myChar) {
 return myChar - '0';
}
// or, for a more robust function:
int integerFromChar(char myChar) {
 if (myChar < '0' || myChar > '9') return -1;
 return myChar - '0';
}

Re: convert string to int
Reply #9 - Mar 7th, 2010, 3:17am
 


Yes, thank you all very much!


Page Index Toggle Pages: 1