// THe program gets input from the user and prints it out 
// alternating uppercase and lowercase characters

// the program does not have if/else statement inside the loop

// need this import for reading data:
import java.io.*;

public class Alternate {

    public static void main (String [] args)  throws IOException {
	// Create a buffered reader
	// need only one buffered reader for all your reading
	BufferedReader in = new BufferedReader(
			new InputStreamReader(System.in));
	// string for storing a line of input
	String line;

	// Read one integer
	// prompt the user:
        System.out.print("Please enter a string: ");
	// read a line of input:
	line = in.readLine();
	String upper = line.toUpperCase();
	String lower = line.toLowerCase();

	int len = line.length();
	for (int i = 0; i < len - 1; i = i + 2) {
	    //System.out.print(upper.charAt(i));
	    System.out.print(Character.toUpperCase(upper.charAt(i)));
	    System.out.print(lower.charAt(i + 1));
	}
	if (len%2 == 1) {
	    System.out.print(upper.charAt(len - 1));
	}
    }
}

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.