【问题标题】:Have some problem with " useDelimiter("[,\n]"); ". How can it correct?“ useDelimiter("[,\n]"); 有一些问题。怎么可能改正?
【发布时间】:2020-10-14 15:18:59
【问题描述】:
public void getConformFileDate(int year,int month,int date,int numOfday,int sr,int dr,int tr,int fr,int kr,int qr){
    
    String PIN = "" ; String check_in = ""; String NDates =""; String Rmonth = "" ; String Ryear = "" ;String people = "" ; String price = "" ;
    String single = "" ; String doub = ""; String triple =""; String fam = "" ; String king = "" ;String queen = "" ;
    
    try{
        x = new Scanner(new File(conf));
        x.useDelimiter("[,\n]");
        
        while(x.hasNext()){
            PIN       = x.next();
            check_in  = x.next();
            NDates    = x.next();
            Rmonth    = x.next();
            Ryear     = x.next();
            people    = x.next();
            price     = x.next();
            single    = x.next();
            doub      = x.next();
            triple    = x.next();
            fam       = x.next();
            king      = x.next();
            queen     = x.next();
            
            checkWithConformFile(Ryear,Rmonth,check_in,NDates,year,month,date,numOfday,single,doub,triple,fam,king,queen,sr,dr,tr,fr,kr,qr);
            //sumcroom = sumcroom + croom;
        }
        
        x.close();
        
    }
    catch(Exception e){
        System.out.println(e);
    }
}

在我将一些字符串值(如 Rmonth、Ryear)转换为 int 类型并与我的输入值进行比较之后。 如果我的输入值不在我的文本文件中,它会给出真正的输出。但是我的输入值在我的文本文件中,它给出了错误。

错误: "ava.lang.NumberFormatException: 对于输入字符串:"3

我的文本文件:

000001,2,7,9,2020,1,8000,1,0,0,0,0,0 000002,1,5,9,2020,2,12000,1,1,0,0,0,0 000003,9,12,10,2020,5,26000,1,0,0,1,0,0enter image description here

【问题讨论】:

    标签: java


    【解决方案1】:

    问题:

    在您的代码(如下所示)中,您调用 next() 13 次以调用 x.hasNext(),因此只有第一次调用 next()PIN = x.next(); 将是有效的,而其他可能是无效的,因为 @ 987654325@ 没有检查它们。

    while(x.hasNext()) {
          PIN       = x.next();
          check_in  = x.next();
          ...
          ...
          ...      
          checkWithConformFile(Ryear,Rmonth,check_in,NDates,year,month,date,numOfday,single,doub,triple,fam,king,queen,sr,dr,tr,fr,kr,qr);
      }
    

    解决办法:

    您需要在每次调用next()之前调用x.hasNext(),如下所示:

    while(x.hasNext()) {
          PIN       = x.next();
          if(x.hasNext()){
                check_in  = x.next();
          }   
          if(x.hasNext()){
                NDates    = x.next();
          } 
          ...
          ...
          ...
          checkWithConformFile(Ryear,Rmonth,check_in,NDates,year,month,date,numOfday,single,doub,triple,fam,king,queen,sr,dr,tr,fr,kr,qr);
    }
    

    【讨论】:

    • 非常感谢兄弟。
    • @Arvind Kumar Avinash。谢啦兄弟。我会做的。
    【解决方案2】:

    您的字符串无法转换为数字。如果你的字符串是“3,那么它是一个无效的整数。

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多