returning all string values that meet the condition

问题: Not sure how to set up this method which gets as parameter a String array and has to return in a new array all values that meet the following condition: 25% of characters i...

问题:

Not sure how to set up this method which gets as parameter a String array and has to return in a new array all values that meet the following condition: 25% of characters in every element of array are numbers;

public static String[] returnSentence(String[] str){
    int nrOfWords = str.length;
    int count = 0;
    for(int i = 0; i < nrOfWords; i++){
        for(int j = 0; j < str[i].length; j++){

        }
    }
}

I had an idea that it should be something like this but cant format the code to test the condition...


回答1:

Your question basically boils down to figuring out how many characters in the String fullfil a given condition, and how many do not.

There is two ways to do this:

1) Simply count the characters:

int numPositiveChars = 0;
int numNegativeChars = 0;
for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);        
    if (/*check c here*/)
        numPositiveChars++;
    else
        numNegativeChars++;
}

In fact you don't even need to count the negative chars, because that value is simply s.length() - numPositiveChars.

2) Another approach would be to use regular expressions, e.g. by removing all non-numerical characters and then get the character count:

int numPositiveChars = s.replaceAll("[^0-9]+", "").length();

This line will remove all characters from the String that are not numerical (not 0-9) and then return the length of the result (the number of characters that are numerical).

Once you have the number of chars that match your condition, calculating the percentage is trivial.


回答2:

You need to just replace all non digits in each element and then compare the length like so :

public static List<String> returnSentence(String[] str) {
    int nrOfWords = str.length;
    List<String> result = new ArrayList<>();
    for (int i = 0; i < nrOfWords; i++) {
        if(str[i].replaceAll("\D", "").length() == str[i].length() * 0.25){
            result.add(str[i]);
        }
    }
    return result; // If you want an array use : return result.toArray(String[]::new);
}

I would also use as result a List instead of array because you don't have any idea how many element is respect the condition.

If you want to solve with streaming it can be more easier :

public static String[] returnSentence(String[] str) {
    return Arrays.stream(str)
            .filter(s-> s.replaceAll("\D", "").length() == s.length() * 0.25)
            .toArray(String[]::new);
}

回答3:

some thing like this

public static String[] returnSentence(String[] str){
int nrOfWords= str.length;

String[] temp_Str = new String[20];
int count = 0;
int k=0;
for(int i = 0;i<nrOfWords;i++){
    for(int j = 0;j<str[i].length;j++){
      if(Character.isAlphabetic(str[i].getcharat(j)))
   {
     count++;
   }
   if((count/100.0)*100>=25)
    {  temp_Str[k]=str[i];
       k++;
       }


    }
}

}

  • 发表于 2019-03-27 02:50
  • 阅读 ( 464 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除