In this assignment you will write a Java program that prompts the user for a str
ID: 3534055 • Letter: I
Question
In this assignment 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. It's very similar to an earlier assignment, but you are required to use methods and arrays. Functional requirements 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. For example: 123 4 56 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. Non-Functional requirements 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. Grading rubric Using the standard grading rubric at http://www.cs.uwm.edu/~cs201/semester/assignment/rubric.html, your program will be scored as follows: Performance Indicator [1] [2] [3] Non-Functional requirements 1 2 2 Readability & documentation 1 2 2 Functional requirements 2 3 4 Runtime efficiency/ Succinct code 1 2 2 Sample run Integer Parsing Program *********************** Please enter a string: 2954tb94762234 b9 2954 94762234 9 Would you like to parse another string (y/n): y Please enter a string: 10*100=1,000 10 100 1 0 Would you like to parse another string (y/n): xfugih miller:Explanation / Answer
100 % working code. The way your problem defintion want it to be.
import java.util.Scanner;
public class ExtractMe {
public int intParse(String s)
{
int i= 0;
String[] numbers = s.split("[^\d]+");
for (String number : numbers) {
if (!number.isEmpty()) {
i = Integer.parseInt(number);
System.out.println( i);
}
}
return i;
}
public static void main(String[] args) {
System.out.println("Enter a String");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
System.out.println("Integer values are");
ExtractMe extract = new ExtractMe();
extract.intParse(input);
char letter;
while(true)
{
System.out.println(" Press Y if you want to compute for another string.");
System.out.println("Press N to exit the program");
letter = scanner.next().charAt(0);;
if(letter=='N' || letter == 'n')
System.exit(1);
switch(Character.toUpperCase(letter))
{
case 'Y':
System.out.println("Enter a String");
String input2 = scanner.next();
System.out.println("Integer values are");
ExtractMe extract2 = new ExtractMe();
extract2.intParse(input2);
break;
default :
System.out.println("Kindly press Either Y or N");
}
}
}
}import java.util.Scanner;
public class ExtractMe {
public int intParse(String s)
{
int i= 0;
String[] numbers = s.split("[^\d]+");
for (String number : numbers) {
if (!number.isEmpty()) {
i = Integer.parseInt(number);
System.out.println( i);
}
}
return i;
}
public static void main(String[] args) {
System.out.println("Enter a String");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
System.out.println("Integer values are");
ExtractMe extract = new ExtractMe();
extract.intParse(input);
char letter;
while(true)
{
System.out.println(" Press Y if you want to compute for another string.");
System.out.println("Press N to exit the program");
letter = scanner.next().charAt(0);;
if(letter=='N' || letter == 'n')
System.exit(1);
switch(Character.toUpperCase(letter))
{
case 'Y':
System.out.println("Enter a String");
String input2 = scanner.next();
System.out.println("Integer values are");
ExtractMe extract2 = new ExtractMe();
extract2.intParse(input2);
break;
default :
System.out.println("Kindly press Either Y or N");
}
}
}
}