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
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. Here are some examples of the format that will be used for this data:
French Horn; Brass; 3 Cornet; Brass; 5 Timpani; Percussion; 2This example means that there are 3 French Horn players in the Brass section of the orchestra, 5 Cornet players in the Brass section, and 2 Timpanis (Kettle Drums) in the Percussion section. Our goal in this program is to use this text input data to create an array of Instrument objects that we can process.
myString
into an array of Strings, using a semicolon as the delimiter, we can use the following code:
String[] part = myString.split(";");Then we would have an array of
String
s called part
, and if myString
contained one of the sample lines above, 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. instArray
. Then,
immediately before the for
loop in the actionPerformed method
for the processInput button, instantiate this
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,
replace the outputArea.append
statement that is in the loop with the following code:
instArray[index] = makeANewInstrument( lineOfInput[index] ); outputArea.append( "[" + index + "]: " + instArray[index].toString() + "\n" );When you do this, NetBeans will give you an error, saying that
makeANewInstrument
is not defined. We need to write a method with this name in the program, and we will do that in the next step of these instructions.makeANewInstrument
. This method accepts one String
parameter, and will return an Instrument
object as its result. Your task in this step of the lab will be to write code to take a line of input, split it into pieces using the delimiter character, and then create a new Instrument
object to be sent back as the return value for the method.part
,
using a semicolon as the delimiter. See the code example in instruction B. 1) above. for (Integer p = 0; p < part.length; p++) {
part[p] = part[p].trim();
} // end for
answer
, using the part
array as the values to pass to the constructor method part[0]
part[1]
Integer.parseInt( part[2] )
The program should print information for 26 different instruments from this test data.