Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write a Java program that prompts the user for a string, extracts the i

ID: 3635224 • Letter: Y

Question

You will write a Java program that prompts the user for a string, extracts the integers from it, then displays the list of integers one line at a time: first in order of input, then sorted in ascending order.

The program should first prompt the user for a single string of integers. The program must read the entire line of input. All integers are assumed to be positive, so the integers within the string will each be a sequence of consecutive digits. Integers are separated by any non-digits characters. For example, the string "aa123 4p56-7890" contains the integers 123, 4, 56, and 7890.

Next the program should display each integer on its own line in the order they were entered. For example:

123
4
56
7890

Then the program should display the same list, sorted in ascending order:

4
56
123
7890

Finally, the program should prompt the user, asking if he or she wants to parse another string. If the user response begins with an uppercase or lowercase 'y' the program starts over from the beginning, otherwise the program ends.

You must write and use a method intParse to break the string into a list of integers. Its heading is

public static int[] intParse(String s)

and it should return an array containing the integers extracted from the input string s. The method intParse should not have any side-effects. In particular, it should not print anything. The main method should use the array of integers returned by intParse to print the list of integers.

You must write and use a method intSort to sort the array of integers. Its heading is

public static int[] intSort(int[] a)

and it should return a new array containing the contents of a, sorted in ascending order. The original array, a, should not be modified by intSort.


Sample run

Integer Parsing Program

Please enter a string: 2954tb94762234 b9
2954
94762234
9

9
2954
94762234
Would you like to parse another string (y/n): y

Please enter a string: 10*100=1,000
10
100
1
0

0
1
10
100
Would you like to parse another string (y/n): xsgrd

Explanation / Answer

public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter a string: ");
int[] temp = intParse(kb.nextLine());
for(int i : temp)
System.out.println(i);
}
public static int[] intParse(String s)
{
// split the string on all non-digit characters
String[] split = s.split("[\D]+");
// count numbers
int count = 0;
for(String x : split)
if(x.length() > 0)
count++;
// create output
int[] output = new int[count];
int index = 0;
for(String x : split)
if(x.length() > 0)
output[index++] = Integer.parseInt(x);
return output;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote