I have my code to switch the case from upper to lower and vice versa. I also have it to where it will toggle upper to lower, and lower to upper. My question is; is there a way I can get it to also include the character such as a comma or a period. For example, if I type in the string "Hello, there." I will get: "HELLO, THERE.", "hello, there" and "hELLOTHERE". How can I get it to where my third output will say "hELLO, THERE."
import java.util.*;
public class UpperLower2
{
public static void main(String[] args)
{
System.out.println("Enter in a sentence:");
Scanner input = new Scanner(System.in);
String sentence = input.nextLine();
System.out.println("All uppercase:" + sentence.toUpperCase());
System.out.println("All lowercase:" + sentence.toLowerCase());
System.out.println("Converted String:" + toggleString(sentence));
input.close();
}
public static String toggleString(String sentence)
{
String toggled = "";
for(int i=0; i<sentence.length(); i++)
{
char letter = sentence.charAt(i);
if(Character.isUpperCase(sentence.charAt(i)))
{
letter = Character.toLowerCase(letter);
toggled = toggled + letter;
}
else if(Character.isLowerCase(sentence.charAt(i)))
{
letter = Character.toUpperCase(letter);
toggled = toggled + letter;
}
}
return toggled;
}
}