Deleted for privacy.
Printable View
Deleted for privacy.
Unfortunately, Java's auto-boxing and auto-unboxing feature has a few gaps in what's covered. If you change your array to a char array rather than a Character array it should work.
Code Java:public static String getDigits( char[] array ) { String str = new String(array); if (str == "") { return " "; } StringBuffer strBuff = new StringBuffer(); char c; for (int i = 0; i < str.length() ; i++) { c = str.charAt(i); if (Character.isDigit(c)) { strBuff.append(c); } } return strBuff.toString(); } }
A better solution though is to simply straight up take a string rather than a char array which may or may not be filled.
edit:
fixed typo
See I was going to do that, however, the test case that we were given has to be Character[]:
Code Java:@Test public void testGetDigitsWithListEmpty() { Character[] values = { }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { }; assertArrayEquals( "The result is incorrect", expected, actual ); } @Test public void testGetDigitsWithListWithNoDigits() { Character[] values = { 'y', 'o', 'd', 'a' }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { }; assertArrayEquals( "The result is incorrect", expected, actual ); } @Test public void testGetDigitsWithListWithLettersAndDigits() { Character[] values = { 'C', '-', '3', 'P', 'O' }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { '3' }; assertArrayEquals( "The result is incorrect", expected, actual ); } @Test public void testGetDigitsWithListWithDuplicatedDigits() { Character[] values = { 'a', '3', '2', 'b', '1', '2', 'c', '3', 'd' }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { '3', '2', '1', '2', '3' }; assertArrayEquals( "The result is incorrect", expected, actual ); }
You can do something like this... I think you don't need any String either a char[].
Code Java:public static String getDigits( Character[] array ) { if (array.length == 0) { return " "; } StringBuffer strBuff = new StringBuffer(); char c; for (int i = 0; i < array.length ; i++) { if (Character.isDigit(array[i])) { strBuff.append(array[i]); } } return strBuff.toString(); }
Looks good in theory, but it fails all of these tests:
It has something to do with "Lab03One.getDigits( values )"
Cannot convert from String to Character[].
Here is the whole test code, ignore the part about getMedian, all that works and compiled.
Code Java:import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.Test; public class Lab03OneTest { @Test public void testGetMedianWithEmptyList() { Integer[] values = { }; Double actual = Lab03One.getMedian( values ); Double expected = 0.0; assertEquals( "The result is incorrect", expected, actual ); } @Test public void testGetMedianWithListOneValue() { Integer[] values = { 5 }; Double actual = Lab03One.getMedian( values ); Double expected = 5.0; assertEquals( "The result is incorrect", expected, actual ); } @Test public void testGetMedianWithListEvenValues() { Integer[] values = { 22, 19, 20, 21 }; Double actual = Lab03One.getMedian( values ); Double expected = 20.5; assertEquals( "The result is incorrect", expected, actual ); } @Test public void testGetMedianWithListOddValues() { Integer[] values = { 42, -27, 8, -2, 59, 42, -1 }; Double actual = Lab03One.getMedian( values ); Double expected = 8.0; assertEquals( "The result is incorrect", expected, actual ); } @Test public void testGetDigitsWithListEmpty() { Character[] values = { }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { }; assertArrayEquals( "The result is incorrect", expected, actual ); } @Test public void testGetDigitsWithListWithNoDigits() { Character[] values = { 'y', 'o', 'd', 'a' }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { }; assertArrayEquals( "The result is incorrect", expected, actual ); } @Test public void testGetDigitsWithListWithLettersAndDigits() { Character[] values = { 'C', '-', '3', 'P', 'O' }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { '3' }; assertArrayEquals( "The result is incorrect", expected, actual ); } @Test public void testGetDigitsWithListWithDuplicatedDigits() { Character[] values = { 'a', '3', '2', 'b', '1', '2', 'c', '3', 'd' }; Character[] actual = Lab03One.getDigits( values ); Character[] expected = { '3', '2', '1', '2', '3' }; assertArrayEquals( "The result is incorrect", expected, actual ); } }
Not a clue where to go from here =\
Also, I took out "char c" in the code you suggested (it was unused code)