I need to use \"public static int[] intSort(int[] a)\" to sort the array. Also,
ID: 3635434 • Letter: I
Question
I need to use "public static int[] intSort(int[] a)" to sort the array. Also, a prompt must ask the user if they want to parse another string. It must add on to the partial code at the end of the description.A sample of what the output should be is as follows:
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): xfugih
I have this java code:
import java.util.Scanner;
public class intsort {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter a string: ");
int[] std = intParse(kb.nextLine());
for(int i : std)
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);
Arrays.sort(output);
return null;
}
Explanation / Answer
please rate - thanks
import java.util.Scanner;
import java.util.Arrays;
public class intsort {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
do
{
System.out.print("Please enter a string: ");
int[] std = intParse(kb.nextLine());
print(std);
System.out.print("Would you like to parse another string (y/n): ");
}while(Character.toUpperCase(kb.nextLine().charAt(0))=='Y');
}
public static int[] intParse(String s)
{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);
print(output);
Arrays.sort(output);
return output;
}
public static void print(int std[])
{for(int i : std)
System.out.println(i);
System.out.println();
}