import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; /** get a string from the user using the command line. @author Greg Vogl 2003-09-21 */ public class EchoStrings { /** get strings from the user using the command line.
input: a string using the command line
output: the string and its length; count of number of strings @throws IOException to handle bad strings */ public static void main (String[] args) throws IOException { System.out.println("Enter a string:"); // ask the user for input BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); String input; int i = 0; while ((input = console.readLine()) != null) { // get a string from the user System.out.println("You entered: " + input); // display the string to the screen System.out.println("String length: " + input.length()); System.out.println("Enter a string:"); // ask for another string i++; } System.out.println("You entered " + i + " strings."); } }