Java Program to Check String being Singular or Plural – In this article, we will express the Java program to find out if any string is singular or plural. We will also include sample output and sufficient examples.
This code is for checking whether a given word is in plural form or not using Java language.
- The problem here is to determine whether a user-defined input string is a plural word or not.
- The constraint here is the inability to determine plurals that do not end with the alphabet s (example: sheep).
- The input is a string provided by the user.
- The output is a boolean value of true or false printed out which helps determine whether the input string is a plural word or not.
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 CheckPlural { public static void main(String arg[]) { int count=0; Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String name = sc.nextLine(); System.out.println("Is it plural("+name+") :"+checkPlural(name)); } static boolean checkPlural(String str) { int n=str.length(); if(str.charAt(n-1)=='s') { return true; } return false; } } |
Output – 1:
- Solution :
- In the main method, int variable count is initialized to 0 and a new Scanner class object is initialized and a reference variable sc is set to represent the object.
- This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
- The next line prints a statement to the console instructing the user to enter a string.
- The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the string variable name.
- Within the print statement in the last line, the checkPlural method is called and string name is passed in as the argument.
1234567static boolean checkPlural(String str) {int n=str.length();if(str.charAt(n-1)=='s') {return true;}return false;} - The length of the parameter str representing the argument name is stored in the int variable n in the first line of this method.
- A conditional statement checks whether the last character in the given string (last character is at index n-1) is the character s.
- If this condition is satisfied, the method returns a boolean value of true which is then printed out to the console and a boolean value of false is returned and printed out.
1 2 3 |
Enter the string changes Is it plural(changes) :true |
- Here, the input word is changes. The last alphabet is an s and so the condition in the method checkPlural is satisfied.
- The method returns a boolean value of true which is printed out.
Output – 2:
1 2 3 |
Enter the string change Is it plural(change) :false |
- Here, the input word is change. The last alphabet is not an s and so the condition in the method checkPlural is not satisfied.
- The method returns a boolean value of false which is printed out.