0

The Following method receives an array of Character objects and returns a new array with only characters that are digits.

Example; old[] array:{1,R,Y,O,2,3,3 }----new [] array: {1,2,3,3}.

This is my code, and it is not returning what I want it to return.

public static char[] getDigits(char[] charArray) {    
4
  • So, what does it actually returns? Commented Feb 3, 2014 at 5:02
  • Works Great. Congos :ideone.com/JnOPzD Commented Feb 3, 2014 at 5:03
  • 1
    I'm missing part where you set length to toReturnDigits. (In java than means copying it...) Commented Feb 3, 2014 at 5:04
  • when I do a Junit Test on eclipse I get "array lengths differed, expected.length = 4 actual.length = 7" Commented Feb 3, 2014 at 5:09

5 Answers 5

1

I think you need to do two loops to size your array correctly.

public static char[] getDigits(char[] charArray) {
  int digitCount = 0;
  for (char ch : charArray) {
    if (Character.isDigit(ch)) {
      digitCount++;
    }
  }
  char[] toReturnDigits = new char[digitCount];
  int index = 0;
  for (char ch : charArray) {
    if (Character.isDigit(ch)) {
      toReturnDigits[index++] = ch;
    }
  }
  return toReturnDigits;
}

public static char[] getDigitsOld(char[] charArray) {
  int arrayLength = charArray.length;
  char[] toReturnDigits = new char[arrayLength];
  int index = 0;
  for (int i = 0; i < arrayLength; i++) {
    if (charArray[i] >= 48 && charArray[i] <= 57) {
      toReturnDigits[index++] = charArray[i];
    }
  }
  return toReturnDigits;
}

public static void main(String arg[]) {
  char[] old = new char[] { '1', 'R', 'Y', 'O', '2',
      '3', '3' };
  System.out.println(Arrays
      .toString(getDigitsOld(old)));
  System.out.println(Arrays
      .toString(getDigits(old)));
}

Outputs

[1, 2, 3, 3, 
[1, 2, 3, 3]
Sign up to request clarification or add additional context in comments.

Comments

0

Why not just checking if is a number?:

public static char[] getDigits(char[] charArray) {    
        int arrayLength = charArray.length;
        char[] toReturnDigits = new char[arrayLength];
        int index = 0;
        for (int i = 0; i < arrayLength; i++) {
            if (parseInt(charArray[i].toString(), 10)) { //assuming you expect base 10 digits
                toReturnDigits[index++] = charArray[i];
            }
        }
        return toReturnDigits;
    }

EDIT: To solve the issue with the array length you could use a dynamic array instead of preallocating it:

...

var toReturnDigits = [];

for (int i = 0; i < arrayLength; i++) {
  if (parseInt(charArray[i].toString(), 10)) { //assuming you expect base 10 digits
     toReturnDigits.push(charArray[i]);
    }
 }
return toReturnDigits;

2 Comments

How on earth this satisfied OP question ?? How it is cleaner than if (Character.isDigit(charArray[i]){} @OP you asked for the question array lengths differed in comments even, expected.length = 4 actual.length = 7 May I see how that resolved ?? Don't accept the answers just for the sake of acceptance. If you posted for this question to better way to check a digit, You haven't really checkedthe suggestions given in comments. If you closely check there is a library level check which is better in the link given by me. ideone.com/JnOPzD
@sᴜʀᴇsʜᴀᴛᴛᴀ: In the OP question the issue with the array length is not explicitly declared. Anyway, I edited my answer to fix that.
0

If you want to array to just contain the digits, you either have to count the number of digits before you create the array or use a list.

P.S. You should prefer to use Character.isDigit(char) as opposed to comparing.

Comments

0

Perhaps you could try something like:

public static char[] getDigits(final char[] array){
    final StringBuilder builder = new StringBuilder();
    for(final char c : array)
        if(Character.isDigit(c))
            builder.append(c);
    return builder.toString().toCharArray();
}

Comments

0

You can do this using regex easily.

public static char[] getDigits(char[] charArray) {
    Pattern p = Pattern.compile("-?\\d+");
    Matcher m = p.matcher(String.valueOf(charArray));
    String str=new String();
    while (m.find()) {
       str=str+m.group();
    }
    return str.toCharArray();
}

Demo

Again you can do this easy in following way too.

 public static char[] getDigits(char[] charArray) {
  String str=new String();
  for(int i=0;i<charArray.length;i++){
      if(Character.isDigit(charArray[i])){
          str=str+charArray[i];
      }
  }
  return str.toCharArray();
}

3 Comments

How is this an improvement over what they have?
@Whymarrh this is not an improvement and this is a suggestion.
Thanks for your response! But I haven't learn that much in my class, so i am very limited on what I can use and what not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.