繁体   English   中英

Java:使用StringTokenizer添加整数

[英]Java : add integers using StringTokenizer

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

但编译后显示以下异常: 在此处输入图片说明

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

尝试使用此代码。

days   = Integer.parseInt (st.nextToken ());
tprice = Integer.parseInt (st.nextToken ());

如果您将days和tprice声明为int,就可以做到。

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

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

您正在尝试两次读取名称。 您的数据只有6个字段,正试图读取7个字段。 (名称= 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()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM