FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   converting ascii to int
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: converting ascii to int  (Read 730 times)
thetourist3

Email
converting ascii to int
« on: Oct 13th, 2004, 5:33am »

hi i am a newbie and am interested in converting ascii text to integers from a website i am parsing... something that  in java is along the lines of using string tokenizer and intValue... i just wondered how i would do this. thanks.
 
fry


WWW
Re: converting ascii to int
« Reply #1 on: Oct 14th, 2004, 3:19am »

you can do the same thing in processing.. either you can use a StringTokenizer and intValue(), or in rev 68, there's splitStrings() that will do the work of a StringTokenizer (it's called split() in rev 69).  
 
rev 70 and higher (not yet publicly released) have even better support for this sort of thing too, so that it's a bit easier.
 
thetourist3

Email
Re: converting ascii to int
« Reply #2 on: Oct 15th, 2004, 8:22am »

awesome. look forward to using this and playing with the newer versions. thanks for the response.
 
thatbrock


Re: converting ascii to int
« Reply #3 on: Oct 15th, 2004, 5:31pm »

Err. Maybe I am a bit dense, but I don't understand how to use splitStrings() in rev68 to tokenize a string.
 
I am trying to read in a file using  String lines[] = loadStrings("foo.txt"); and to use numbers stored in this file as integers. Doesn't like it at all. The strings are stored in the array, but i can't use them as integers, and of course I can't just simply cast them.  
 
Also, I am using a simple loop to stuff the array, but I can't use splitStrings() within that loop.
 
Any advice would be really helpful!
 
fry


WWW
Re: converting ascii to int
« Reply #4 on: Oct 15th, 2004, 6:13pm »

loadStrings() will give you the contents of the file as an array of String objects, one line per array element:
http://processing.org/reference/loadStrings_.html
 
if each line contains only ints, separated by spaces, use splitInts() to turn that line into an array of ints.  
http://processing.org/reference/splitInts_.html
 
if only part of the line contains ints, first use splitStrings() to separate things into an array of String objects
http://processing.org/reference/splitStrings_.html
then do Integer.parseInt() on each one that you want to use:
i.e. if the first column is a word, then the others are ints:
String lines[] = loadStrings("foo.txt");
for (int i = 0; i < lines.length; i++) {
  String pieces[] = split(lines[i]);  // split on space chars
  String rowName = pieces[0];
  for (int j = 1; j < pieces.length; j++) {
    int num = Integer.parseInt(pieces[j]);
    println("next number is " + num);
  }
}
 
hope that helps.  
 
for what it's worth (or people that are curious) in future releases we're gonna try and get a 'foreach' structure in there so you don't have to count up to lines.length by hand, and casting will work on strings, so this line:
int num = Integer.parseInt(pieces[j]);
will change to:
int num = int(pieces[j]);
 
thatbrock


Re: converting ascii to int
« Reply #5 on: Oct 16th, 2004, 5:59am »

Thanks Fry for the fast reply!
 
For furture readers, the code you provided above, works if you make the change below since split is not a method:
 
String pieces[] = split(lines[i]);  // split on space chars  
 
Change to:
String pieces[] = splitStrings(lines[i]);  // split on space chars  
 
Such a fast response, a typo musta slipped in!
 
Pages: 1 

« Previous topic | Next topic »