2017-07-26 57 views
0

我有一個文件,我試圖從中讀取多行文件。但取決於它是一個房地產經紀人行還是屬性行,它會將某些東西存儲到行值列表中。我找不出合適的循環讀取線正常這裏是我的代碼讀取行如何讀取多個文件行並將它們存儲到使用掃描程序的不同對象中

while (fileScanner.hasNextLine()) { 
     String oneLine; 
     String[] lineValues = null; 
     oneLine = fileScanner.nextLine(); 

     lineValues = oneLine.split(","); 

     if (lineValues[0].contains("REALTOR")) { 
      if (lineValues[1].contains("ADD")) {      
       processRealtorAddition(lineValues);      
      } else if (lineValues[1].contains("DEL")) { 
       realtorDeletion(lineValues); 
      } 
      else 
       break; 

     } if (lineValues[0].contains("PROPERTY")) { 
      System.out.println("fsdfsdfsdfdsfdsfsdfsdfds");     
      if (lineValues[1].contains("ADD")) {      
       processPropertyAddition(lineValues); 
       break; 
      } else if (lineValues[1].contains("DEL")) { 
       propertyDeletion(lineValues); 
      } 
     }    
    } 
} 

但是當我運行,我得到這樣的:

The Realtor Object with a license number of MN4564567 has been added fsdfsdfsdfdsfdsfsdfsdfds Realtor Log:

Property Log: Property{mlsNumber=4455667, licenseNumber=MN4564567, streetAdress=4455 This Circle, city=Denver, state=CO, zipCode=80333, bedrooms=1, bathrooms=1.0, sold=false, askingPrice=344555.0} All properties are correct BUILD SUCCESSFUL (total time: 0 seconds)

這是我的文件想讀:

REALTOR,ADD,MN4564567,Carla,Combs,444-555-6666,0.014 PROPERTY,ADD,4455667,MN4564567,4455 This Circle,Denver,CO,80333,1,1,N,344555 REALTOR,ADD,RR6655443,Jerry,Smith,555-444-3333,0.013 PROPERTY,ADD,23456789,RR6655443,888 Terry Lane,Longmont,CO,80503,3,2,N,222222 REALTOR,ADD,AB1234567,Matthew,Munez,123-456-7890,0.012 PROPERTY,ADD,1234567,AB1234567,1234 Which Way,Somewhere,CO,82222,3,3,Y,222222 PROPERTY,ADD,2234567,AB1234567,345 Main St,Fort Collins,CO,81333,4,3.5,N,222333 REALTOR,DEL,MN4564567 REALTOR,ADD,XY98765432,Alex,Yung,999-888-7777,0.013 PROPERTY,ADD,9998888,XY98765432,111 Main St,Cheyenne,WY,82222,1,1

,N,199888

我只需要它來讀取第一行獲得所有的R ight信息,然後再次啓動while循環,然後讀取第二行。謝謝!

這是處理另外

static void processPropertyAddition(String lineValues[]) { 
    Property property = new Property(lineValues); 

    boolean value1 = property.verifyMlsNumber(); 
    boolean value2 = property.verifyState(); 
    boolean value3 = property.verifyZipCode(); 

    if (value1 == false) { 
     System.out.println("ERROR: invalid MLS number: " 
       + property.mlsNumber + "\n"); 
    } else if (value2 == false) { 
     System.out.println("ERROR: Invalid State: " 
       + property.state + "\n"); 
    } else if (value3 == false) { 
     System.out.println("ERROR: Invalid zip code: " 
       + property.zipCode + "\n"); 
    } 

    boolean value4 = realtorLogImpl.isLicenseUnique(property.getLicenseNumber()); 

    boolean value5 = propertyLogImpl.isMlsUnique(property.getMlsNumber()); 
    if (value4 == false && value5 == false) { 
     propertyList.add(property); 
    } else if (value5 == true && value4 == true) { 
     propertyList.add(property); 
    } else if (value4 == false && value5 == true) { 
     propertyList.add(property); 

     System.out.println("The Property with Realtor license number " 
       + property.getLicenseNumber() + " and with MLS number" 
       + property.getMlsNumber() + " has been added"); 
    } else if (value4 == true && value5 == false) { 
     System.err.println(" Property will not be added due to " 
       + "a Realtor license or a MLS number that is not " 
       + "unique"); 
    } 
} 

這是PropertyLogImpl的一個屬性添加到屬性鏈接列表的方法靜態方法

public boolean add(Property property) { 
    return propertyList.add(property); 
} 

我必須有一個方法添加一個屬性,我覺得這種方法可能不正確,可能是它的原因

+0

你知道'break'語句的作用嗎? – ajb

+0

我忘了刪除系統輸出fsdfdsfsdfdsfdsfsdfsd – INnoVate

+0

是我做的,這是,如果我拿出break語句 – INnoVate

回答

0

而不是這個

} if (lineValues[0].contains("PROPERTY")) { 

你可能想寫

} else if (lineValues[0].contains("PROPERTY")) { 

此外,只要您使用contains你probaly想用equals代替。

而且正如在評論中提到的那樣,您對break聲明的使用是可疑的。

如果您遇到無限循環,那麼到目前爲止所顯示的代碼中沒有任何內容會導致此問題。這是可能的無限循環來自這裏:

realtorLogImpl.isLicenseUnique(property.getLicenseNumber()); 

它應該是很容易找到一個調試器的原因(提示,暗示,...)

+0

好吧即時編輯它,並告訴你之後會發生什麼 – INnoVate

0

break語句就會中斷for/while循環。

如果您在processPropertyAddition方法調用後刪除break語句,應該修復您的錯誤。

相關問題