/* * ================================================================= * DemoStrings.java: Demo features of Strings in Java * * String is a class for working with arrays of multiple characters. * * Written By: Mark Austin October 2003 * ================================================================= */ import java.lang.Math; import java.text.*; public class DemoStrings { // Constructor method. public DemoStrings() {}; // main method : this is where the program execution begins. public static void main ( String [] args ) { int i; // [a] Setup and print two strings ... String pet1 = "Fred"; String pet2 = "Ginger"; System.out.println(""); System.out.println("My cats are " + pet1 + " and " + pet2 ); // [b] Let's concetenate and then print the strings String myPets = pet1.concat(" and ").concat( pet2 ); System.out.println("My pets are " + myPets ); // [c] Test for equality of strings .... if ( pet1.equals(pet2) == true ) System.out.println("My pets have the same name" ); else System.out.println("My pets have different names" ); // [d] Let's count the characters in each pet name .... System.out.println(""); System.out.println("\"Fred\" has " + pet1.length() + " characters" ); System.out.println("\"" + pet2 + "\"" + " has " + pet2.length() + " characters" ); // [e] Define array of character strings -- this is how I // remembered how to spell "arithmetic" at the age of 5-6. String [] saArithmetic = { "A", "Red", "Indian", "Thought", "He", "Might", "Eat", "Toffee", "In", "Church" }; System.out.println(""); System.out.println("Array: saArithmetic"); for ( i = 0; i < saArithmetic.length; i = i + 1) System.out.println( " " + saArithmetic [i] ); System.out.println(""); System.out.println("Length of saArithmetic = " + saArithmetic.length ); // [f] Let's compute some simple operations on strings.... for ( i = 0; i < saArithmetic.length; i = i + 1) System.out.println(" saArithmetic[" + i + "] has " + saArithmetic[i].length() + " characters" ); // [g] Let's convert the saArithmetic to uppercase and lowercase.... System.out.println(""); System.out.println("Convert Strings to Uppercase and Lowercase" ); for ( i = 0; i < saArithmetic.length; i = i + 1) System.out.println(" saArithmetic[" + i + "] is: " + saArithmetic[i].toUpperCase() + " and " + saArithmetic[i].toLowerCase() ); // [h] Convert a number to a string int iA = 34; float fA = 23.34F; String sA = Integer.toString( iA ); String sB = Float.toString( fA ); System.out.println(""); System.out.println("Number to string: sA = " + sA ); System.out.println("Number to string: fA = " + sB ); // [i] Convert a string to a number .... String myInt = "31415926"; String myPi = "3.1415926"; int imyPi = Integer.valueOf( myInt ).intValue(); float fmyPi = Float.valueOf( myPi ).floatValue(); double dmyPi = Double.valueOf( myPi ).doubleValue(); System.out.println(""); System.out.println("String to Number: int myInt = " + imyPi ); System.out.println("String to Number: float myPi = " + fmyPi ); System.out.println("String to Number: double myPi = " + dmyPi ); } }