Complete the main method in the SwitchCases class by: Declaring a String variable input, and storing the user’s input after asking: “Please enter a phrase: “.Using a for loop to iterate through the string stored inside input, and printing each character in the opposite case (e.g. ‘G’ becomes ‘g’ and vice versa.).Use the method charAt in the String class and isLowerCase, toUpperCase, toLowerCase in the Chararacter class
Question
Complete the main method in the SwitchCases class by: Declaring a String variable input, and storing the user’s input after asking: “Please enter a phrase: “.Using a for loop to iterate through the string stored inside input, and printing each character in the opposite case (e.g. ‘G’ becomes ‘g’ and vice versa.).Use the method charAt in the String class and isLowerCase, toUpperCase, toLowerCase in the Chararacter class
Solution
Here is a step-by-step guide on how to complete the main method in the SwitchCases class:
- First, you need to import the Scanner class from the java.util package at the beginning of your program. This class is used to get the user's input.
import java.util.Scanner;
- Declare the main method. This is the entry point of any Java application.
public static void main(String[] args) {
- Inside the main method, declare a String variable named 'input'. Also, create an instance of the Scanner class.
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a phrase: ");
String input = scanner.nextLine();
- Now, use a for loop to iterate through each character in the string stored in 'input'.
for (int i = 0; i < input.length(); i++) {
- Inside the for loop, use the charAt method to get each character. Then, use the isLowerCase method to check if the character is in lower case. If it is, convert it to upper case using the toUpperCase method. If it's not, convert it to lower case using the toLowerCase method.
char c = input.charAt(i);
if (Character.isLowerCase(c)) {
System.out.print(Character.toUpperCase(c));
} else {
System.out.print(Character.toLowerCase(c));
}
- Close the for loop, the main method, and the class.
}
}
}
Here is the complete code:
import java.util.Scanner;
public class SwitchCases {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a phrase: ");
String input = scanner.nextLine();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isLowerCase(c)) {
System.out.print(Character.toUpperCase(c));
} else {
System.out.print(Character.toLowerCase(c));
}
}
}
}
This program will take a phrase from the user and print it with the case of each character switched.
Similar Questions
Write a method that takes a String and returns it in AltCase.This means that the first letter is uppercase and each subsequent character alternates between lowercase and uppercase.Here are some examples of words in AltCase:HeLlOHeLlO WoRlDThIs iS In aLtCaSe.Note: if there is a space, you still treat that as a character, so the String "A B" is in AltCase, since technically, the space between the letters is lower case.
Which method can be used to return a string in upper case letters?
public class Solution { public static void main(String args[]) { int i = 2; switch(i) { case 1: System.out.print("Case 1" + " "); break; case 2: System.out.print("Case 2" + " "); break; case 3: System.out.print("Case 3" + " "); break; default: System.out.print("Default" + " "); } } } Options: Pick one correct answer from belowCase 1DefaultCase 2 Case 3 Case 4Case 2
Abi, an aspiring programmer, is intrigued by character manipulation. She seeks your assistance to write a program that takes two characters as inputs and implements a function called swapCase(), swaps their positions along with case conversion, and prints the modified characters.Input format :The input consists of two space-separated characters.Output format :The output displays the two space-separated characters, representing the modified values after swapping and converting cases.Refer to the sample output for formatting specifications.Code constraints :Input characters should be 'a' to 'z' or 'A' to 'Z'.Sample test cases :Input 1 :a ZOutput 1 :z AInput 2 :d kOutput 2 :K DInput 3 :K sOutput 3 :S k
Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.