Write a program to input a string and find frequency of a word entered
import java.util.Scanner;
public class Sword
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String s=sc.nextLine().toUpperCase();
System.out.println("Enter the Word to find its frequency");
String w=sc.nextLine().toUpperCase();
int frequency=0;
int index=s.indexOf(w);
while(index!=-1)
{
frequency++;
index=s.indexOf(w,index+1);
}
System.out.println("Frequency of the given Word in the String = "+frequency);
}
}
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.
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.

0 Comments