/* A StringTokenizer example: given a list of elements, return the first element and the list of all but the first element (i.e. the rest of the list). */ import java.util.Scanner; import java.util.StringTokenizer; class First_Rest2 { public static void main (String[] args) { Scanner scan = new Scanner(System.in); StringTokenizer list; String new_list = ""; String next_word; System.out.print ("Enter a list of words: "); System.out.flush(); list = new StringTokenizer (scan.nextLine(), " ", true); /* now spaces are treated as tokens too */ System.out.println ("The first element of the list is: " + list.nextToken()); next_word = list.nextToken(); // ignore the space between first and rest while (list.hasMoreTokens()) { next_word = list.nextToken(); new_list = new_list.concat(next_word); } System.out.println ("The rest of the list is: " + new_list); } }