Practice Simple String Methods
Practice Exercises
Here are a few exercises to practice using simple String methods in Java. These exercises will help reinforce your understanding of how to manipulate and validate strings using the methods we’ve discussed.
Exercise 1: Email Validation
Scenario: Create a program that checks if a user’s email address contains the “@” symbol and a “.” after the “@” to ensure it’s a valid format.
Task:
- Use the
contains()
method to check for the “@” symbol. - Use
indexOf()
andsubstring()
to ensure there is a “.” after the “@” symbol.
Instructions:
- Create a class
EmailValidator
. - Write a method
public boolean isValidEmail(String email)
that returnstrue
if the email is valid andfalse
otherwise. - Write an
EmailValidatorTester
class to test the method with various email addresses.
Expected Outcome:
- The program should correctly identify whether an email is valid based on the presence of “@” and “.”.
Exercise 2: Greeting Message Generator
Scenario: Create a program that generates a personalized greeting message by concatenating a greeting phrase with a user’s name.
Task:
- Use
concat()
or the+
operator to combine the greeting and the user’s name.
Instructions:
- Create a class
GreetingGenerator
. - Write a method
public String generateGreeting(String name)
that returns a personalized greeting message like “Hello, John!”. - Write a
GreetingGeneratorTester
class to test the method with different names.
Expected Outcome:
- The program should correctly generate a personalized greeting for any given name.
Exercise 3: Password Length Checker
Scenario: Create a program that checks if a password meets a minimum length requirement (e.g., 8 characters).
Task:
- Use the
length()
method to check the length of the password.
Instructions:
- Create a class
PasswordChecker
. - Write a method
public boolean isPasswordLongEnough(String password)
that returnstrue
if the password is long enough, andfalse
otherwise. - Write a
PasswordCheckerTester
class to test the method with various passwords.
Expected Outcome:
- The program should correctly determine if a password meets the minimum length requirement.
Exercise 4: Username Sanitizer
Scenario: Create a program that sanitizes a username by trimming any leading or trailing spaces and replacing any spaces within the username with underscores.
Task:
- Use
trim()
to remove spaces at the beginning and end. - Use
replace()
to replace spaces with underscores.
Instructions:
- Create a class
UsernameSanitizer
. - Write a method
public String sanitizeUsername(String username)
that returns the sanitized username. - Write a
UsernameSanitizerTester
class to test the method with various usernames.
Expected Outcome:
- The program should correctly sanitize usernames by trimming and replacing spaces.
Exercise 5: Case Insensitive Login
Scenario: Create a program that checks if a user’s entered username matches the stored username, ignoring case differences.
Task:
- Use
toLowerCase()
ortoUpperCase()
to standardize both the entered username and the stored username before comparison.
Instructions:
- Create a class
LoginSystem
. - Write a method
public boolean login(String enteredUsername, String storedUsername)
that returnstrue
if the usernames match, ignoring case. - Write a
LoginSystemTester
class to test the method with various usernames.
Expected Outcome:
- The program should correctly identify matching usernames regardless of case.
Exercise 6: Sentence Replacer
Scenario: Create a program that replaces all occurrences of a word in a sentence with another word.
Task:
- Use the
replace()
method to replace the specified word in the sentence.
Instructions:
- Create a class
SentenceReplacer
. - Write a method
public String replaceWord(String sentence, String oldWord, String newWord)
that returns the modified sentence. - Write a
SentenceReplacerTester
class to test the method with different sentences and words.
Expected Outcome:
- The program should correctly replace the specified word in any given sentence.
Exercise 7: String Trimmer and Length Checker
Scenario: Create a program that trims a string and then checks its length.
Task:
- Use
trim()
to remove extra spaces, and thenlength()
to measure the trimmed string.
Instructions:
- Create a class
StringTrimmer
. - Write a method
public int trimAndCheckLength(String input)
that returns the length of the trimmed string. - Write a
StringTrimmerTester
class to test the method with various strings.
Expected Outcome:
- The program should correctly trim the string and return its length.
Exercise 8: Case Conversion Utility
Scenario: Create a program that converts a given string to both uppercase and lowercase.
Task:
- Use
toUpperCase()
to convert the string to uppercase. - Use
toLowerCase()
to convert the string to lowercase.
Instructions:
- Create a class
CaseConverter
. - Write a method
public void convertCase(String input)
that prints both the uppercase and lowercase versions of the string. - Write a
CaseConverterTester
class to test the method with different strings.
Expected Outcome:
- The program should correctly display both the uppercase and lowercase versions of the input string.
Summary of Practice Exercises
- Validation: Practice using
length()
,contains()
, andtrim()
to validate and clean up user input. - String Manipulation: Practice using
replace()
,concat()
,toUpperCase()
, andtoLowerCase()
to modify and format strings. - Practical Application: These exercises cover real-world scenarios like login validation, text formatting, and message generation.
These exercises should help solidify your understanding of simple String methods in Java. Let me know if you need any further guidance or if you’d like to discuss the solutions!
Next Topic:
Go to Conditionals to learn more with examples.