// Project: InputOutput // No modules defined. // Source code file: TestScanner.java // Test a Scanner object that reads from a string. // Also use the String split method to extract the // words from the same string. import java.util.Scanner; public class TestScanner { public static void main(String[] args) { // Define Scanner object s that reads from // String "This is a test." Scanner s = new Scanner("This is a test."); s.useDelimiter(" "); while (s.hasNext( )) { String word = s.next( ); System.out.println(word); } s.close( ); System.out.println( ); String t = "This is a test."; // The String split method extracts space // delimited fields from a string. String[ ] fields = t.split(" "); // Print the extracted fields. for(String word : fields) { System.out.println(word); } } }