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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact