這是一個學校的分配。 我的任務之一是從一個包含15個元素的文件中讀取數據,將每個對象存儲在一個數組中,並將更正後的對象打印到一個新文件中。循環打印最後一行多次 - 我做錯了什麼? - 使用BufferedReader
每個對象都有6個元素,用空格分隔。所以我用一個開關殼體,以確定被記錄什麼元素時:
例如:空間0是一個長期的,空間1是一個字符串,空間2是一個int等
下面是在該元件.txt文件
900876512 Core_Java 2007 Mike_Simon 129.99 568
765867999 Java_Applications_for_Programmers 2010
David_Wilson_and_Jack_Westman 173.25 672
465979798 From_Java_to_C++ 2008 Linda_Jackson 118.73 439
760098908 Microsoft_VC++ 2006 Garry_Wesley 165.20 416
529086890 Software_Engineering 2005 Alain_Macmillan 219.99 651
765867999 Visual_Basic 2004 Mary_Rosen 108.33 388
529086890 Database_Systems 2007 Peter_Jones_and_Jack_Lewis 157.87 862
800003243 VLSI 2008 Martha_Niclson 117.29 360
200900210 C_# 2007 D._Smith 109.99 387
200900210 Cellular_Communications 2010 Jones_Tomson 127.87 162
542087665 Pattern_Recognition 1998 Sam_Davis 212.59 328
900876512 Programming_Methodologies 2009 Steve_A._Richmond 182.95 590
900876512 OO_Programming 2008 Frank_Raymond 182.25 439
900876512 Design_Pattern 2002 Jay_Franklin 122.15 217
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
問題是,如果我在J循環內,循環會打印所有對象。但是,一旦外,如果我打印陣列將多次打印的最後一行是這樣的:
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
900876512 Networking_and_Data_Communications 2010 Pete_Jonson 229.25 724
我試圖把 PublicationArray[i] = obj_publication;
在不同的地方,但沒有任何工程。
對於我所看到的,每一行都被讀取並存儲在數組中,但隨後會被循環的下一次迭代覆蓋。
而不是做
I [0] f] [0]
I [0] f] [1]
I [0] j的[2]
等
它做這樣的:
我[0:J [0]
我[1:J [0]
I [2:J [0]
等
我明白循環,但我不知道這個是錯的碼。
這是學校的一項任務,所以我有我可以做和不可以做的具體事情。例如,我不能使用Arraylist。只有一個數組。
這只是我的代碼的一小部分,如果有什麼不清楚請告訴我,所以我可以修復它。我一直在盯着它,因爲我從心底深處知道,所以我從新的角度看待困難。
我在這段代碼的try catch中創建了BufferReader和PrintWriter。 BufferReader和PrintWriter在循環外部關閉。
編輯:所以我試圖打印索引我和j像@Jernej K說,但一切似乎都很好。
編輯2:我認爲問題是我的輸出方法,但我意識到它是在循環中的問題。當我在j
循環內使用System.out.println(PublicationArray[i].toString);
時,我的結果與在i
循環內使用System.out.println(PublicationArray[i].toString);
的結果不同。
//******************************readline() in files*********************
//holding array...
//this works, prints everything ok.
String[] myHolder = new String[iNumberOfItems];
for(int i=0; i < iNumberOfItems; i++){
myHolder[i] = myReader.readLine();
System.out.println("holder "+myHolder[i].toString());
}
//this is the problem...
for(int i=0; i < iNumberOfItems; i++){
sContainer = myHolder[i];
sContainer = sContainer.replace('\t', ' ');//replace all tabs with spaces
if(sContainer == null){
return;//exit the loop if this read, shouldn't do it but just in case!
}
else{//if there is a line
iCountSpace = 0;//reset space count for each line
//******************read each char in the line*********************
for(int j=0; j < sContainer.length(); j++){
cTemp = sContainer.charAt(j); //read one character at a time
//***********switch case-->what publication type needs to be recorded?
//iCountSpace check at which space the read() is -> if space new space, iCountSpace++
switch(iCountSpace){
case 0:// case 0 space is Long publication_code
sTemp.append(cTemp);//concatenation
//if the char is a space
if(cTemp == cSpace){
sTemp.deleteCharAt(sTemp.length()-1);//remove the space
sSwitchTypeContainer = sTemp.toString(); //1-->StringBuffer converting to String
code = Long.valueOf(sSwitchTypeContainer);//2-->Value of the String converted into a Long
obj_publication.setCode(code);//3-->value of long placed in code
iCountSpace ++;//4-->increase value of the space number
sTemp.delete(0, sTemp.length());//4--empty the StringBuffer for next element
}
break;
case 1://case 1 space is String publication_names
sTemp.append(cTemp);//concatenation-->casting because we are recording a String
if(cSpace == cTemp){//if the char is a space, remove it
sTemp.deleteCharAt(sTemp.length()-1);//remove the space
obj_publication.setName(sTemp.toString());//transform the StringBuffer to String
iCountSpace ++;//increase value of the space number
sTemp.delete(0, sTemp.length());//reset sTemp
}
break;
case 2://case 2 space is int publication_year
sTemp.append(cTemp);//concatenation
if(cSpace == cTemp){//if iTemp is a space...remove it
sTemp.deleteCharAt(sTemp.length()-1);//remove the space
sSwitchTypeContainer = sTemp.toString();//1-->converting StringBuffer to String
iPublication = Integer.valueOf(sSwitchTypeContainer);//2-->converting String to int
obj_publication.setYear(iPublication);//3-->set year
iCountSpace++;//4-->increase space number
sTemp.delete(0, sTemp.length());//5-->clear the buffer String
}
break;
case 3://case 3 space is String publication_authorname
sTemp.append(cTemp);//concatenation-->string
if(cSpace == cTemp){//if cTemp is a space...
sTemp.deleteCharAt(sTemp.length()-1);//remove the space
obj_publication.setAuthorName(sTemp.toString());//-->set the string in the object
iCountSpace++;//-->increase space
sTemp.delete(0, sTemp.length());//clear buffer String
}
break;
case 4://case 4 space is double publication_cost
sTemp.append(cTemp);//concatenation
if(cSpace == cTemp){//if iTemp is a space
sTemp.deleteCharAt(sTemp.length()-1);//remove the space
sSwitchTypeContainer = sTemp.toString();//1-->converting StringBuffer to String
dCost = Double.valueOf(sSwitchTypeContainer);//2-->converting String to Double
obj_publication.setCost(dCost);//3-->recording variable in object
iCountSpace++;//4-->increase space number
sTemp.delete(0, sTemp.length());//-->reset value of sTemp String Buffer
}
break;
case 5://case 5 space is int publication_nbpages
sTemp.append(cTemp);//concatenation
if(j == (sContainer.length()-1)){//if it's the last char of the line
sSwitchTypeContainer = sTemp.toString();
iPublication = Integer.valueOf(sSwitchTypeContainer);
obj_publication.setPages(iPublication);
iCountSpace++;
sTemp.delete(0, sTemp.length());
}
break;
default:
break;
}//end of switch case
}//end of for loop J
PublicationArray[i] = obj_publication;
}//end of else
}//end of foor loop i
什麼是您的輸出方法是什麼樣子? – DarkJade
如果你已經找到了問題的答案,那麼你應該張貼作爲一個答案,而不是作爲一個編輯的問題。 – Servy