3.4k views
0 votes
3) Create a Java program that asks the user to enter his/her

favorite city. Use a String variable to store the input.
The program should then display the following:
-The number of characters in the city name
-The name of the city in uppercase letters
-The name of the city in lowercase letters
-The first character in the city name

1 Answer

2 votes

Here is an example of a Java program that meets the requirements:

import java.util.Scanner;

public class FavoriteCity {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String city;

System.out.print("Enter your favorite city: ");

city = input.nextLine();

System.out.println("Number of characters in the city name: " + city.length());

System.out.println("City name in uppercase letters: " + city.toUpperCase());

System.out.println("City name in lowercase letters: " + city.toLowerCase());

System.out.println("First character in the city name: " + city.charAt(0));

}

}

In this example, the Scanner class is used to retrieve input from the user. The user is prompted to enter their favorite city, and the input is stored in a String variable named city. The length of the city name is obtained using the length() method, the city name in uppercase letters is obtained using the toUpperCase() method, the city name in lowercase letters is obtained using the toLowerCase() method, and the first character in the city name is obtained using the charAt(0) method. The results are displayed to the user using println statements.

User Ravi Dhorajiya
by
7.1k points