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 & HelpSyntax Questions › Import string from csv and then turning into int
Page Index Toggle Pages: 1
Import string from csv and then turning into int (Read 2247 times)
Import string from csv and then turning into int
May 19th, 2010, 2:19pm
 
Hi,
I'm having some issues trying to import some values from a csv file and turn them into int values.
Does anyone know any way I'd be able to do it?
Re: Import string from csv and then turning into int
Reply #1 - May 19th, 2010, 2:26pm
 
Integer.parseInt(String)
Re: Import string from csv and then turning into int
Reply #2 - May 19th, 2010, 3:00pm
 
why not just int(String)?
Re: Import string from csv and then turning into int
Reply #3 - May 19th, 2010, 3:10pm
 
true, processing does it by itself.
i guess i was confused by the question and thought of a more complicated answer than that.
Re: Import string from csv and then turning into int
Reply #4 - May 20th, 2010, 12:21am
 
what would be the correct syntax to use when using the int(String) method?
Re: Import string from csv and then turning into int
Reply #5 - May 20th, 2010, 12:47am
 
that is the correct syntax.

int num = int(String);
Re: Import string from csv and then turning into int
Reply #6 - May 20th, 2010, 3:06am
 
Still have some problems.

Here's my code:
Code:
String lines[] = loadStrings("list.csv");
int nums = Integer.parseInt("lines");


Which returns this error:
"processing.app.debug.RunnerException: NumberFormatException: For input string: "lines""

I've also tried:
Code:
String lines[] = loadStrings("list.csv");
int i = int(lines);

and
Code:
String lines[] = loadStrings("list.csv");
int i = parseInt(lines);


Both return the same error:
"Cannot convert from int[] to int"

Trying:
Code:
String lines[] = loadStrings("list.csv");
Integer.parseInt(lines);


Gets me this error:
"The method ParseInt(String) in the type integer is not applicable for the arguments (String[])"
Re: Import string from csv and then turning into int
Reply #7 - May 20th, 2010, 3:21am
 
String[] lines = loadStrings("lines.csv");
int[] lines_int = new int[lines.length];

for(int i = 0; i < lines.length; i++) {
lines_int[i] = int(lines[i]);
}
Re: Import string from csv and then turning into int
Reply #8 - May 20th, 2010, 3:35am
 
Thanks Rapatski, that's working a charm.
Only thing now is that the new ints haven't kept their original numbers, they're now either 0 or 1.

/edit. Ignore that, it's just working a charm. Thank you.
Re: Import string from csv and then turning into int
Reply #9 - May 20th, 2010, 4:43am
 
You were close in your second attempt.
This should work:
Code:
String[] lines = loadStrings("list.csv");
int[] numbers = int(lines);

Although this should work only if there is one number per line. Since you wrote that Rapatski's solution worked, this should work too.
Page Index Toggle Pages: 1