Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class User {
- public static void main(String[] args) {
- Map<String, String> user = new TreeMap<String, String>();
- user.put("taro", "pass1");
- user.put("hanako", "pass2");
- StringBuilder sb1 = new StringBuilder();
- sb1.append("username taro password pass1\n");
- sb1.append("username jiro password pass3\n");
- Scanner in = new Scanner(sb1.toString());
- Pattern re = Pattern.compile("username (\\w+) password (\\w+)");
- while (in.hasNextLine()) {
- Matcher m = re.matcher(in.nextLine());
- if (m.find()) {
- boolean found = false;
- for (Entry<String, String> e : user.entrySet()) {
- if (e.getKey().equals(m.group(1))) {
- System.out.println(m.group(1) + "はすでに登録されています");
- found = true;
- break;
- }
- }
- if (!found) {
- user.put(m.group(1), m.group(2));
- System.out.println("新しく" + m.group(1) + "を登録しました");
- }
- }
- }
- in.close();
- StringBuilder sb2 = new StringBuilder();
- for (Entry<String, String> e : user.entrySet()) {
- sb2.append("User: " + e.getKey() + "\n");
- }
- System.out.println(sb2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement