switchData
Category: Token-Based File Processing
Author: Stuart Reges
Book Chapter: 6.2
Problem: switchData
Write a static method called switchData
that takes as a parameter a Scanner containing a label followed
by a sequence of integers and that prints to System.out the same information
with each successive pair of integers switched in order. For example,
suppose that a Scanner called data contains the following tokens:
Jan 1 2 3 4 5 6
Here the label is "Jan". The label will always be a single word that
appears at the beginning. After the label, we have a series of six
integers. If we make the following call:
switchData(data);
the method should produce the following output:
Jan 2 1 4 3 6 5
Notice that the first pair of integers (1, 2) has been switched (2, 1), and
the second pair of integers (3, 4) has been switched (4, 3), and so on.
This first example involved sequential integers to make the switching more
obvious, but this won't always be the case. You also shouldn't assume that
you have an even number of integers. If there is an odd number of integers,
then the final value should not be moved. For example, if the Scanner had
instead contained these tokens:
Feb 38 14 79 4 -3
then the method would have produced the following output:
Feb 14 38 4 79 -3
There will always be a one-word label, but the list of integers might be
empty, in which case the method simply prints the label on a line by itself.
Your method should produce a complete line of output. In other words, if it
is called n times, it will produce n lines of output. You may assume that
the input is legal (a one-word label followed by 0 or more integer values).