I am trying to get this to work and am stuck. I run a JUnit test and get a few failures. Can someone show me the way to make this work?

/**
	 * int numberOf(String s, String characters)
	 * 
	 * Returns the number of times any of the chars in the second String occur in the first String
	 * 
	 * if s is "", then no matter what characters is the answer is 0 (zero)
	 * if s is "abcdefg", and characters is "f", then the correct answer is 1
	 * if s is "farfalle", and characters is "alf", then the correct answer is 6
	 * 
	 * Some examples:
	 * 
	 * The only methods you may call on either String are charAt(int) and length().
	 * 
	 * @param s is the original String
	 * @param characters is the String whose constituent character counts in s we want to determine
	 * @return the number of occurrences of the chars in characters in s 
	 */
	public int numberOf(String s, String characters) 
	{

			int totalchar = characters.length();
			int a =0;
			for (int t=0;t<characters.length();t++)
			{
				if (s.charAt(t)== characters.charAt(t))
			a++;		
			}
			return a;

Perhaps you could elaborate on "a few failures" rather than expecting people to guess?

error on testMultiCountInEmpty
-

public void testMultiCountInEmpty() {
		String s = _s0;
		String cs = "a";
		int expected = 0;
		int actual = _counter.numberOf(s, cs);
		assertTrue("I expected the count of the chars in \""+cs+"\" in the String \""+s+"\" to be "+expected+", but the computed answer was "+actual, expected == actual);
	}

Failures on the next three

@Test
	public void testMultiCountInShortOddLengthStringLast() {
		String s = _s1;
		String cs = "c";
		int expected = 1;
		int actual = _counter.numberOf(s, cs);
		assertTrue("I expected the count of the chars in \""+cs+"\" in the String \""+s+"\" to be "+expected+", but the computed answer was "+actual, expected == actual);
	}

	@Test
	public void testMultiCountInShortOddLengthStringMiddle() {
		String s = _s1;
		String cs = "b";
		int expected = 1;
		int actual = _counter.numberOf(s, cs);
		assertTrue("I expected the count of the chars in \""+cs+"\" in the String \""+s+"\" to be "+expected+", but the computed answer was "+actual, expected == actual);
	}
	
	@Test
	public void testMultiCountMultiDigitMixedOccurrence() {
		String s = _s4;
		String cs = "014";
		int expected = 6;
		int actual = _counter.numberOf(s, cs);
		assertTrue("I expected the count of the chars in \""+cs+"\" in the String \""+s+"\" to be "+expected+", but the computed answer was "+actual, expected == actual);
	}

failures on the next three

@Test
	public void testMultiCountUpperLowerMultiOccurrence() {
		String s = _s5;  // "Friends, Romans, countrymen, lend me your ears! I come to bury Caesar, not to praise him.";  // from Julius Caesar, by William Shakespear (as presented at http://www.gutenberg.org/cache/epub/1120/pg1120.txt)
		String cs = "Ii";
		int expected = 4;
		int actual = _counter.numberOf(s, cs);
		assertTrue("I expected the count of the chars in \""+cs+"\" in the String \""+s+"\" to be "+expected+", but the computed answer was "+actual, expected == actual);
	}

	@Test
	public void testMultiCountUpperSingleOccurrence() {
		String s = _s5;  // "Friends, Romans, countrymen, lend me your ears! I come to bury Caesar, not to praise him.";  // from Julius Caesar, by William Shakespear (as presented at http://www.gutenberg.org/cache/epub/1120/pg1120.txt)
		String cs = "I";
		int expected = 1;
		int actual = _counter.numberOf(s, cs);
		assertTrue("I expected the count of the chars in \""+cs+"\" in the String \""+s+"\" to be "+expected+", but the computed answer was "+actual, expected == actual);
	}

	@Test
	public void testMultiCountLowerMultiOccurrence() {
		String s = _s5;  // "Friends, Romans, countrymen, lend me your ears! I come to bury Caesar, not to praise him.";  // from Julius Caesar, by William Shakespear (as presented at http://www.gutenberg.org/cache/epub/1120/pg1120.txt)
		String cs = "i";
		int expected = 3;
		int actual = _counter.numberOf(s, cs);
		assertTrue("I expected the count of the chars in \""+cs+"\" in the String \""+s+"\" to be "+expected+", but the computed answer was "+actual, expected == actual);
	}

You have to compare two different arrays of characters, which would require two loops, yes?

I only see a single loop in your function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.