Results 1 to 4 of 4
  1. #1
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default 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:

    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);
                    
                    }
                }
    So this is what I don't get. The value of i on this line:

    Java Code:
     for (int i = 0; i<arrayChars.length; 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.

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Question on looping over arrays and getting back the results.

    I would expect to increment up till it reaches the value
    An easy way to see what values it gets is to print them out inside of the loop as it executes.
    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

  3. #3
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default 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

    Quote Originally Posted by Norm View Post
    An easy way to see what values it gets is to print them out inside of the loop as it executes.
    Also print out the length of the array before the loop starts.
    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).


    Quote Originally Posted by Norm View Post
    EDIT: Read the API doc for the find() method. You don't understand what it is doing.
    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Question on looping over arrays and getting back the results.

    it finds the 8 times find is true and returns the first 8 characters.
    Bottom line: There is no relationship between the character at the index i and where find() finds a match.

    Can you post your solution for others to see in case they are trying to do the same thing?

Similar Threads

  1. Getting back weird results
    By jenxin in forum New To Java
    Replies: 12
    Last Post: 03-03-2011, 02:00 AM
  2. Quick question about for looping and delay
    By DouboC@gmail.com in forum JDBC
    Replies: 0
    Last Post: 12-29-2010, 09:00 PM
  3. Looping question
    By lost_soul in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 04:34 AM
  4. posting results back to parent page
    By carag in forum New To Java
    Replies: 0
    Last Post: 07-29-2009, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •