Write a program to input a string and count the number of vowels in it
import java.util.Scanner;
public class vowel
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
String s=sc.nextLine();
s=s.toUpperCase();
int l=s.length();
int v=0;
char tc;
for(int i=0;i<l;i++)
{
tc=s.charAt(i);
if(tc=='A' || tc=='E' || tc=='I' || tc=='O' || tc=='U')
v++;
}
if(v>0)
System.out.println("The number vowels are="+v);
else
System.out.println("No Vowels Found");
}
}
Click on the picture for full view
📚 Core Terms & Concepts Used in This Program
If you are a beginner, a school student, or just starting out with Java and BlueJ, here is a quick breakdown of the exact terms used in this program to help you master the logic:
Import Statement: A command used at the very top of your program to bring in external classes or entire packages. It saves you from writing out full, complex definitions and significantly improves code readability.
java.util: A built-in Java utility package. It contains highly useful framework tools, including random number generators, string parsers, and input scanning classes.
Scanner Class: A specific class found within the java.util package that allows your program to accept input from the user via the keyboard. To use it, we create a scanner object and call its built-in methods (like nextLine()).
public: An access modifier keyword that makes a class, method, or variable visible and accessible to all other classes in your program.
static: A keyword indicating that a method or attribute belongs to the class itself, rather than a specific object. This means it can be executed without creating an instance of the class first.
void: A keyword used in method declarations to specify that the method does not return any value after executing its task.
main: The starting point of any Java application. It is the specific identifier name that the Java Virtual Machine (JVM) looks for to begin executing your code. Note that while it is vital, main is an identifier, not a system keyword.
int: A primitive data type used to store standard whole numbers (integers). By default, it is a 32-bit signed value.
System.out.println(): The standard output statement in Java. System.out refers to the standard output stream, and println() prints whatever argument you pass into it, automatically moving the cursor to a fresh line afterward.
String[] args: This represents an array of text sequences (Strings) passed directly to the main function when a program is executed from a command line environment.
if / if-else: The fundamental decision-making structural statements in Java. An if block executes code only if a specified condition evaluates to true. Adding an else block provides an alternative path of code to run if that condition is false.
double: A primitive data type used to declare variables that need to hold fractional or decimal numbers. It represents a double-precision 64-bit floating-point value.
toUpperCase() Method: A built-in Java String method that converts all characters of a string into capital letters. Using this prevents us from having to check for both lowercase (a, e, i, o, u) and uppercase (A, E, I, O, U) variants separately.
|| (Logical OR) Operator: An operator used in conditional statements that returns true if at least one of the conditions being checked is true. In this program, it ensures the counter increments if the character matches any of the five vowels.
vowelCount++ (Increment Operator): A shorthand operation in Java that increases the value of an integer variable by exactly 1. It is equivalent to writing vowelCount = vowelCount + 1.
If you are a beginner, a school student, or just starting out with Java and BlueJ, here is a quick breakdown of the exact terms used in this program to help you master the logic:
Import Statement: A command used at the very top of your program to bring in external classes or entire packages. It saves you from writing out full, complex definitions and significantly improves code readability.
java.util: A built-in Java utility package. It contains highly useful framework tools, including random number generators, string parsers, and input scanning classes.ScannerClass: A specific class found within thejava.utilpackage that allows your program to accept input from the user via the keyboard. To use it, we create a scanner object and call its built-in methods (likenextLine()).public: An access modifier keyword that makes a class, method, or variable visible and accessible to all other classes in your program.static: A keyword indicating that a method or attribute belongs to the class itself, rather than a specific object. This means it can be executed without creating an instance of the class first.void: A keyword used in method declarations to specify that the method does not return any value after executing its task.main: The starting point of any Java application. It is the specific identifier name that the Java Virtual Machine (JVM) looks for to begin executing your code. Note that while it is vital,mainis an identifier, not a system keyword.int: A primitive data type used to store standard whole numbers (integers). By default, it is a 32-bit signed value.System.out.println(): The standard output statement in Java.System.outrefers to the standard output stream, andprintln()prints whatever argument you pass into it, automatically moving the cursor to a fresh line afterward.String[] args: This represents an array of text sequences (Strings) passed directly to themainfunction when a program is executed from a command line environment.if/if-else: The fundamental decision-making structural statements in Java. Anifblock executes code only if a specified condition evaluates to true. Adding anelseblock provides an alternative path of code to run if that condition is false.double: A primitive data type used to declare variables that need to hold fractional or decimal numbers. It represents a double-precision 64-bit floating-point value.toUpperCase()Method: A built-in Java String method that converts all characters of a string into capital letters. Using this prevents us from having to check for both lowercase (a, e, i, o, u) and uppercase (A, E, I, O, U) variants separately.||(Logical OR) Operator: An operator used in conditional statements that returns true if at least one of the conditions being checked is true. In this program, it ensures the counter increments if the character matches any of the five vowels.vowelCount++(Increment Operator): A shorthand operation in Java that increases the value of an integer variable by exactly 1. It is equivalent to writingvowelCount = vowelCount + 1.

0 Comments