Deleted for privacy.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Deleted for privacy.
Last edited by shanklove; September 7th, 2010 at 04:57 PM.
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.
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
Last edited by helloworld922; September 6th, 2010 at 11:16 AM.
See I was going to do that, however, the test case that we were given has to be Character[]:
@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 ); }
Last edited by eyp; September 6th, 2010 at 07:39 AM.
Eduardo Yáñez Parareda
http://serfj.sourceforge.net - Simple Ever REST Framework for Java
http://serfj.wordpress.com
You can do something like this... I think you don't need any String either a char[].
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(); }
Eduardo Yáñez Parareda
http://serfj.sourceforge.net - Simple Ever REST Framework for Java
http://serfj.wordpress.com
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.
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)
Last edited by shanklove; September 6th, 2010 at 01:02 PM.