Latest :

Java : Check String Is Singular or Plural | Java Programs

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.

Output – 1:

  • Solution :
  1. 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.
  2. 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.
  3. The next line prints a statement to the console instructing the user to enter a string.
  4. 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.
  5. Within the print statement in the last line, the checkPlural method is called and string name is passed in as the argument.
  6. The length of the parameter str representing the argument name  is stored in the int variable in the first line of this method.
  7. A conditional statement checks whether the last character in the given string (last character is at index n-1) is the character s.
  8. 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.
  • Here, the input word is changes. The last alphabet is an and so the condition in the method checkPlural is satisfied.
  • The method returns a boolean value of true which is printed out.

Output – 2:

  • Here, the input word is change. The last alphabet is not an and so the condition in the method checkPlural is not satisfied.
  • The method returns a boolean value of false which is printed out.

Check Also

Rhombus Star Pattern Program In Java – Patterns

Java program to print Rhombus star pattern program. We have written the below print/draw Rhombus ...