好!
import java.io.*;
import java.util.*;
class str14
{
InputStreamReader stdin=new InputStreamReader(System.in);
BufferedReader bufin=new BufferedReader(stdin); public static void main(String args[]) throws IOException
{
new str14();//run the application
} public str14() throws IOException//constructor
{
Hashtable htable=new Hashtable(20,0.75F);
dataBase(htable);
} public str14(Hashtable htable) throws IOException //override the constructor
{
dataBase(htable);
} public void dataBase(Hashtable htable) throws IOException
{
int count=htable.size();//get the ammount of the data in htable
int value,id,num;
String key,enter,data;
Enumeration keys=htable.keys();//get all the key in htable
Enumeration elements=htable.elements();//get all the elements in htable
System.out.println("Hashtable 简易数据库程序");
System.out.println("(1)输入数据");
System.out.println("(2)请除所有数据");
System.out.println("(3)显示单笔数据");
System.out.println("(4)删除单笔数据");
System.out.println("(5)显示所有数据");
System.out.println("(6)结束程序");
System.out.print("请输入您的选择:");
value=select();//call select(),return the function number
switch(value)//the function
{
case 1:
System.out.print("请输入一笔数据:");//need data input
data=bufin.readLine();
count++;
key=String.valueOf(count);
htable.put(key,data);//store it to the htable
System.out.print("输入完成,按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 2://clear all data from the htable
htable.clear();
System.out.print("已删除了所有数据... 按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 3:
System.out.print("请输入要显示的数据编号:");
id=getid(count);//call getid()
key=String.valueOf(id);
Object select=htable.get(key);//fetch the data from the htable
data=select.toString();
System.out.print("编号"+" "+"内容");
System.out.println(" "+key+" "+data);//display the data
System.out.print("按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 4:
System.out.print("请输入要删除的数据编号:");
id=getid(count);
key=String.valueOf(id);
htable.remove(key);//remove data
count--;
num=count;
System.out.print("已删除了所选择的数据...按任意见继续...");
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
elements=htable.elements();//fetch all the data from htable
while(elements.hasMoreElements())
{
key=String.valueOf(num);//a new value
data=(String)elements.nextElement();//fetch content of the data
htable1.put(key,data);//store it to htable1
num--;
}
htable.clear();
enter=bufin.readLine();
new str14(htable1);
break;
case 5:
String[] sortkey=new String[count];//create a new sort array
String[] sortdata=new String[count];
num=count;
elements=htable.elements();
keys=htable.keys();
while(elements.hasMoreElements())
{
key=(String)keys.nextElement();//fetch value of key
data=(String)elements.nextElement();//fetch value of data
sortkey[num-1]=key;//store the value of key to sortkey array
sortdata[num-1]=data;//store the value of data to sortdata array
num--;
}
System.out.println("编号"+" "+"内容");
for(int i=0;i<count;i++)//display all content after sorting
System.out.println(" "+sortkey[i]+" "+sortdata[i]);
System.out.print("目前共有"+count+"笔数据");
System.out.print("按任意键继续...");
enter=bufin.readLine();
new str14(htable);
break;
default:
}
} public int select() throws IOException//method of getting a function selector
{
String input;
int value=0;
input=bufin.readLine();//read a input from keyboard
try
{
value=Integer.parseInt(input);//convert a string to a int value
}catch(NumberFormatException e)//can’t vonverted
{
System.out.print("请输入选项1~6:");
value=select();
}
if(value>6||value<1)//if exceed then print a message and reenter
{
System.out.print("请输入选项1~6:");
value=select();
}
return value;//return a value
} public int getid(int count)throws IOException//a method of return the number of data
{
String input;
int value=0;
input=bufin.readLine();//read a user input string from keyboard
try
{
value=Integer.parseInt(input);//convert the string to a int
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
{
System.out.print("请输入数据编号:");
value=getid(count);
}
if(value>count)//the input value is out of bound
{
System.out.print("无此编号的数据,请重新输入:");
getid(count);
}
return value;//return a value
}
}
import java.io.*;
import java.util.*;
class str14
{
InputStreamReader stdin=new InputStreamReader(System.in);
BufferedReader bufin=new BufferedReader(stdin); public static void main(String args[]) throws IOException
{
new str14();//run the application
} public str14() throws IOException//constructor
{
Hashtable htable=new Hashtable(20,0.75F);
dataBase(htable);
} public str14(Hashtable htable) throws IOException //override the constructor
{
dataBase(htable);
} public void dataBase(Hashtable htable) throws IOException
{
int count=htable.size();//get the ammount of the data in htable
int value,id,num;
String key,enter,data;
Enumeration keys=htable.keys();//get all the key in htable
Enumeration elements=htable.elements();//get all the elements in htable
System.out.println("Hashtable 简易数据库程序");
System.out.println("(1)输入数据");
System.out.println("(2)请除所有数据");
System.out.println("(3)显示单笔数据");
System.out.println("(4)删除单笔数据");
System.out.println("(5)显示所有数据");
System.out.println("(6)结束程序");
System.out.print("请输入您的选择:");
value=select();//call select(),return the function number
switch(value)//the function
{
case 1:
System.out.print("请输入一笔数据:");//need data input
data=bufin.readLine();
count++;
key=String.valueOf(count);
htable.put(key,data);//store it to the htable
System.out.print("输入完成,按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 2://clear all data from the htable
htable.clear();
System.out.print("已删除了所有数据... 按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 3:
System.out.print("请输入要显示的数据编号:");
id=getid(count);//call getid()
key=String.valueOf(id);
Object select=htable.get(key);//fetch the data from the htable
data=select.toString();
System.out.print("编号"+" "+"内容");
System.out.println(" "+key+" "+data);//display the data
System.out.print("按任意键继续...");
enter=bufin.readLine();
new str14(htable);//reenter
break;
case 4:
System.out.print("请输入要删除的数据编号:");
id=getid(count);
key=String.valueOf(id);
htable.remove(key);//remove data
count--;
num=count;
System.out.print("已删除了所选择的数据...按任意见继续...");
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1
elements=htable.elements();//fetch all the data from htable
while(elements.hasMoreElements())
{
key=String.valueOf(num);//a new value
data=(String)elements.nextElement();//fetch content of the data
htable1.put(key,data);//store it to htable1
num--;
}
htable.clear();
enter=bufin.readLine();
new str14(htable1);
break;
case 5:
String[] sortkey=new String[count];//create a new sort array
String[] sortdata=new String[count];
num=count;
elements=htable.elements();
keys=htable.keys();
while(elements.hasMoreElements())
{
key=(String)keys.nextElement();//fetch value of key
data=(String)elements.nextElement();//fetch value of data
sortkey[num-1]=key;//store the value of key to sortkey array
sortdata[num-1]=data;//store the value of data to sortdata array
num--;
}
System.out.println("编号"+" "+"内容");
for(int i=0;i<count;i++)//display all content after sorting
System.out.println(" "+sortkey[i]+" "+sortdata[i]);
System.out.print("目前共有"+count+"笔数据");
System.out.print("按任意键继续...");
enter=bufin.readLine();
new str14(htable);
break;
default:
}
} public int select() throws IOException//method of getting a function selector
{
String input;
int value=0;
input=bufin.readLine();//read a input from keyboard
try
{
value=Integer.parseInt(input);//convert a string to a int value
}catch(NumberFormatException e)//can’t vonverted
{
System.out.print("请输入选项1~6:");
value=select();
}
if(value>6||value<1)//if exceed then print a message and reenter
{
System.out.print("请输入选项1~6:");
value=select();
}
return value;//return a value
} public int getid(int count)throws IOException//a method of return the number of data
{
String input;
int value=0;
input=bufin.readLine();//read a user input string from keyboard
try
{
value=Integer.parseInt(input);//convert the string to a int
}catch(NumberFormatException e)//if can’t convert to a integer then reenter
{
System.out.print("请输入数据编号:");
value=getid(count);
}
if(value>count)//the input value is out of bound
{
System.out.print("无此编号的数据,请重新输入:");
getid(count);
}
return value;//return a value
}
}
import java.io.*;
class people{
private String name;
private int age;
people(String s,int i){
name=s;
age=i;
}
public String getname(){
return name;
}
}
class peoplelist{
private people[] p;
public int leng;
peoplelist(int max){
p=new people[max];
leng=0;
}
public void insertp(String s,int i){
people pe=new people(s,i);
p[leng++]=pe;
}
public void displayp()
{
System.out.println("");
for(int i=0;i<leng;i++)
System.out.println(" "+p[i].getname());
}
}public class peoplelistapp{
public static void main(String[] args)throws IOException
{
peoplelist pl=new peoplelist(4);
String s1=new String();
System.out.println("enter a string ......:");
System.out.flush();
s1=getString();
if(s1.equals(" "))
return;
pl.insertp("huang",20);
pl.insertp("long",21);
pl.displayp();
}
public static String getString() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
return s;
}public int getInt() throws IOException
{
String s=getString();
return Integer.parseInt(s);
}
}
input=bufin.readLine();//read a input from keyboard
}catch(Exception e){
System.out.print("怪哉");
System.exit(1);
}键盘输入流转BufferReader出错
InputStream in=System.in;
in=new BufferedInputStream(in);
Reader r=new InputStreamReader(in);