// StringToUpperCase.java (c) 2004 Kari Laitinen // http://www.naturalprogramming.com // 2005-04-10 File created. // 2005-04-10 Last modification. // A solutions to Exercise 8-4. import java.util.* ; class StringToUpperCase { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n Please, type in a string. \n\n " ) ; String given_string = keyboard.nextLine() ; System.out.print( "\n The given String in uppercase form: \n\n " ) ; for ( int character_index = 0 ; character_index < given_string.length() ; character_index ++ ) { if ( given_string.charAt( character_index ) >= 'a' && given_string.charAt( character_index ) <= 'z' ) { // The result of a subtraction operation is of type int. // Therefore, the conversion (char) is used below to convert // the numerical value to a value of type char System.out.print( (char) (given_string.charAt( character_index ) - 0x20) ) ; } else { System.out.print( given_string.charAt( character_index ) ) ; } } // An easier way to solve this problem is to use the // ToUpper() String method in the following way. System.out.print( "\n\n " + given_string.toUpperCase() ) ; } }