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 › COnverting array of strings to ints.
Pages: 1 2 
COnverting array of strings to ints. (Read 1901 times)
COnverting array of strings to ints.
Nov 26th, 2009, 6:04am
 
I have managed to get a sketch to output a list of collected numbers as a text file (only 1 column, 1000 rows).

I have found many bits of info for loadStrings, and this works fine, but once I have the String[] array, I cant figure out how to iterate through and copy the numbers over to an int[] array.

Can copy to another string array ok, but the integer array just returns  0's when I run a println. Any ideas!?

Quote:
int[] xPositionArray = new int [1000];
String[] xStringsArray = new String [1000];
String[] testStringArray = new String [1000];

 void setup(){
   size(200, 200);
  xStringsArray = loadStrings("xPositionsArray26112009.txt");
  println(xStringsArray);
  
    for(int i=0; i < xPositionArray.length; i++){
    xPositionArray[i] = int(xStringsArray[i]);
    println(xPositionArray[i]);
  }
  
  
 }

Re: COnverting array of strings to ints.
Reply #1 - Nov 26th, 2009, 6:33am
 
try     xPositionArray[i] = Integer.parseInt(xStringsArray[i]);
Re: COnverting array of strings to ints.
Reply #2 - Nov 26th, 2009, 6:52am
 
Hmm, I cant get that working, is there a way of importing a list of integers straight into an array? - I have managed it in the past but only via lots of strings and a 'table class', which I really want to avoid!
Re: COnverting array of strings to ints.
Reply #3 - Nov 26th, 2009, 7:07am
 
now as i tested it i realized the problem was somewhere else...

int[] xPositionArray;
String[] xStringsArray;


void setup(){
  size(200, 200);

 xStringsArray = loadStrings("x.txt");
 xPositionArray = new int[xStringsArray.length];
 
   for(int i=0; i < xPositionArray.length; i++){
   xPositionArray[i] = int(xStringsArray[i]);
ellipse(  xPositionArray[i],100,4,4);
}
}
Re: COnverting array of strings to ints.
Reply #4 - Nov 26th, 2009, 7:35am
 
Processing's int() function is smart enough to convert a whole string array at once.
Re: COnverting array of strings to ints.
Reply #5 - Nov 26th, 2009, 12:22pm
 
PhiLo - can you see what's wrong here? It seems like there should be an easy solution to this problem! Thankyou. W.

Quote:
int[] xPositionArray = new int [1000];

 void setup(){
   size(200, 200);

 }
 
 
 
 void draw(){
   
     for(int i=0; i < 999; i++){
         String[] xStringsArray = loadStrings("xPositionsArray26112009.txt");
         xPositionArray[i] = int(xStringsArray[i]);
   //println(xStringsArray[999]);  
   println(xPositionArray);  
   }
   
 }
 
 //CURRENTLY THE STRINGS ARRAY READS OK.
 
 //INT ARRAY READS ZEROS.

Re: COnverting array of strings to ints.
Reply #6 - Nov 26th, 2009, 12:23pm
 
you didnt test my solution i guess...
so your problem is an indexOutOfBoundsException, right?
Re: COnverting array of strings to ints.
Reply #7 - Nov 26th, 2009, 12:55pm
 
Cedric - Yes I tried it, but I kept getting an array full of 0's like I got before, I have uploaded the text file I am trying to read if that's any help here?

I did get some out of bounds exception errors, but both the code you suggested and the one I posted most recently eliminate errors, but fall short of actually 'copying' the array.

The arrays are all 1000 lines long, so I thought it's be a bit silly to paste into the sketch,
eg
String x = 12, 40, 76, 123, 19 etc..

but do you think it would work?

Re: COnverting array of strings to ints.
Reply #8 - Nov 26th, 2009, 1:05pm
 
Woodlouse wrote on Nov 26th, 2009, 12:22pm:
can you see what's wrong here

Many things, like loading the strings in draw() (repetitive loading is a bad idea), doing the loadStrings in the loop (repetitive loading is ... Uh, I repeat myself!), etc.
The code in the first message was better.
Although you allocate a big array of strings, then drop it to replace it with the result of loadStrings... And as Cedric pointed out, you should allocate the same number of ints as the number of strings.

Beside, baring the remarks above, I see no blatant errors in the first code, so maybe the problem is in your file.

Testing it:
Code:
int[] xPositionArray;
// Simulate the loadStrings result
String[] xStringsArray = { "51", "0", "1586", "44" };

void setup(){
 size(200, 200);
//  xStringsArray = loadStrings("xPositionsArray26112009.txt");
 println(xStringsArray);
 xPositionArray = new int[xStringsArray.length];
 
 
 for(int i=0; i < xPositionArray.length; i++){
   xPositionArray[i] = int(xStringsArray[i]);
 }
 println(xPositionArray);
}

Using my trick:
Code:
int[] xPositionArray;
// Simulate the loadStrings result
String[] xStringsArray = { "51", "0", "1586", "44" };

void setup(){
 size(200, 200);
//  xStringsArray = loadStrings("xPositionsArray26112009.txt");
 println(xStringsArray);
 xPositionArray = int(xStringsArray);
 println(xPositionArray);
}


Beware of line endings. If I do, for example:
String[] xStringsArray = { "51\n", "0\n", "1586\n", "44\n" };
the int() calls fail.
Re: COnverting array of strings to ints.
Reply #9 - Nov 26th, 2009, 1:09pm
 
i made a test txt file and it worked.
how does your file look like ?
do you still have the commas in there?
Re: COnverting array of strings to ints.
Reply #10 - Nov 26th, 2009, 2:38pm
 
Ok, I see, I think it is to do with my file then, at the moment each number is on a different row, so I'll try using join() then feeding it back in!

Until then thanks both!
Re: COnverting array of strings to ints.
Reply #11 - Nov 26th, 2009, 2:53pm
 
hmm, just past a part of your line please.
Re: COnverting array of strings to ints.
Reply #12 - Nov 27th, 2009, 2:14am
 
Well this is how my numbers are exported from another sketch: http://www.samhumphrey.co.uk/applet/xPositionsArray26112009.txt

I didnt get it working yesterday for some reason.
Re: COnverting array of strings to ints.
Reply #13 - Nov 27th, 2009, 2:30am
 
A little progress! - It's not working if I paste my numbers from the file below and add in all he " " 's and ,'s between each number. Will try adding a split() now - seems a bit odd that the original list doesnt work, but hopefully by splitting it up now the new lis will be ok!
Re: COnverting array of strings to ints.
Reply #14 - Nov 27th, 2009, 2:37am
 
Yes this seems to have worked! Im not sure why this works rather than using the original string, but hopefully it will keep working! - Do either of you see why this works better? -
Code:


int[] xPositionArray = new int [1000];

void setup(){
size(200, 200);

String[] xStringsArray = loadStrings("xPositionsArray26112009.txt");
String joinedx = join(xStringsArray, ",");
println(xStringsArray);

// String[] simulateLoad = {"0", "28", "46", "47", "48", "54", "55", "55", "58", "58", "108",
// "138", "216", "254", "255", "256", "257", "258", "259", "259", "260", "261", "264", "266", "267",
// "268", "270", "271", "272", "272", "280", "282", "282", "286", "287", "287", "288", "292", "293",
// "294", "294", "295", "295", "296", "300"}; //TEST

xPositionArray = int(split(joinedx, ", "));

println(xPositionArray);


}
Pages: 1 2