Hey guys, I need help with my homework. I'm super new to coding on my own as you can see and I need to create a character counter using an array, in just this one method.

public int[] letterCounts(String s) {
		int count = 0;
		int[] array= new int [27];
		for(int i= 0; i < s.length(); i++){
			if(s.charAt(i)>=1);{
				count++;{

				}
			}
		}


		return array;
	}
}

So yeah, I'm stuck and I don't know what to do next. With only this code, the test for "" comes out as a pass but tests with actual strings come out as failed because the array entries are 0. And according to my notes, apparently I need an else statement after the if and I need to implement Characters.toLowerCase for some reason. Thanks a lot guys.

Oh and here's one of the tests

@Test
	public void test02() {
		String s = "Baaa!";
		LetterCounter lc = new LetterCounter();
		int [] expected = new int[27];
		expected[0] = 3;
		expected[1] = 1;
		expected[26] = 1;
		int [] actual = lc.letterCounts(s);
		String result = arraysAreTheSame(s,expected,actual);
		assertTrue(result, result.length()==0);
	}

Well, that helps with the toLowerCase I guess, but now I need help on the rest haha. Thanks for the link, appreciate it.

this method does everything for you:
- it searches every character if it is a "letter" (character) that's between 'a' and 'z', 'A' and 'Z', or '0' and '9'. (check ASCII code for help)
- if it is, it save them in another string that is returned.
The string returned has the alphanumeric characters. You can count the number of characters with .length() and thats it.

Ah, so would that go in the method I already have?

try it yourself, the better way to learn

;)

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.