Topic

String Tokenizer
Primitive Arrays

Description

Enhance the last assignment by providing the following additional features:

Input All Data As A Single String

Input all data values as a single input string using one input dialog box. Then use a StringTokenizer object to tokenize (separate into components) the data. For this purpose, do the following:

  • Input all data as a single String using JOptionPane.showInputDialog.
  • Create a StringTokenizer object and supply it with the input string and a token delimiter (separator) during object construction.
  • Find the token count by calling StringTokernizer's method countTokens ( ).
  • Create an array of size count to store data values.
  • Write a Java For statement to loop token count times. During each pass through the loop, obtain the next token by calling StringTokenizer's nextToken( ) method. Convert the token received as a String to a double and save it in the corresponding array element.

Output All Data To Three Decimal Places

You can display output data to desired number of decimal places. For this purpose do the following:

  • Create a DecimalFormat object and supply it with the appropriate Format String to format decimal numbers to required decimal places.
  • Use the DecimalFormat object to format any decimal numbers into a formatted strings.
  • Display the formatted String.

Testing:

Input Test Run

Test the above program for the input data values below. (User input is shown in bold).

Enter Data < separated by commas/spaces>:
7.2,7.6, 5.1,4.2,2.8,0.9,0.8,0.0,0.4,1.6,3.2,6.4
Enter the Number of Decimal Places to which the Computed Values are displayed : 3

Output Test Run

Original Data: 7.2 7.6 5.1 4.2 2.8 0.9 0.8 0.0 0.4 1.6 3.2 6.4
Sorted Data: 0.0 0.4 0.8 0.9 1.6 2.8 3.2 4.2 5.1 6.4 7.2 7.6

Computed Values Displayed To 3 Decimal Places:
Min Value: 0.000
Max Value: 7.600
Mean: 3.350
Median: 3.000
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.