Question:
Write a program that takes keyboard input, uses scanner delimiter “," and use a while loop to check for hasNext() and print out resulting parsed input.
Example:
Input: Now, is, a good time, to get this done
Output:
Now
is
a good time
to get this done
Example:
Input: Now, is, a good time, to get this done
Output:
Now
is
a good time
to get this done
Code:
import java.util.*; public class ScannerExample { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("What is your sentence: "); String str = keyboard.nextLine(); Scanner s = new Scanner(str); s.useDelimiter(","); while(s.hasNext()){ System.out.println(s.next()); } } }
Output:
$ java ScannerExample What is your sentence: Now, is, a good time, to get this done Now is a good time to get this done