Advertisement
cd62131

User

Jul 11th, 2014
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.39 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.Map.Entry;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class User {
  9.   public static void main(String[] args) {
  10.     Map<String, String> user = new TreeMap<String, String>();
  11.     user.put("taro", "pass1");
  12.     user.put("hanako", "pass2");
  13.     StringBuilder sb1 = new StringBuilder();
  14.     sb1.append("username taro password pass1\n");
  15.     sb1.append("username jiro password pass3\n");
  16.     Scanner in = new Scanner(sb1.toString());
  17.     Pattern re = Pattern.compile("username (\\w+) password (\\w+)");
  18.     while (in.hasNextLine()) {
  19.       Matcher m = re.matcher(in.nextLine());
  20.       if (m.find()) {
  21.         boolean found = false;
  22.         for (Entry<String, String> e : user.entrySet()) {
  23.           if (e.getKey().equals(m.group(1))) {
  24.             System.out.println(m.group(1) + "はすでに登録されています");
  25.             found = true;
  26.             break;
  27.           }
  28.         }
  29.         if (!found) {
  30.           user.put(m.group(1), m.group(2));
  31.           System.out.println("新しく" + m.group(1) + "を登録しました");
  32.         }
  33.       }
  34.     }
  35.     in.close();
  36.     StringBuilder sb2 = new StringBuilder();
  37.     for (Entry<String, String> e : user.entrySet()) {
  38.       sb2.append("User: " + e.getKey() + "\n");
  39.     }
  40.     System.out.println(sb2);
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement