0

我想从“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 ());

但编译后显示以下异常: 在此处输入图像描述

4

3 回答 3

1
name = st.nextToken();
                    ID = st.nextToken();
                    name = st.nextToken();
                    roomType = st.nextToken();
                    meal = st.nextToken();
                    days = st.nextToken();
                    tprice =st.nextToken();
                    int Intdays = Integer.parseInt(days);
                    int IntTprice = Integer.parseInt(tprice);
                 myList.add(new Customer(name,ID,roomType,meal,Intdays,IntTprice));

试试这个代码。

于 2012-04-25T10:18:26.523 回答
0
days   = Integer.parseInt (st.nextToken ());
tprice = Integer.parseInt (st.nextToken ());

如果您将 days 和 tprice 声明为 int,就会这样做。

但是您可以直接使用您的扫描仪 - 我展示了如何使用字符串而不是文件,这更适合演示:

data = "John, Miller, 303, 404";
sc = new java.util.Scanner (data);
sc.useDelimiter (", ");
name = sc.next ();      // John
surname = sc.next ();   // Miller
tdays = sc.nextInt ();  // 303
于 2012-04-25T10:18:31.027 回答
0

您正在尝试读取名称两次。您的数据只有 6 个字段,而您正在尝试读取 7 个字段。(name = st.nextToken() 看起来像一个错字)

 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));
                }    

这是错误的。每次调用 st.nextToken() 之前,您都需要检查 st.hasMoreTokens()。

于 2012-04-25T11:14:20.740 回答