import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application intended to illustrate the use of the ** CSV_String class. */ public class CSV_String_ClientR { public static void main(String[] args) throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); String s = getString("Enter a string:>",keyboard); CSV_String X = new CSV_String(s); int numFields = X.numberOfFields(); System.out.println("The CSV string is " + quoted(X.toString())); System.out.println("It has " + numFields + " fields.\n"); // For each position from -1 to the number of fields, see what // the elementAt() method returns. for (int p = -1; p <= numFields; p++) { System.out.printf("The field at position %d is ", p); String fieldVal = X.elementAt(p); if (fieldVal == null) { System.out.println("null"); } else { System.out.println(quoted(fieldVal)); } } // Now use the "iterator" methods to iterate over the fields // in X, printing each one's value. System.out.println("\nStarting an iteration..."); X.reset(); while (X.hasNext()) { System.out.println("The next field is " + quoted(X.next())); } System.out.println("\nDone!"); } /* Prints the given prompt and then reads and returns the full line of ** input (as a string) entered by the interactive user. */ public static String getString(String prompt, Scanner input) { System.out.print(prompt); return input.nextLine(); } public static String quoted(String s) { String result = s; if(s != null) { result = "\"" + s + "\""; } return result; } }