Viserion
Viserion

Reputation: 13

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 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...

Upvotes: 1

Views: 172

Answers (3)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

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);
}

Upvotes: 1

Arsii Rasheed
Arsii Rasheed

Reputation: 351

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++;
       }


    }
}

}

Upvotes: 0

Max Vollmer
Max Vollmer

Reputation: 8598

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.

Upvotes: 1

Related Questions