This lab is another example of the use of the split
method of the String class, and
illustrates how we can process lines of information that contain more than one value per line.
split
method to break each line of input into separate values.for
loop. Let's say we
have some data that describes the makeup of the Cleveland Orchestra for the current season.
Each line of input will contain three values for a different type of instrument in the
orchestra: the name of the instrument, the orchestra section to which the instrument
belongs, and the number of musicians that play that instrument this season. Each of
these values is separated from each other by a semicolon.
for
loop that displays each line in the output area:
String[] part = lineOfInput[index].split(";");Now we have an array of
String
s called part
, where element 0
of the array contains the instrument name, element 1 contains the instrument's section
and element 2 contains the number of musicians for this instrument.
Instrument
class with private data members for each of the three
pieces of data described above. Look at the code in this file and make sure
you understand how it is written. for
loop in the actionPerformed method
for the processInput button, instantiate
the array of Instruments so that there are as many elements in this array as there are
lines in the input.
for
loop in the actionPerformed method for the processInput button,
do the following:
part
,
using a semicolon as the delimiter (see the code example above) for (Integer p = 0; p < part.length; p++) {
part[p] = part[p].trim();
} // end for
index
of the array of Instrument
objects, using the part
array as the values to pass to the constructor method part[0]
part[1]
Integer.parseInt( part[2] )
append
call inside the loop to be this:
outputArea.append( "line " + index + ": " + instrumentArray[index].toString() + "\n" );(you may need to change the name
instrumentArray
in this
sample to be the name you used in your program)The program should print information for 26 different instruments from this test data.