Java program to convert a string to lowercase and uppercase

Java example to convert string to lowercase and uppercase :

In this example, I will show you how to convert all characters of a string to uppercase or lowercase. First we will take the input string from the user using ‘Scanner’ class and then convert it to upper and lower case using following two methods :

String.toUpperCase():

This built-in method that converts a string to upper case and returns the result string.

String.toLowerCase():

This is also a built-in method that converts a string to lower case and returns the result string.

Easy. isn’t it ? Let’s write some code :

import java.util.Locale;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a String ");
        String userInputString = scanner.nextLine();

        //print uppercase string
        System.out.println("Uppercase string "+ userInputString.toUpperCase());

        //print lowercase string
        System.out.println("Lowercase string "+userInputString.toLowerCase());
    }
}

Example :

Enter a String 
HellO WoRlD
Uppercase string HELLO WORLD
Lowercase string hello world

Handling with Local :

A ‘Locale’ object represents a specific geographical, political or cultural region. Any information that depends on Local is called locale-sensitive. We can create a new Locale object by using ‘Locale(String language, String country)’ constructor. We can also set default locale for that instance of the JVM using ‘setDefault(Locale locale)’ method. e.g. to set Turkish locale, we will have to use ‘Locale.setDefault(new Locale(“tr”,“TR”));‘.

Let’s try to run our program using this locale :

import java.util.Locale;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        Locale.setDefault(new Locale("tr","TR"));

        System.out.println("Enter a String ");
        String userInputString = scanner.nextLine();

        //print uppercase string
        System.out.println("Uppercase string "+ userInputString.toUpperCase());

        //print lowercase string
        System.out.println("Lowercase string "+userInputString.toLowerCase());
    }
}

Examples :

Enter a String 
title
Uppercase string TİTLE
Lowercase string title

Enter a String 
TITLE
Uppercase string TITLE
Lowercase string tıtle

Have you seen the difference both time ? So, we should always handle this for all locals. For this example, suppose we are working targeting ‘english’ locale. Before starting the program, we should always set the local by using ‘setDefault()’ method or pass it while calling ‘toUpperCase’ or ‘toLowerCase’ methods.(I have commented out the ‘setDefault()’ method below).

import java.util.Locale;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        //Locale.setDefault(Locale.ENGLISH);

        System.out.println("Enter a String ");
        String userInputString = scanner.nextLine();

        //print uppercase string
        System.out.println("Uppercase string "+ userInputString.toUpperCase(Locale.ENGLISH));

        //print lowercase string
        System.out.println("Lowercase string "+userInputString.toLowerCase(Locale.ENGLISH));
    }
}

If you love our articles, please share and subscribe to the blog :)

Similar tutorials :