Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Notes On Java Cases In A Nutshell

Loading...

Published in: Java And J2EE
648 Views

To work with programs to tackle cases, like capitalizing first letter of each word in a sentence to uppercase, camel case, toggle case and other similar codes with explanations.

Prantik S / Kolkata

11 years of teaching experience

Qualification: MCA (Jaipur National University - [JNU], Jaipur - 2017)

Teaches: Basic Computer, Computer for official job, MS Office, School Level Computer, ICT Training, Computer Science, Information Practice, IT & Computer Subjects, BCA Tuition, IT, Computer, C / C++, C# (C Sharp), Java And J2EE, Python Programming, Visual Basic, BCA Subjects, Hardware Training, Networking, Java Script

Contact this Tutor
  1. Java Cases in a Nutshell: Converting a String to Camel Case As already said, in camel case, we capitalise the first letter of each word. To do so, we use a loop similar to what we have used in the previous conversions. Within the loop, we check if the character at index i-l is a space. If it is so, we capitalise the current character at index i and append it to the result. Otherwise, we convert the current char to lower case and append it. However, we will experience a problem when the loop counter i has the value of zero as accessing character at index (0-1) will result in an exception. To avoid this, before the loop, we check if the length of the input string is zero. If it is so, we simply return an empty String. Otherwise, we initialise the result with the capitalized version of the character at index zero and then start the loop from i = 1. The code is shown at the bottom of this page. Converting a String to Sentence Case In sentence case, the first letter of each sentence is capitalized. This conversion cannot be solved in a way similar to the camel case problem, replacing the space with a period. The reason is that a sentence does not always end in a period. Sentences also end with question marks and exclamation marks. The second reason is that the first letter of the sentence doesn't always immediately follow the period. In most cases, there is a space between the two. To tackle the above problems, we maintain an array of terminal characters - the characters with which a sentence ends such as period, exclamation mark and question mark. We will also use a boolean variable 'terminalCharacterEncounteredI which will be set to true whenever one of the terminal characters is encountered. Within the loop, we first extract a character, if the terminalCharacterEncountered flag is false, we simply append the character to the output String. If the flag was true, the current character is capitalized and appended to the result. And then, the flag is reset to false. After performing the above task, we check if the current character is one of the terminal characters. If so, the flag is set to true again so that the first character of the next sentence will be capitalised. The Program Given below is a complete program which takes an input String from the user, converts it into different cases and displays them on the console.
  2. import java.util.Scanner; public class CaseManipulation { public static String toUpperCase(String inputString) { String result = for (int i = 0; i < inputString.length(); i++) { char currentChar = inputString.charAt(i); char currentCharToUpperCase = Character.toUpperCase(currentChar); result = result + currentCharToUpperCase; return result; public static String toLowerCase(String inputString) { String result = for (int i = 0; i < inputString.length(); i++) { char currentChar = inputString.charAt(i); char currentCharToLowerCase = Character.toLowerCase(currentChar); result = result + currentCharToLowerCase; return result;
  3. public static String toToggleCase(String inputString) { String result = for (int i = 0; i < inputString.length(); i++) { char currentChar = inputString.charAt(i); if (Character.isUpperCase(currentChar)) { char currentCharToLowerCase = Character.toLowerCase(currentChar); result = result + currentCharToLowerCase; } else { char currentCharToUpperCase = Character.toUpperCase(currentChar); result = result + currentCharToUpperCase; return result; public static String toCamelCase(String inputString) { String result = if (inputString.length() 0) { return result; char firstChar = inputString.charAt(0); char firstCharToUpperCase = Character.toUpperCase(firstChar); result = result + firstCharToUpperCase; for (int i = 1; i < inputString.length(); i++) { char currentChar = inputString.charAt(i);
  4. char previousChar = inputString.charAt(i - 1); if (previousChar I) { char currentCharToUpperCase = Character.toUpperCase(currentChar); result = result + currentCharToUpperCase; } else { char currentCharToLowerCase = Character.toLowerCase(currentChar); result = result + currentCharToLowerCase; return result; public static String toSentenceCase(String inputString) { String result = if (inputString.length() 0) { return result; char firstChar = inputString.charAt(0); char firstCharToUpperCase = Character.toUpperCase(firstChar); result = result + firstCharToUpperCase; boolean terminalCharacterEncountered = false; char[] terminalCharacters = {1.1, 1!1},. for (int i = 1; i < inputString.length(); i++) { char currentChar = inputString.charAt(i); if (terminalCharacterEncountered) {
  5. if (currentChar I I) { result = result + currentChar; } else { char currentCharToUpperCase = Character.toUpperCase(currentChar); result = result + currentCharToUpperCase; terminalCharacterEncountered = false; } else { char currentCharToLowerCase = Character.toLowerCase(currentChar); result = result + currentCharToLowerCase; for (int j = 0; j < terminalCharacters.length; j++) { if (currentChar terminalCharacters[j]) { terminalCharacterEncountered = true; break; return result; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an input String: "); String inputString = scanner.nextLine();
  6. System.out.println("Upper Case: " + toUpperCase(inputString)); System.out.println("Lower Case: " + toLowerCase(inputString)); System.out.println("Toggle Case: " + toToggleCase(inputString)); System.out.println("Camel Case: " + toCamelCase(inputString)); System.out.println("Title Case: " + toSentenceCase(inputString));