Java Program To Calculate Number Of Words In A String – In this article, we will brief in on all the methods to calculate the number of words in a string. The following java code has been written in multiple ways, if you have any suggestions or queries do leave a comment about number of words in string.
The methods used in this article are as follows:
- Using a Static Method
- Using Scanner Class
- Using Command-Line Arguments
- Using Separate Class
A string is a data type used in programming like an integer or a floating – point but it is used to represent text whenever it is required instead of numerical.
As you can see, the string mentioned here is “Hello”. The string consists of 5 characters. All of the 5 characters are represented by a pointer. The location is represented by a pointer, in this case, starting from 0 to 4.
Similarly, other strings also have ideal lengths where the space between the two words is also counted.
Thus, the methods used to find the length of a string in Java programming are as follows
Number Of Words In A String – Using a Static Method
Static methods in Java are known for beings the methods that work without the need for creating objects of that class.
In here, initially a string is read and stored in String variable ‘a’.
Then the static method named ‘Length’ is called. In the Length method, an integer variable c is declared which maintains the count of the number of words in the input string.
Every character in the string is checked for a few conditions. First it’s checked whether the index is not out of bound and whether the character at that position in a blankspace.
This is done because one word ends with a space. If it is a space, then it is immediately verified if the character before it is a space as well.
This is done only to ensure that one doesn’t end up counting the same word twice (in case we give 2 or more blankspace while entering a string). When all these conditions are met, then the count (variable c) is incremented by 1.
Another possibility is to encounter a blankspace in the beginning of the string itself, even under that case we increment the count by 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; class WordsInaString { public static void main(String arg[]) { Scanner sc= new Scanner(System.in); System.out.println("enter a string :"); String a = sc.nextLine(); //char ch[]=new char[s.length()]; int count=Length(a); System.out.println("number of words in a given string is :"+count); } static int Length(String s) { int c=0; for (int i = 0;i<=(s.length()-1);i++) { if( ( (i>0)&& (s.charAt(i)!=' ') &&(s.charAt(i-1)==' ')) || ((s.charAt(i)!=' ')&&(i==0)) ) c++; } return c; } } |
Output:
1 2 3 |
enter a string : NEVER GIVE UP number of words in a given string is :3 |
Using Scanner Class
Scanner is a class that is mainly meant to be used for taking inputs at runtime to make it much more user friendly.
This can be used for different primitive data types like int,double,etc.
Even in case of scanner class, the main logic of the program is still the same as the one used in Static method, the only difference here is that, we do not use a separate static method to invoke the main logic. In the main method itself, the logic is written.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class WordsInaString { public static void main(String arg[]) { Scanner sc= new Scanner(System.in); System.out.println("enter a string"); String s = sc.nextLine(); //String s=new String("welcome to java programs hub"); //char ch[]=new char[s.length()]; int count=0; for (int i = 0;i<=(s.length()-1);i++) { if( ( (i>0)&& (s.charAt(i)!=' ') &&(s.charAt(i-1)==' ')) || ((s.charAt(i)!=' ')&&(i==0)) ) count++; } System.out.println("number of words in a given string is :"+count); } } |
Output:
1 2 3 |
enter a string hello world number of words in a given string is :2 |
Words in a String Java – Using Command Line Arguments
Command line arguments in Java are the arguments given in the command line while running the java program.
Similarly here, we can directly pass the string as an argument in command line while running the code.
Since every word is naturally separated by space, by just using the length method we can figure out the number of words in the string we passed.
Not just that, every argument(word) is given a separate index as well, so we can also find the positioning of each word as well.
1 2 3 4 5 6 7 8 9 |
class WordsInaString { public static void main(String x[]) { System.out.println("number of words = "+x.length); for(int i=0;i<x.length; i++) System.out.println((i+1)+":"+x[i]); } } |
Output:
1 2 3 4 5 6 7 |
WordsInaString input given at command window number of words = 5 0:input 1:given 2:at 3:command 4:window |
Using Separate Class
Another possible way of writing this is, in a separate class. In this case the length method (constructor) is written in a different class consisting of the same logic. Then, in the main class, under the main function we create an object referring to the length class.
When the object is created, automatically the methods and data is the Length class is now usable in the main class as well. So, by calling the length method it automatically goes through the execution of the logic and returns the count of words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Scanner; class Length { int c=0; Length(String s) { for (int i = 0;i<=(s.length()-1);i++) { if( ( (i>0)&& (s.charAt(i)!=' ') &&(s.charAt(i-1)==' ')) || ((s.charAt(i)!=' ')&&(i==0)) ) c++; } } } class WordsInaString { public static void main(String arg[]) { Scanner sc= new Scanner(System.in); System.out.println("enter a string :"); String a = sc.nextLine(); Length count=new Length(a); System.out.println("number of words in a given string is :"+count.c); } } |
Output:
1 2 3 |
enter a string : welcome to java programs number of words in a given string is :4 |