我想从“DataFile.txt”文件中读取数据,但在程序中我有 2 个整数,那么如何使用 StringTokenizer 传递整数(“days”和“tprice”变量应该是整数)?
private void readDataFileAndDisplay()
{
String allRecord=""; // all details of customer
try
{
Scanner in = new Scanner (new FileReader ("DataFile.txt")); //open file
String myEntry = "" ;
String name ="";
String ID="";
String roomType ="";
String meal="";
String days="";
String tprice="";
while (in.hasNextLine ())
{
myEntry = in.nextLine();
StringTokenizer st = new StringTokenizer (myEntry, ",");
while (st.hasMoreTokens ())
{
name = st.nextToken();
ID = st.nextToken();
name = st.nextToken();
roomType = st.nextToken();
meal = st.nextToken();
days = st.nextToken();
tprice =st.nextToken();
myList.add (new Customer (name, ID, roomType, meal, days, tprice));
}
} // end of while loop
in.close(); // close file
} catch (IOException ex)
{
System.out.println ("file loading failed.");
}
}
我也试过 parseInt 像:
int Intdays = Integer.parseInt (st.nextToken ());
int IntTprice = Integer.parseInt (st.nextToken ());
但编译后显示以下异常: