当前位置: 技术问答>java相关
查错给分!!!!!!!!!!帮忙啦!!
来源: 互联网 发布时间:2015-01-23
本文导语: import java.io.*; import java.util.*; public class fee2 { public static void main(String arg[]) { Hashtable user=new Hashtable(); String username; int usetime; try { String line_str; int line_number=0; int addtime; FileInputStream...
import java.io.*;
import java.util.*;
public class fee2
{
public static void main(String arg[])
{
Hashtable user=new Hashtable();
String username;
int usetime;
try
{
String line_str;
int line_number=0;
int addtime;
FileInputStream is=new FileInputStream("fee.txt");
DataInputStream ds=new DataInputStream(is);
while((line_str=ds.readLine())!=null)
{
line_number++;
StringTokenizer st=new StringTokenizer(line_str);
//get username and usetime
username=(String)st.nextToken();
usetime=Integer.valueOf(st.nextToken()).intValue();
if(!user.containsKey(username))
{
user.put(username,usetime);
}
else
{
addtime=user.get(username);
usetime=usetime+addtime;
user.put(username,usetime);
}
username="";
usetime=0;
}
//System.out.println("the end:"+username+":"+usetime);
Enumeration keys = user.keys();
while(keys.hasMoreElements())
{
String key = (String)keys.nextElement();
System.out.println("Key is: " + key.toString());
}
ds.close();
}
catch(IOException e)
{
System.out.println("file error:"+e);
}
}
}
import java.util.*;
public class fee2
{
public static void main(String arg[])
{
Hashtable user=new Hashtable();
String username;
int usetime;
try
{
String line_str;
int line_number=0;
int addtime;
FileInputStream is=new FileInputStream("fee.txt");
DataInputStream ds=new DataInputStream(is);
while((line_str=ds.readLine())!=null)
{
line_number++;
StringTokenizer st=new StringTokenizer(line_str);
//get username and usetime
username=(String)st.nextToken();
usetime=Integer.valueOf(st.nextToken()).intValue();
if(!user.containsKey(username))
{
user.put(username,usetime);
}
else
{
addtime=user.get(username);
usetime=usetime+addtime;
user.put(username,usetime);
}
username="";
usetime=0;
}
//System.out.println("the end:"+username+":"+usetime);
Enumeration keys = user.keys();
while(keys.hasMoreElements())
{
String key = (String)keys.nextElement();
System.out.println("Key is: " + key.toString());
}
ds.close();
}
catch(IOException e)
{
System.out.println("file error:"+e);
}
}
}
|
Hashtable的put 的两个参数都是Object:
user.put(username,usetime);
是错的,应时:
user.put(username,new Integer(usetime));
相应:
addtime=user.get(username);
应该为:
addtime=((Integer)user.get(username)).intValue();
*另外,你的愿意是不是要把 usetime也打印出来?如果那样在输出时要加上:
int times=((Integer)user.get(key)).intValue();
System.out.println("Key is: " + key.toString()+" usetime:"+times);
这样,我运行了,输出结果类似:
---------- run ----------
Key is: gang usetime:10
Key is: niu usetime:11
Key is: zhao usetime:3
Normal Termination
Output completed (5 sec consumed).
user.put(username,usetime);
是错的,应时:
user.put(username,new Integer(usetime));
相应:
addtime=user.get(username);
应该为:
addtime=((Integer)user.get(username)).intValue();
*另外,你的愿意是不是要把 usetime也打印出来?如果那样在输出时要加上:
int times=((Integer)user.get(key)).intValue();
System.out.println("Key is: " + key.toString()+" usetime:"+times);
这样,我运行了,输出结果类似:
---------- run ----------
Key is: gang usetime:10
Key is: niu usetime:11
Key is: zhao usetime:3
Normal Termination
Output completed (5 sec consumed).
|
import java.io.*;
import java.util.*;
public class fee1
{
public static void main(String arg[])
{
Hashtable user=new Hashtable();
try
{
String line_str;
int line_number=0;
int addtime;
String username="0";
String usetime="0";
FileInputStream is=new FileInputStream("fee.txt");
DataInputStream ds=new DataInputStream(is);
while((line_str=ds.readLine())!=null)
{
line_number++;
StringTokenizer st=new StringTokenizer(line_str);
//get username and usetime
//System.out.println("line="+line_number+"--->"+line_str+"st="+st);
username=st.nextToken();
//usetime=Integer.parseInt(String.valueOf((Object)st.nextToken()));
usetime=st.nextToken();
//System.out.println("line="+line_number+" username--->"+username);
//user.put(username,usetime);
if(!user.containsKey(username))
{
user.put(username,usetime);
}
else
{
addtime=Integer.parseInt(String.valueOf(user.get(username)));
usetime=usetime+addtime;
user.put(username,usetime);
}
}
//System.out.println("the end:"+username+":"+usetime);
Enumeration e = user.elements() ;
while(e.hasMoreElements())
{
username=String.valueOf((Object)e.nextElement());
usetime=(String)user.get(username);
System.out.println("username:"+username);
System.out.println("time:"+usetime);
}
System.out.println("---------end---------");
ds=null;
}
catch(Exception e)
{
System.out.println("file error:"+e);
}
}
}
import java.util.*;
public class fee1
{
public static void main(String arg[])
{
Hashtable user=new Hashtable();
try
{
String line_str;
int line_number=0;
int addtime;
String username="0";
String usetime="0";
FileInputStream is=new FileInputStream("fee.txt");
DataInputStream ds=new DataInputStream(is);
while((line_str=ds.readLine())!=null)
{
line_number++;
StringTokenizer st=new StringTokenizer(line_str);
//get username and usetime
//System.out.println("line="+line_number+"--->"+line_str+"st="+st);
username=st.nextToken();
//usetime=Integer.parseInt(String.valueOf((Object)st.nextToken()));
usetime=st.nextToken();
//System.out.println("line="+line_number+" username--->"+username);
//user.put(username,usetime);
if(!user.containsKey(username))
{
user.put(username,usetime);
}
else
{
addtime=Integer.parseInt(String.valueOf(user.get(username)));
usetime=usetime+addtime;
user.put(username,usetime);
}
}
//System.out.println("the end:"+username+":"+usetime);
Enumeration e = user.elements() ;
while(e.hasMoreElements())
{
username=String.valueOf((Object)e.nextElement());
usetime=(String)user.get(username);
System.out.println("username:"+username);
System.out.println("time:"+usetime);
}
System.out.println("---------end---------");
ds=null;
}
catch(Exception e)
{
System.out.println("file error:"+e);
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。