Requirements(java) that acheives somethings useful for programmers:
Write a program with a main method
Create at least one new method which is called from main that takes at least one parameter
Call at least two distinct methods from the String class
Utilize conditional statements or compound Boolean expressions
Utilize iteration
Assistant
Below is a simple Java program that conforms to the requirements you have listed. This program is a utility to search for a specific substring within a larger string and count the number of occurrences. It demonstrates the use of String methods, conditional statements, iteration, and custom method invocation with parameters.
java
public class SubstringFinder {
public static void main(String[] args) {
String text = "This is a simple string. This string will be searched for occurrences of a substring.";
String searchSubstring = "string";
// Call a method to search for the substring and get the count of occurrences
int count = countOccurrences(text, searchSubstring);
// Utilize conditional statements to provide feedback
if (count > 0) {
System.out.println("The substring '" + searchSubstring + "' was found " + count + " times.");
} else {
System.out.println("The substring '" + searchSubstring + "' was not found in the text.");
}
}
// Custom method with a parameter
public static int countOccurrences(String text, String sub) {
if (text == null || sub == null || text.isEmpty() || sub.isEmpty()) {
// Return 0 if either text or sub is null or empty
return 0;
}
int count = 0;
int index = 0;
// Utilize iteration to find all occurrences of 'sub' in 'text'
while ((index = text.indexOf(sub, index)) != -1) {
count++;
// Call a method from String class: 'length()'
index += sub.length(); // Move index forward to continue the search
}
// Utilize another method from the String class: 'toLowerCase()'
// This line doesn't affect the program's functionality; it's for demonstration.
text = text.toLowerCase();
return count;
}
}
Explanation of the program:
In main, we have a String object called text, which is the larger string we want to search within, and a String called searchSubstring, which is the substring we want to locate within text.
The main method then calls a custom method named countOccurrences with the text and searchSubstring as parameters.
The countOccurrences method processes the input parameters to count occurrences of the provided substring within the larger string.
This method uses the String class’s indexOf method to search for the substring and the length method to skip the already found substring when iterating.
The while loop employs iteration and conditional logic to determine when the end of the string is reached and to continue looking for more occurrences.
After completion, the method returns the number of occurrences found to main, where conditional statements give different outputs based on whether the substring was found and the count of occurrences.
As additional fulfillment of the requirement, a no-op use of the String class's toLowerCase method illustrates calling a second method from the String class.
To execute this program, simply copy the code into a file named SubstringFinder.java, compile it with javac SubstringFinder.java and run it with java SubstringFinder. Ensure you have a JDK installed on your computer to compile and run Java programs.