#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Testing how many times an element occurs


    Hello everyone.

    While this is somewhat related to my other topic, this involves a different class. Specifically, this class is meant to count how many times an element occurs in an array, then when the test is run, return true when compared to how many times that array is expected.

    Code:
    public int howMany(String element) {
    	int amt = 0;
    	for (int i = 0; i < log.length; i++) {
    		if (element.equalsIgnoreCase(log[i]))
    			amt = amt++;
    		}
    	return amt;
    	}
    That is the relevant code for counting how many times an element occurs.

    Code:
    public void test_1_match_at_beginning() {	
    		strLog.insert("string 1"); strLog.insert("string 2");
    		strLog.insert("string 3"); strLog.insert("string 4");
    		strLog.insert("string 5"); 
    		
    		assertEquals(1, strLog.howMany("string 1"));		
    	}
    It fails on a relatively simple test like that. It doesn't matter what I change amt to either; it still fails. The only 3 tests it doesn't fail on are the ones with no elements. For some reason, 0 is ALWAYS expected.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Notice again, you are using the postincrement operator.

    Code:
    amt = amt++;
    When you use the postincrement operator as a value, the value is what the variable is before the increment takes place.

    Afterwards the amt is incremented by 1, but you reset amt back to what it was before amt was incremented.

    So amt doesn't change.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    That isn't the problem here.

    Changing it to amt+1 does nothing. Besides, the fact that it goes back to 0 even if I hardcode it as, say, 5 when it's initialized, was a pretty big hint to me that that wasn't the issue.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Note the first method returns 0.

    The second method returns 1.

    Code:
    public class Test5 {
       private static String[] log = {"string 1","string 2","string 3","string 4","string 5"};
     
       public static int howManyFirstMethod(String element) {
    	int amt = 0;
    	for (int i = 0; i < log.length; i++) {
    		if (element.equalsIgnoreCase(log[i]))
    			amt = amt++;
    		}
    	return amt;
       }
    
       public static int howManySecondMethod(String element) {
    	int amt = 0;
    	for (int i = 0; i < log.length; i++) {
    		if (element.equalsIgnoreCase(log[i]))
    			amt = amt+1;
    		}
    	return amt;
       }
    
       public static void main(String[] args) {
          System.out.println(howManyFirstMethod("string 1"));
    
          System.out.println(howManySecondMethod("string 1"));
       }
    
    }

IMN logo majestic logo threadwatch logo seochat tools logo