Question: create a new Java application called Parser (without the quotation marks) that uses methods to: Get a comma-delimited String of integers (e.g. 4, 8, 16,

create a new Java application called "Parser" (without the quotation marks) that uses methods to:

Get a comma-delimited String of integers (e.g. "4, 8, 16, 32") from the user at the command line and then convert the String to an ArrayList of integers (using the wrapper class) with each element containing one of the input integers in sequence

Print the integers to the command line, using a for loop, so that each integer is on a separate line

Note

Use an ArrayList where each element is an integer. Declare the ArrayList in main.

The use of two methods is required.

The first method prompts the user for a list of integers separated by commas. Read this into a variable of type string. There are several approaches to extracting the numeric values from the string. We have done this before.

One approach is to pre-process the string replacing the commas with blank spaces and then reusing the while loop code for hasNextX (the X can be Double or Integer).

Look up the replaceAll method for replacing the commas. (I did not have to use a Wrapper method with this approach).

Using this approach, the integer extraction loop looks similar to this:

Scanner listOfNums = new Scanner(strOfNums);

// prompt for string of integers separated by commas

// read in string

// replace , with blank spaces

// set up local scanner on the string in this case called listOfNums

while (listOfNums.hasNextInt()) {

curInt = listOfNums.nextInt();

intArray.add(i,curInt);

i++;

}

Notice the storing of the integer values into ArrayList intArray using the add method.

The second method just prints out the integer values of the elements in the ArrayList.

Example run testing for extra spaces and commas, as well as, the case where the user only enters one value:

run:

Enter list of integers separated by commas:

5, 6, 45,56,67 , 0, 1,

Output:

5

6

45

56

67

0

1

run:

Enter list of integers separated by commas:

6

Output:

6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!