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

12.2 Fill in the missing code. String in- \"9tu104u-113\ 8ut4-u5\ \"; Scanner s-

ID: 3738598 • Letter: 1

Question

12.2 Fill in the missing code. String in- "9tu104u-113 8ut4-u5 "; Scanner s-new Scanner(in); while (s.hasNextLine()) f String pattern - --; íf (s . findInLine (pattern) != null) { MatchResult mr -S.match (); int a = Integer.parseint ( int b Integer.parseInt( mr . group(1) mr . group (2) mr . group (3) ): int c = Integer . par seint ( ); if (a + b --c) System. out. println ("Correct"); else System.out.println (" Incorrect"); 1 s.nextLine ); // output Correct Incorrect 12.3 Fill in the missing code. String in - "Methuselah ,uage :938 Wilma, age :u45 "; Scanner s = new Scanner (in); while(s.hasNextLine()) [ String pattern - -- if (s.findInLine (pattern) !- null) f MatchResult mr - s .match ); int a - Integer .parseInt (---); String b . mr . group (2) mr. group (1) System . out , printf ( "Name : uZs , uage : u%d%n" , b , a) ; > s.nextLine ); // output Name: Methuselah, age: 938 Name: Wilma, age: 45

Explanation / Answer

12.2 The explanation of the code is given as coments to each line .

string in = "9+ 104 =113 8 +4= 5 "; // Initialize the string
Scanner s = new scanner (in);
while(s.hasNextline()) { // Tokenize and validate the index line.
string pattern = "(\d+)\s*\+\s*(\d+)\s*=\s*(\d+)"; //Regex pattern to match the string
if(s.FindInLine(pattern)!=null){ // method used to return a String object which satisfies the Pattern object specified as method argument.
MatchResult mr = s.match(); // method to match the regex and input
int a = Integer.ParseInt(mr.group(1)); // initailize a for first word
int b = Integer.ParseInt(mr.group(2)); // initailize b for second word
int c = Integer.ParseInt(mr.group(3)); // initailize c for third word
if (a+b==c) System.out.println("Correct"); // print correct if calculation is right
else System.out.println("InCorrect"); // print incorrect if calculation is wrong
}
s.nextLine();
}

The Regex \d+ matches one or more occurences of digits and \s matches the space like white space or , The operators + and = are matched by using \+ and \= .

In this code the string initialized as in is matched with the regex pattern , According to the pattern given the string is , 9+104 =113 , since the calculation is right "Correct " is printed and the next line is matched which is 8 + 4 =5 , since the calculation is wrong "Incorrect" is printed.

12.3

string in = "Methuselah, age:938 Wilma,age: 45 "; // Initialize the string
Scanner s = new scanner (in);
while(s.hasNextline()) { // Tokenize and validate the index line.
string pattern = "(\w+)\s*,\s*age:\s*(\d+)";
if(s.FindInLine(pattern)!=null){ // method used to return a String object which satisfies the Pattern object specified as method argument.
MatchResult mr = s.match(); // method to match the regex and input
int a = Integer.ParseInt(mr.group(2)); // initailize a for second word that is age
String b = Integer.ParseInt(mr.group(1)); // initailize string b for first word that is Name
System.out.println("Name: %s, age: %d ",b,a); // print Name and age
s.nextLine();
}

The Regex \w+ matches one or word characters and \s matches the space like white space or .

In the program The Name of the person and age is matched with pattern given , each of the name and age are given two lines separated by in the input string in, the output as given below .

Name: Methuselah, age: 938

Name: Wilma, age: 45

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