我有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语句。提前感谢任何能够帮助的人!
也许包含一些示例代码来演示您的答案?事实上,目前这不是一个真正的答案。 – 2014-03-01 17:20:25