有关于 “useDelimiter(“[,\n]”);” 的一些问题。如何进行修正?

huangapple go评论78阅读模式
英文:

Have some problem with " useDelimiter("[,\n]"); ". How can it correct?

问题

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

            int parsedRyear = Integer.parseInt(Ryear);
            int parsedRmonth = Integer.parseInt(Rmonth);

            checkWithConformFile(parsedRyear, parsedRmonth, 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);
    }
}
英文:
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);
}
}

after I convert some String values(like Rmonth,Ryear) to int Type and compare with my input values.
If my input value are not in my text file it give true output. But my input values are in my text file it gives error.

error:
"ava.lang.NumberFormatException: For input string: "3

my text File:

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

答案1

得分: 0

你的字符串无法转换为数字。如果你的字符串是"3,则作为整数无效。

英文:

Your string can not be converted to a number. If your string is "3, then it is an invalid as an integer.

答案2

得分: 0

问题:

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

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);
}
英文:

Problem:

In your code (shown below), you are calling next() 13 times for one call to x.hasNext() and therefore only the first call to next() i.e. PIN = x.next(); will be valid while others may be invalid because x.hasNext() is not checked for them.

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

Solution:

You need to call x.hasNext() before each call to next() as shown below:

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

huangapple
  • 本文由 发表于 2020年10月14日 23:18:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64356492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定