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.

Results 1 to 7 of 7

Thread: Help with Character arrays

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with Character arrays

    Deleted for privacy.
    Last edited by shanklove; September 7th, 2010 at 04:57 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with Character arrays

    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.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with Character arrays

    Quote Originally Posted by helloworld922 View Post
    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 ) {
            char[] charArray = new char[] {array};
            String str = new String(charArray);
            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.
    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 );
    	}

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Character arrays

    Quote Originally Posted by helloworld922 View Post
    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.

    char[] charArray = new char[] {array};
    That won't compile, you're assigning a char[] to char.
    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

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Character arrays

    Quote Originally Posted by shanklove View Post
    My thought is that I need to take the array and make it into a String. Then take the string and take out everything that isn't a digit. Then return that String. But like I said, it's not working. Eclipse is throwing me the error:

    Character[] charArray = new Character[] {array};
    "Cannot convert from Character[] to Character

    and
    String str = new String(charArray);
    "The constructor String(Character[]) is undefined".
    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

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with Character arrays

    Quote Originally Posted by eyp View Post
    That won't compile, you're assigning a char[] to char.
    oops, my bad. I accidentally mixed several thoughts together (yay for late nights)

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with Character arrays

    Quote Originally Posted by eyp View Post
    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();
        }
    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.

Similar Threads

  1. Assignment: Display Character
    By kidsforsale in forum Java Theory & Questions
    Replies: 1
    Last Post: September 2nd, 2010, 03:34 PM
  2. [SOLVED] Unknown Character
    By aussiemcgr in forum Java Theory & Questions
    Replies: 19
    Last Post: September 1st, 2010, 05:22 PM
  3. [SOLVED] last character position
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 05:31 AM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM