1 /**
 2 *
 3 *  This code was written to see test using different delimiters of the user's
 4 *  choice as in the  .useDelimiter(     ) method of Scanner. Try different single 
 5 *  value character strings when you run it.
 6 *
 7 *  @author Elizabeth Adams
 8 * 
 9 *
10 */
11        // import needed packages/classes
12 ÏÏÏimport java.util.Scanner;
13 ÏÏÏimport java.io.*;
14 
15 
16 ÏÕÖ×public class MultipleDelimiterAttempt
17 ÏÏ§{
18 ÏÏ§ÏÞßàpublic static void main (String  [] args)
19 ÏÏ§ÏÏ§{
20 ÏÏ§ÏÏ§
21 ÏÏ§ÏÏ¨¹íÏScanner fileScanner, lineScanner, keyboardScanner;
22 ÏÏ§ÏÏ¨¹íÏString fileName;
23 ÏÏ§ÏÏ¨¹íÏString temp;
24 ÏÏ§ÏÏ¨¹íÏString myDelimiter;
25 ÏÏ§ÏÏ§
26 ÏÏ§ÏÏ§
27 ÏÏ§ÏÏ¨¹¹ÏfileName = "delimiters.txt"; // hard coding filename for test example
28 ÏÏ§ÏÏ§  
29 ÏÏ§ÏÏ¨¹¹´try   // to instantiate Scanner for file
30 ÏÏ§ÏÏ§ÏÏ§{  
31 ÏÏ§ÏÏ§ÏÏ¨¹¹ÏfileScanner = new Scanner (new File (fileName)); // works if file present
32 ÏÏ§ÏÏ§ÏÏ§}
33 ÏÏ§ÏÏ§ÏÏ§Ïðîìcatch (FileNotFoundException fnfe)// if file not present
34 ÏÏ§ÏÏ§ÏÏ§ÏÏ§{
35 ÏÏ§ÏÏ§ÏÏ§ÏÏ¨¹¹ÏfileScanner = new Scanner ("je#bs&dgu%asasabd&n$k $7");  // will scan this string
36 ÏÏ§ÏÏ§ÏÏ§ÏÏ¨¹¹ÏSystem.out.println (fnfe.getMessage());  // prints error
37 ÏÏ§ÏÏ§ÏÏ§ÏÏ¨¹¹Ïfnfe.printStackTrace();  // prints trace of calling stack
38 ÏÏ§ÏÏ§ÏÏ©ÏÏ©}     
39 ÏÏ§ÏÏ§
40 ÏÏ§ÏÏ¨¹¹Ïtemp = fileScanner.nextLine(); // gets single line from file or String above
41 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println (" Picked up line containing " + temp);  // echoes it
42 ÏÏ§ÏÏ¨¹¹ÏlineScanner = new Scanner (temp);  // sets Scanner to scan String
43 ÏÏ§ÏÏ§
44 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println 
45 ÏÏ§ÏÏ§ÏÏÏÏÏÏ(" Please enter the character you would like to use as a delimiter ");
46 ÏÏ§ÏÏ¨¹¹ÏkeyboardScanner = new Scanner (System.in);  //sets scanner to Keyboard
47 ÏÏ§ÏÏ¨¹¹ÏmyDelimiter = keyboardScanner.next();  // gets user's favorite delimiter
48 ÏÏ§ÏÏ¨¹¹ÏSystem.out.println( "*" + myDelimiter + "*");// echoes delimiter with stars
49 ÏÏ§ÏÏ§
50 ÏÏ§ÏÏ§           // tells line scanner to use user's delimiter
51 ÏÏ§ÏÏ¨¹¹ÏlineScanner.useDelimiter (myDelimiter);  
52 ÏÏ§ÏÏ§
53 ÏÏ§ÏÏ¨¹¹±while (lineScanner.hasNext()) // if there's something left in String
54 ÏÏ§ÏÏ§ÏÏ5{
55 ÏÏ§ÏÏ§ÏÏ7¹¹Ïtemp = lineScanner.next();  // get a token from the String
56 ÏÏ§ÏÏ§ÏÏ7¹¹ÏSystem.out.println ('*' + temp + '*');  // echo it
57 ÏÏ§ÏÏ§ÏÏ°}
58 ÏÏ§ÏÏ©} // end main
59 ÏÏ©} // end class