How to Capitalize a String in Java

Problem:

Write a program that would read a string of length 4 (small letters) from the user then capitalize it and print each character on a separate line. For example, if the user input is java, the output should look as follows:

Output:

Please enter a word of length 4: java

J
A
V
A

Solution:

/**
 * @(#)Problem1.java
 *
 *
 * @author George Chalhoub
 * @version 1.00 2012/10/23
 */



   import java.util.Scanner;

public class Problem1 {
     public static void main(String[] args) {
         Scanner input = new Scanner(System.in);
         System.out.println("Please enter a word of length 4:");
         String word = input.next();
         String wordUpper = word.toUpperCase();
           
         char char1 = wordUpper.charAt(0);
         char char2 = wordUpper.charAt(1);
         char char3 = wordUpper.charAt(2);
         char char4 = wordUpper.charAt(3);
           
         System.out.println(char1);
         System.out.println(char2);
     System.out.println(char3);
      System.out.println(char4); 
     }
}

    

No comments:

Post a Comment