Sunday, February 05, 2006

In a sick sick way I love fixing little code problems...
today for example I needed to search through a text file and find this line...

"Log: Browse: DM-jake2?BonusVehicles=false?Game=XGame.xDeathMatch?

this line changes after every match of UT but the beginning part (and the basic structure) of the line always remains the same

"Log: Browse: DM-" (always starts like this)

So heres the problem... youve got lots of other array elements some of which also start
"Log: Browse:"

I want to find this particular line, and get only one piece of information out of it, the number of bots playing, in this case

how to do it...
well, I dont know the proper way, but this is how I did it :)

for(int i=0; i < file.length; i++){ //i cant include the "is less than" symbol *fixed now*

String temp = file[i]; //copys this particular line of the text file into a temporary string
int templength = temp.length(); //finds out the length of this string (because if its under 13 we will have problems later as I attempt to access the 13th char)

if (templength>13){
char temp1 = file[i].charAt(0); // L (Log:)
char temp2 = file[i].charAt(5); // B (Browse:)
char temp3 = file[i].charAt(13); // D (DM-) //have we got letters in the right place?

if ((temp1 == 'L') && (temp2 == 'B') && (temp3 == 'D')){ //do these letters match, is this our line?
String cutline[] = split(file[i], '?'); //cut the line into seperate array elements using the ?'s as cut markers
println (cutline[5]); //using this method numbots is always element 5

p.s. Ive just made the number of bots a usable number. It may seem simple but it took me a while to see the simplness...
String justbotnumber[]= split(cutline[5], '='); //split string into 2 at the equals
int botnumber = int(justbotnumber[1]); //int that string!


Blogger Tom Carden said...

Yeah, blogger isn't ideal for code is it? The characters themselves can really confuse things when parsing an html file which is why they cause problems - try using codes &lt; for a less than symbol and &gt; for a greater than symbol.

1:16 pm  
Blogger Alison said...

thanks, that worked great :)


1:27 pm  
Anonymous Anonymous said...

There's much easier ways to search through strings...

for(int i=0;i<file.length;i++)
int start=file[i].indexOf("?NumBots")+8;
// find the first occurance of ?NumBots (since there's bAutoNumBots too)
int end=file[i].indexOf("?",start);
// find the first ? after the ?NumBots since that's the end..
String a=file[i].substring(start,end);

10:57 pm  
Blogger Alison said...

owch.... so much smaller yet works perfectly!

thank you

i just cant believe how much shorter that is, I knew there must have been a simpler way than searching for specific leters at specific points, I mean come on! lol

Unfortunately Im such a beginner that at this stage If its not on the processing reference page I assume it dosnt exist!

thanks again

11:46 am  
Anonymous toastie said...

You should look into regular expressions.

6:39 pm  

Post a Comment

<< Home