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

Please explain the pattern and why we used \"Integer . parseInt ( mr . group (3)

ID: 3700096 • Letter: P

Question

Please explain the pattern and why we used "Integer . parseInt ( mr . group (3) ) " in 12.5 and when to use it and when not.

12.4 Fill in the missing code String in - "Deadpoolu891-22-3954 "+ "Wadeu113-24-5566 "; Scanner s - new Scanner (in); while (s.hasNextLine)) String pattern = if(s.find InLine (pattern) . != nu11) { MatchResult mr - s.match (); int a = Integer.parseInt( int b = Integer.parseInt( int c = Integer.parseInt( String d = . mr.group (2) mr.group (3) mr.group (4) ) ); ): mr.group (1) System . out. print f ("Name : u%s , ussn :u%d-%d-%d%n" , d, a, b, c) ; J s.nextLine (); // output: Name: Deadpool, ssn: 891-22-3954 Name: Wade, ssn: 113-24-5566 12.5 Fill in the missing code String in = "MoDuJan, 2,?1982 WeduAuEu14,u1979 "; Scanner s new Scanner (in); while (s.hasNextLine)) String pattern - -- if (s.findInLine (pattern) !- null) [ MatchResult mr - s.match ) String weekday . String month = . int day- mr . group (1) mr.group (2) Integer.parseInt (mr.group (3)) Integer .parseInt (mr . group (4)) nt year - System . out . printf "Date :uZsu%d/Xs/%d%n", weekday, day , month , year) ; else t System. out.println ("Incorrect") j s.nextLine O; // output: Date: Mon 2/Jan/1982 Date: Wed 14/Aug/1979

Explanation / Answer

12.4: In pattern "(\w+)\s+(\d+)-(\d+)-(\d+)",
Each set of open and close brackets represent a group.
1st group is (\w+)=Deadpool
2nd group is (\d+)=891
3rd group is (\d+)=22
4th group is (\d+)=3954
The numbers to the groups are assigned from 1st to last.

Now the 1st group represents a group of letters.
The first group is followed by \s+, which represents a group of spaces.
remaining all groups represent a group of digits.

12.5 : In pattern "(\w+)\s+(\w+)\s+(\d+),\s*(\d+)"
Each set of open and close brackets represent a group.
1st group is (\w+)=Mon
2nd group is (\w+)=Jan
3rd group is (\d+)=2
4th group is (\d+)=1982
The numbers to the groups are assigned from 1st to last.

Now the 1st and 2nd groups each represent a group of letters.
The 1st and 2nd groups each followed by \s+, which represents a group of spaces.
remaining all groups represent a group of digits.

3rd group is followed a single comma and \s+, which represents a group of spaces.

Explanation of why we used "Integer.parseInt ( mr . group (3) )":

group 3 is a group of digits in text format.

we want this group of digits assigned to "int day".

But group 3 is a text and "day" is an integer.

So, before assigning group 3 to "day", we have to convert this group 3 to integer.

Hence, we use "Integer.parseInt()" function to convert group 3 to integer.

Difference between s+ and s*:

+ means 1 or more

* means 0 or more

s+ means 1 or more spaces or tabs.

s* means 0 or more spaces or tabs.