Results 1 to 4 of 4
  1. #1
    sterf is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default match evaluation vs. pre increment

    Java Code:
    "7.07".substring(0,1).matches("[0-9]");
    vs.
    Java Code:
    int i=0;"7.07".substring(i,++i).matches("[0-9]")
    The first expression is true, the second is false. Why.
    To clarify, I expected that ++i would be executed before the substring was calculated and matched.

  2. #2
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: match evaluation vs. pre increment

    I ran the following code and got true for both...

    Java Code:
    package test;
    
    public class Main {
    
    	public static void main(String[] args) {
    		boolean test1;
    		boolean test2;
    		test1 = "7.07".substring(0,1).matches("[0-9]");
    		int i=0;
    		test2 = "7.07".substring(i,++i).matches("[0-9]");
    		System.out.println(test1);
    		System.out.println(test2);
    	}
    
    }

  3. #3
    sterf is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: match evaluation vs. pre increment

    Quote Originally Posted by fiveironfrnzy80 View Post
    I ran the following code and got true for both...

    Java Code:
    package test;
    
    public class Main {
    
    	public static void main(String[] args) {
    		boolean test1;
    		boolean test2;
    		test1 = "7.07".substring(0,1).matches("[0-9]");
    		int i=0;
    		test2 = "7.07".substring(i,++i).matches("[0-9]");
    		System.out.println(test1);
    		System.out.println(test2);
    	}
    
    }
    Thank you for your reply. Your code runs both times true on my machine too now. My code did return false however, it was inside a loop. I still have it in my display screen in eclipse
    "7.07".substring(firstDigitIndex, ++firstDigitIndex).matches("[0-9]")
    (boolean) false


    Very suspicious.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: match evaluation vs. pre increment

    It would be easier if you posted the actual code whose behaviour you can't understand.

    Par it down to something small and runnable that others can try for themselves.

Similar Threads

  1. Increment Counter for ID
    By blkshrk81 in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 01:25 AM
  2. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 04:45 PM
  3. Increment a Variable
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 02:57 AM
  4. How to create auto-increment
    By Albert in forum JDBC
    Replies: 2
    Last Post: 07-04-2007, 05:23 AM

Posting Permissions

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