/* * Important! You must import the util and io libraries for this to work! */ import java.util.StringTokenizer; import java.io.*; /* * This example program illustrates how to use a BufferedReader to read * lines repeatedly from standard input and how to use a StringTokenizer to * read words from a string. It also illustrates the use of exception * handling by try-catch to handle the potential I/O errors. */ public class DemoInteract { public static void main(String[] args) { try // We need try-catch because of potential problems with I/O. { /* * We create a BufferedReader from standard input. */ BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); /* The following while loop reads lines from standard input. */ boolean moreInput = true; String oneLine; while (moreInput) { System.out.print("Please input your text. >>"); oneLine = inputReader.readLine(); // Read a line from standard input. if (oneLine.equals("quit")) { moreInput = false; } else { StringTokenizer wordGetter = new StringTokenizer(oneLine); /* * The following while loop reads individual words from a string * and echos them to standard output on separate lines. */ while (wordGetter.hasMoreTokens()) { String aWord = wordGetter.nextToken(); System.out.println(aWord); } System.out.println(); } } /* Now we close the input stream. */ inputReader.close(); } catch(IOException anError) { anError.printStackTrace(); // Show the function call stack for debugging. } } }