Repository navigation
Expand file tree
/
Copy pathDay28Regex.java
More file actions
78 lines (66 loc) · 2.34 KB
/
Copy pathDay28Regex.java
File metadata and controls
78 lines (66 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package startingJava;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day28Regex {
private static final Scanner scanner = new Scanner(System.in);
public static void practiceRegex() {
// This will match a sequence of 1 or more uppercase and lowercase English letters as well as spaces
String myRegExString = "[a-zA-Z\\s]+";
// This is the string we will check to see if our regex matches:
String myString = "The quick brown fox jumped over the lazy dog...";
// Create a Pattern object (compiled RegEx) and save it as 'p'
Pattern p = Pattern.compile(myRegExString);
// We need a Matcher to match our compiled RegEx to a String
Matcher m = p.matcher(myString);
// if our Matcher finds a match
if( m.find() ) {
// Print the match
System.out.println( m.group() );
}
}
public static void main(String[] args) {
int N = scanner.nextInt();
Map<String, String> people = new HashMap<String, String>();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int NItr = 0; NItr < N; NItr++) {
String[] firstNameEmailID = scanner.nextLine().split(" ");
String firstName = firstNameEmailID[0];
String emailID = firstNameEmailID[1];
if (isGmailAccount(emailID)){
people.put(emailID, firstName);
}
}
// TODO: Sort by value the map
System.out.println(people.toString());
List<String> valueList = new ArrayList<String>(people.values());
Collections.sort(valueList);
for(String person: valueList){
System.out.println(person);
}
scanner.close();
}
public static boolean isGmailAccount(String email){
boolean result = false;
Pattern p = Pattern.compile("@gmail.com");
Matcher m = p.matcher(email);
if (m.find()){
result = true;
}
return result;
}
public static void sortMapByValue(Map<String, String> people){
List<String> keyList = new LinkedList<String>(people.keySet());
List<String> valueList = new ArrayList<String>(people.values());
Collections.sort(valueList);
System.out.println(valueList);
}
}