用 Scanner 輸入 ,用規則表示去拆,再放進 Map 裡,但出現 null ?
Scanner sc = new Scanner(System.in);
Map<String, String> map = new HashMap<String, String>();
int n = sc.nextInt();// input <n> Weighted edges
while(n >= 0){
String s = sc.nextLine();
Matcher edges = Pattern.compile("\\w\\s\\w\\s").matcher(s);
Matcher weighted = Pattern.compile("\\d+").matcher(s);
String ed = null;
String weight = null;
while(edges.find())
// System.out.println(edges.group());
ed = edges.group();
while(weighted.find())
// System.out.println(weighted.group());
weight = weighted.group();
map.put(ed, weight);
n--;
}
System.out.println(map);
/*output
4
a b 1
c d 3
a d 2
b c 2
{null=null, a b =1, c d =3, b c =2, a d =2
*/