Reputation: 21
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) {
Upvotes: 0
Views: 188
Reputation: 7742
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;
Upvotes: 0
Reputation: 35547
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();
}
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();
}
Upvotes: 0
Reputation: 201399
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]
Upvotes: 1
Reputation: 11939
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();
}
Upvotes: 0
Reputation: 549
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.
Upvotes: 0