import java.util.*;
import java.io.*;
class XXXX
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
Map map = new HashMap(); 
String line = br.readLine();
while(line != null && !line.equals("")){
String[] s = line.split("\\s+");
System.out.println(s[0]);
System.out.println(s[1]);
int hour = Integer.parseInt(s[1]);
if(map.containsKey(s[0])){
((Hour)map.get(s[0])).hour+=hour;
}else{
map.put(s[0],new Hour(hour));
}
line = br.readLine();
}
System.out.println(map);
}
}
class Hour{
int hour;
Hour(int hour){
this.hour = hour;
}
public String toString(){
return ""+hour;
}
}最后的输出结果你可以转换为你需要的格式,
我这里为了方便直接用的System.out.println(map);

解决方案 »

  1.   

    import java.util.*;
    import java.io.*;
    class XXXX
    {
    public static void main(String args[]) throws IOException
    {
    BufferedReader br = new BufferedReader(
    new InputStreamReader(System.in));
    Map map = new HashMap(); 
    String line = br.readLine();
    while(line != null && !line.equals("")){
    String[] s = line.split("\\s+");
    //System.out.println(s[0]);这两行不用
    //System.out.println(s[1]);
    int hour = Integer.parseInt(s[1]);
    if(map.containsKey(s[0])){
    ((Hour)map.get(s[0])).hour+=hour;
    }else{
    map.put(s[0],new Hour(hour));
    }
    line = br.readLine();
    }
    System.out.println(map);
    }
    }
    class Hour{
    int hour;
    Hour(int hour){
    this.hour = hour;
    }
    public String toString(){
    return ""+hour;
    }
    }