Copy/paste this code into a file StringTest.java:

import java.awt.*;
import java.io.*;

// the program demonstrates various methods 
// of class String
// Unlike StringBuffer, String value doesn't change in
// memory. String  methods return a new string which can be assigned 
// to the same variable or to a different one. 

public class StringTest {

    public static void main (String [] args) throws IOException{
	// set up the program for reading data
	// Create a buffered reader
	BufferedReader in = new BufferedReader(
			new InputStreamReader(System.in));
	// create a string variable (it doesn't refer to any object)
        String input;
	
	System.out.print("What's your name? ");
	input = in.readLine();
	// format the name: first letter capital, the rest lower case:

	// create the string with just the first letter:
	String first = input.substring(0,1);
	// create another string with the first letter in upper case:
	first = first.toUpperCase();

	// create the string with the rest:
	String therest = input.substring(1);
	// create a string with the lower-case version 
	// of the rest of the name:
	therest = therest.toLowerCase();

	String result = "Nice to meet you, " + first + therest;
	System.out.println(result);
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.