gpt4 book ai didi

java - 循环多次打印最后一行 - 我做错了什么? - 使用BufferedReader

转载 作者:行者123 更新时间:2023-11-30 07:32:33 25 4
gpt4 key购买 nike

这是学校的作业。我的作业之一是从包含 15 个元素的文件中读取数据,将每个对象存储在数组中,然后在新文件中打印更正后的对象。

每个对象有 6 个元素,用空格分隔。因此,我使用 switch case 来确定在以下情况下记录哪个元素:

示例:空格 0 是 Long,空格 1 是 String,空格 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]j[0]
i[0]j[1]
i[0]j[2]
等等
它正在做这样的事情:
i[0]j[0]
i[1]j[0]
i[2]j[0]
等等

我理解循环,但我不知道这段代码有什么问题。

这是学校的作业,所以我有具体可以做和不能做的事情。例如,我不能使用Arraylist。只有一个数组。

这只是我代码的一小部分,如果有不清楚的地方请告诉我,以便我修复它。我已经盯着它看了太久了,我已经熟记在心了,所以我很难从一个新的角度来看待它。

我在这段代码的 try catch 中创建了 BufferReader 和 PrintWriter。 BufferReader 和 PrintWriter 在循环外关闭。

编辑:所以我尝试像 @Jernej K 所说的那样打印 i 和 j 的索引,但那里一切似乎都很好。

编辑2:我认为问题出在我的输出方法上,但我意识到问题出在循环中。当我在 j 循环中使用 System.out.println(PublicationArray[i].toString); 时,我得到的结果与使用 System.out.println(PublicationArray[i].toString); 的结果不同。 out.println(PublicationArray[i].toString);i 循环内。

//******************************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

最佳答案

解决了!

由于 @Servy 评论而更新我的帖子
我使用一个对象作为容器。然后我更新它的值并将其放入数组中。
我以为我正在创建多个对象,但由于它是相同的引用,因此它正在更新放置在数组中的所有对象。

换句话说,我没有使用自己的引用创建多个对象,而是创建了对一个对象的多个引用。
我添加了一个循环来在数组中创建空对象:

Publication[] PublicationArray = new Publication[iSize];
for(int i=0; i<iNumberOfItems; i++){
PublicationArray[i] = new Publication();
}

我不需要像这样设置对象:obj_publication.setName(String),我需要做类似的事情PublicationArray[i].setName(String)

瞧!我的头痛消失了。

关于java - 循环多次打印最后一行 - 我做错了什么? - 使用BufferedReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35880737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com