Results 1 to 4 of 4
- 02-07-2012, 11:45 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
Question on looping over arrays and getting back the results.
Hello again folks.
OK, so here's something I can't wrap my head around and hopeing some of the nice people in here can show me where I'm going wrong. Take the following snippit of code:
So this is what I don't get. The value of i on this line:Java Code:public class MyExtendedClass extends ExtendedGUI { private char [] arrayChars; protected static final String STRING_TO_ANALYSE = "A1234ZN567LR89TN*0#P"; private Pattern en = Pattern.compile("[A-Z]"); private Matcher fi = en.matcher(STRING_TO_ANALYSE); private char aChar; public MyExtendedClass() { arrayChars = STRING_TO_ANALYSE.toCharArray(); for (int i = 0; i<arrayChars.length; i++) { aChar = arrayChars[i]; if(fi.find()) { System.out.println(arrayChars[i] + "--" + i); } }
I would expect to increment up till it reaches the value of arrayChars.length. But, in the code above when I ask to print out the value of arrayChars[i] only when it finds a pattern match against the regular expression, the value of i isn't as the counter, it only gives an incremental value when the find is true. In other words, there are 19 characters in the array and I only want to print out the characters at the index position when find is true. Instead it finds the 8 times find is true and returns the first 8 characters. Why is this the case? I can't wrap my head around it.Java Code:for (int i = 0; i<arrayChars.length; i++)
Thanks in advance.
- 02-08-2012, 01:51 AM #2
Re: Question on looping over arrays and getting back the results.
An easy way to see what values it gets is to print them out inside of the loop as it executes.I would expect to increment up till it reaches the value
Also print out the length of the array before the loop starts.
EDIT: Read the API doc for the find() method. You don't understand what it is doing.Last edited by Norm; 02-08-2012 at 02:00 AM. Reason: find API
- 02-09-2012, 08:23 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
Re: Question on looping over arrays and getting back the results.
Hi Norm, thanks for taking the time out to post a reply, first off
Yes, that was my point. I did that and get 01234567 and not 0,5,6,10,11,14,15,19 as I would expect (i.e. when find() is true).
Not sure where your going with that. I selected the find() method as I intentionally want to use regex to find the correct characters in the string. Non A-Z characters are used for something else.
thanks
P.S. I got it to work, albeit not in the way described above but find() in pattern matching is behaving exactally as the API describes.Last edited by elliotHenry; 02-09-2012 at 08:24 PM. Reason: typo
- 02-09-2012, 10:01 PM #4
Re: Question on looping over arrays and getting back the results.
Bottom line: There is no relationship between the character at the index i and where find() finds a match.it finds the 8 times find is true and returns the first 8 characters.
Can you post your solution for others to see in case they are trying to do the same thing?
Similar Threads
-
Getting back weird results
By jenxin in forum New To JavaReplies: 12Last Post: 03-03-2011, 02:00 AM -
Quick question about for looping and delay
By DouboC@gmail.com in forum JDBCReplies: 0Last Post: 12-29-2010, 09:00 PM -
Looping question
By lost_soul in forum New To JavaReplies: 1Last Post: 05-11-2010, 04:34 AM -
posting results back to parent page
By carag in forum New To JavaReplies: 0Last Post: 07-29-2009, 12:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks