重置Scanner对象读取的字符串

时间:2012-03-03 15:37:59

标签: java

我有2个扫描仪对象,scan1和scan2。 scan1从文件中取一行并将其传递给scan2,后者分别读取行的值。我的问题是:如何重置scan2对象正在读取的行?我有多个if语句,每个语句需要重置scan2正在使用的行。这是扫描方法的代码:

public void start() throws MalformedURLException, IOException
{

/**scan1 takes each line of input as a string,
scan2 breaks up each string into variables and stores in objects with for loop*/

URL data = new URL("sample.txt");
URLConnection url = data.openConnection(); 
BufferedReader buffer = new BufferedReader(newInputStreamReader(url.getInputStream()));
    Scanner scan1 = new Scanner(buffer);
    scan1.nextLine();
    scan1.nextLine();

    for(int i=0;i<customerList.length;i++)
    {   
        String line1 = scan1.nextLine();
        Scanner scan2 = new Scanner(line1);
        scan2.useDelimiter("\t");

        //PersonalInfo
        number=scan2.nextInt();
        gender=scan2.next();
        givenName=scan2.next();
        middleInitial=scan2.next();
        surname=scan2.next();
        //MailingAddress
        streetAddress=scan2.next();
        city=scan2.next();
        state=scan2.next();
        zip=scan2.nextInt();
        emailAddress=scan2.next();
        telephoneNum=scan2.next();
        String nat=scan2.next();
        int natLength = nat.length();
        if(natLength != 11)
        {
            String line2 = scan1.nextLine();
            Scanner scan2 = new Scanner(line2);
            String nat2 = scan2.next();
            nationalId = nat + nat2;
        }
        else if(nat == null)
        {
            String line2 = scan1.nextLine();
            Scanner scan2 = new Scanner(line2);
            nationalId = scan2.next();
        }
        else
        {
            nationalId = nat;
        }

        String birth=scan2.next();

        if(birth==null)
        {
            String line3 = scan1.nextLine();
            Scanner scan2 = new Scanner(line3);
            birthday = scan2.next();
        }
        else
        {
            birthday = birth;
        }

        cctype=scan2.next();

        if(cctype==null)
        {
            String line4 = scan1.nextLine();
            Scanner scan2 = new Scanner(line4);
            String firstVal = scan2.next();
            String checkVal = String.valueOf(i-3);
            if(firstVal == checkVal)
            {
                //creates Customer object in customerList array
                address =new MailingAddress(streetAddress, city, state, zip);
                info = new PersonalInformation(givenName, middleInitial, 
                surname, gender, emailAddress, nationalId,telephoneNum, birthday);

                customerList[i]=new Customer(number, info, address);
            }
        }
        else
        {
            //CreditCard
            String line5 = scan1.nextLine();
            Scanner scan2 = new Scanner(line5);
            ccnumber=scan2.nextLong();
            cvv2=scan2.nextInt();
            ccExpiry=scan2.next();
            //MailingAddress
            ups=scan2.next();

            //creates PurchasingCustomer object in customerList array       
            address =new MailingAddress(streetAddress, city, state, zip);
            info = new PersonalInformation(givenName, middleInitial, surname, gender, 
                                            emailAddress, nationalId,telephoneNum, birthday);
            creditCard = new CreditCard(cctype, ccnumber, cvv2, ccExpiry);
            customerList[i]=new PurchasingCustomer(number, ups, address, info, creditCard );
        }
    }

正如您所看到的,它目前设置为每次读取新行时都创建一个新的scan2对象,但实际上我只想更改scan2为每个if语句读取的字符串。提前感谢任何可以提供帮助的人!

2 个答案:

答案 0 :(得分:3)

为什么不每次都创建一个新的Scanner对象?事实上,这就是应该在这里做的事情。有些对象是为了便于重复使用而制作的,但是扫描仪并不是其中之一,但它们便宜且易于制作,所以我建议你继续做你正在做的事情并制作一个新的Scanner对象。需要,但只要确保在完成任何扫描仪对象后关闭它们。否则,您可能会耗尽系统资源。例如,您需要关闭for循环底部的scan2对象:

for(int i=0;i<customerList.length;i++)
{   
    String line1 = scan1.nextLine();
    Scanner scan2 = new Scanner(line1);
    scan2.useDelimiter("\t");

    // ... a bunch of code deleted for brevity's sake
    // ... etc...

    scan2.close();
} // end of for loop

答案 1 :(得分:1)

创建一个新的扫描仪每个实例都可以工作,但你的表现会很糟糕。创建和销毁对象是一项昂贵的操作。另一种较便宜的解决方案是使用String split方法将组件拆分为数组。然后,您可以遍历数组元素并对每个数组元素进行处理。由于在完成外循环处理之前仍然拥有所有元素,因此如果处理需要,可以返回它们。

在原始代码中,您有一些编译问题,您无法在if语句中使用scan2,您正在重新定义已在for循环中创建的扫描程序。此代码也没有重置原始字符串,它正在读取文件中的下一行。