Print Friendly, PDF & Email

In this tutorial you will learn how to do Java String count

  1.  the number of letters
  2. the number of vowels
  3. the number of consonants
  4. the number of spaces

For the backend we will implement a method for each desired property and to go further we will create an HTML view to display the results with the help of Dukescript all in less than 30 minutes.

Length of String

We can get the length of a text with the method length of the String class as follows:

int letters(String myText) {
return myText.length();
}

Count the spaces of a String

To count the spaces of a string we will use a basic Regular Expression to identify the char whitespace. The whitespace character can be represented as [ ] (please note the space between the brackets).

int spaces(String myText) {
 return myText.length()-myText.replaceAll("[ ]","").length();
}

Count the vowels of a String

To count the vowels we are using RegEx

int vowels(String myText) {
 return myText.length()-myText.replaceAll("[aeiouAEIOU]","").length();
}

Count the consonants of a String

To count the consonants we will select all letters that are not present in the vowels regex.

int consonants(String myText) {
 return myText.length()-myText.replaceAll("[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]","").length();
}

 

Desingning the Data Model

As we can detect we need for the data model a variable of type String named myText, that is meant to hold the text to be analyzed.

class StringStatistics{
 String myText;
 public StringStatistics(myText){
  this.myText=myText;
 }
}

Casting it to the dukescript way we are gonna have

@Model(className="StringStatistics", properties={
@Property(name="myText", type=String.class)
})

Designing the View

Next we are gonna create an HTML for the view like this

<textarea placeholder="Type a text to analyze...">
</textarea>
<p>
The string <pre></pre> has
<ul>
	<li></li>
	<li></li>
	<li></li>
	<li></li>
</ul>

Then we are gonna add the knockout bindings and our final HTML code becomes as following

<textarea
  data-bind="value: myText, valueUpdate: 'afterkeydown'"
  placeholder="Type a text to analyze...">
</textarea>
<p>
The string <pre data-bind="text: myText"></pre> has
<ul>
	<li data-bind="text: letters()+' letters.'"></li>
  	<li data-bind="text: vowels()+' vowels.'"></li>
    <li data-bind="text: consonants()+' consonants.'"></li>
    <li data-bind="text: spaces()+' spaces.'"></li>
</ul>

Desingning the Data Model

As we can detect we need for the data model a variable of type String named myText, that is meant to hold the text to be analyzed.

class StringStatistics{
 String myText;
 public StringStatistics(myText){
  this.myText=myText;
 }
}

Casting it to the dukescript way we are gonna have

@Model(className="StringStatistics", properties={
@Property(name="myText", type=String.class)
})

Migrate the Java code to Dukescript

Our final code will becomeas following

package dew.demo.string;

import net.java.html.json.Model;
import net.java.html.json.ComputedProperty;
import net.java.html.json.Property;

@Model(className="StringStatistics", properties={
@Property(name="myText", type=String.class)
})
class PrimeDemo {

@ComputedProperty static int letters(String myText) {
return myText.length();
}

@ComputedProperty static int vowels(String myText) {
return myText.length()-myText.replaceAll("[aeiouAEIOU]","").length();
}

@ComputedProperty static int consonants(String myText) {
return myText.length()-myText.replaceAll("[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]","").length();
}

@ComputedProperty static int spaces(String myText) {
return myText.length()-myText.replaceAll("[ ]","").length();
}

static {
new StringStatistics("").applyBindings();
}
}

[not_mobile]Here’s the live demo (it will not show on mobile):[/not_mobile]

In case you have troubles visualizing the demo, you can find it on the following link: http://dew.apidesign.org/dew/#fd4a282ff87f23473914c68baae95d03

Radio

Do NOT follow this link or you will be banned from the site!