?? tokenstandardio.java
字號(hào):
//TokenStandardIO.java
import java.io.*;
import java.util.*;
public class TokenStandardIO
{
//需要輸入的person數(shù)目。
public static int NUMBER = 3;
public static void main(String[] args)
{
Person[] people=new Person[NUMBER];
//暫時(shí)容納輸入數(shù)據(jù)的臨時(shí)字符串?dāng)?shù)組。
String[] field = new String[4];
//初始化field數(shù)組。
for(int i=0; i < 4; i++)
{
field[i] = "";
}
//IO操作必須捕獲IO異常。
try
{
//用于對(duì)field數(shù)組進(jìn)行增加控制。
int fieldcount = 0;
//先使用System.in構(gòu)造InputStreamReader,再構(gòu)造BufferedReader。
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
for(int i = 0; i< NUMBER; i++)
{
fieldcount = 0;
System.out.println("The number " + (i + 1) + " person");
System.out.println("Enter name,age,salary,married(optional), please separate fields by ':'");
//讀取一行。
String personstr=stdin.readLine();
//設(shè)置分隔符。
StringTokenizer st = new StringTokenizer(personstr, ":");
//判斷是否還有分隔符可用。
while (st.hasMoreTokens())
{
field[fieldcount] = st.nextToken();
fieldcount++;
}
//如果輸入married,則field[3]不為空,調(diào)用具有四個(gè)參數(shù)的Person構(gòu)造函數(shù)。
if(field[3] != "")
{
people[i] = new Person(field[0], Integer.parseInt(field[1]),
Double.parseDouble(field[2]), field[3]);
//置field[3]為空,以備下次輸入使用。
field[3] = "";
}
//如果未輸入married,則field[3]為空,調(diào)用具有三個(gè)參數(shù)的Person構(gòu)造函數(shù)。
else
{
people[i] = new Person(field[0], Integer.parseInt(field[1]),
Double.parseDouble(field[2]));
}
}
//輸出已經(jīng)輸入的數(shù)據(jù)。
System.out.println("Output the data of people:");
for(int i = 0; i < NUMBER; i++)
{
System.out.println("The number " + (i + 1) + " person");
System.out.println("name: " + people[i].getName() + "; age:" + people[i].getAge() +
"; salary: " + people[i].getSalary() + "; married: " + people[i].getMarried());
}
}
catch (IOException e)
{
System.err.println("IOException");
}
}
}
class Person
{
private String name;
private int age;
private double salary;
private String married;
public Person(String n, int a, double s)
{
name = n;
age = a;
salary = s;
married = "F";
}
public Person(String n, int a, double s, String m)
{
name = n;
age = a;
salary = s;
married = m;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getSalary()
{
return salary;
}
public String getMarried()
{
return married;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -