Java Programming lab

  • Print Article |
  • Send to a Friend |
  • |
  • Add to Google |

Write a progarm that asks the user to enter the name of his or her favorite city. Use a String variable to store the input. The program shoud display the following:

-the number of the character in teh city name

-the name of the city in all uppercase letters

-the name of the city in all lowercase letters

-the first character in the name of the city

The java programming methos to solve this problem is

// This program demonstrates the usage of the methods of the String class.

public class StringManipulator
{
    public static void main(String[] args)
    {
         String name = "Atlanta";
         int stringSize;

         stringSize = name.length();
         System.out.println(name + " has " + stringSize +
                                        " characters.");

         String message = "Atlanta";
         String upper = message.toUpperCase();
         String lower = message.toLowerCase();
         char letter = message.charAt(0);

           System.out.println(" My favorite city is " + message +
                                           ".");
           System.out.println(" My favorite city is " + upper +
                                              ".");
           System.out.println(" My favorite city is " + lower +
                                              ".");
           System.out.println(" The first character is " + letter +
                                                ".");
      }
 }

 

Although this does not use the Scanner method, when you do use it, at the top of the program you have to inlcude the statement: import java.util.Scanner, this will tell the java program that the Scanner class is going to be used. You could also use message boxes to for the program. The whole program that is written here is will be the same way but with just the user asking for a city name. This program however gives the name of the city and does not ask the user.

Rate this Article:
  • Article Word Count: 283
  • |
  • Total Views: 772
  • |
  • permalink
  • Print Article |
  • Send to a Friend |
  • |
  • Add to Google |
>