2012-03-03 89 views
1

我有2個掃描儀對象scan1和scan2。 scan1正在從文件中取出一行並將其傳遞給scan2,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語句。提前感謝任何能夠幫助的人!

回答

3

爲什麼不每次都創建一個新的掃描儀對象?而實際上這就是你在應該在這裏做的。有些物體是爲了便於重複使用而設計的,但掃描儀不是其中之一,但它們便宜且易於製造,因此我建議您繼續按照自己的操作進行操作並製作新的掃描儀對象需要的,但只要確保在完成掃描儀對象後關閉任何掃描儀對象。否則,您可能會耗盡系統資源。例如,你想在的底部,關閉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

創建一個新的Scanner每個實例都將工作,但你的表現將是可怕的。創建和銷燬對象是一項昂貴的操作。另一個更便宜的解決方案是使用字符串拆分方法將組件拆分爲數組。然後,您可以遍歷數組元素並對每個數組元素進行處理。由於在完成外部循環處理之前,您仍然擁有所有的元素,所以如果需要處理,可以返回到它們。

在原始代碼中,您有一些編譯問題,您不能在if語句中使用scan2,您正在重新定義已在for循環中創建的掃描程序。此外,此代碼不會重置原始字符串,而是讀取文件中的下一行。

+0

也許包含一些示例代碼來演示您的答案?事實上,目前這不是一個真正的答案。 – 2014-03-01 17:20:25