嗨,SO专家,
第 1 步。我创建了一个 DVDInfo 类,它具有标题、流派、leadActor 以及各自的 getter 和 setter,如下面的代码所示。
问题:我必须从具有“正斜杠/”的 dvdInfo.txt 文件中填充一个 Arraylist,并且所需的输出应包含不带“正斜杠/”的 arrayList。
------------------------------------
dvdInfo.txt file
------------------------------------
Donnie Darko/sci-fi/Gyllenhall, Jake
Raiders of the Lost Ark/action/Ford, Harrison
2001/sc-fi/??
Caddy Shack/comedy/Murray, Bill
Star Wars/sc-fi/Ford, Harrison
Lost in Translation/comedy/Murray, Bill
Patriot Games/action/Ford, Harrison
-------------------------------------
import java.util.*;
import java.io.*;
class DVDInfo{
private String title;
private String genre;
private String leadActor;
DVDInfo(String t,String g,String a){
this.title = t;
this.genre =g;
this.leadActor=a;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getLeadActor() {
return leadActor;
}
public void setLeadActor(String leadActor) {
this.leadActor = leadActor;
}
@Override
public String toString() {
return "DVDInfo [title=" + title + ", genre=" + genre + ", leadActor="
+ leadActor + "]";
}
}
public class TestDVD {
/*
* //access dvdInfo.txt file and populate the arrayList by removing forward slash.
*/
public static void populateList() throws Exception {
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\src\\dvdInfo.txt")));
//read each line of dvdInfo.txt below
String line=br.readLine();
while(line !=null) {
DVDInfo = new DVDInfo(t,g,a);
// Step 1.Wanted to create a DVDInfo Instance for each line of data we read in from dvdInfo.txt file.//failed to do so
// Step 2.for each DVDInfo Instance ,we need to parse the line of data and populate DVDInfo's 3 instance variables --no clue.
// Step 3.Finally put all DVDInfo instances into the ArrayList--- i dont know how to proceed(no clue)
}
}
public static void main(String[] args) {
ArrayList<DVDInfo> dvdList= new ArrayList<DVDInfo>();
try{
populateList();
}catch(Exception e){
System.out.println("Exception caught"+e.getMessage());
}
System.out.println(dvdList);
}
}
------------------------------------------------------
output of populateList should be without forward slash
------------------------------------------------------
Donnie Darko sci-fi Gyllenhall, Jake
Raiders of the Lost Ark action Ford, Harrison
2001 sc-fi ??
Caddy Shack comedy Murray, Bill
Star Wars sc-fi Ford, Harrison
Lost in Translation comedy Murray, Bill
Patriot Games action Ford, Harrison
请帮助我使用 Java 1.5 版中 PopulateList 方法的代码,我对此很困惑。
感谢一切顺利!!!向所有 SO 专家欢呼。
最佳答案
populateList()
//Signature of method changed to add List<DVDInfo> as a parameter
public static void populateList(List<DVDInfo> info) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\src\\dvdInfo.txt")));
//read each line of dvdInfo.txt below
String line = null;
while ((line = br.readLine()) != null) { //check line in loop
String[] tokens = line.split("/");
DVDInfo infoItem = new DVDInfo(tokens[0],tokens[1],tokens[2]);
info.add(infoItem);
}
}
main()
//dvdList is now passed to the method.
public static void main(String[] args) {
ArrayList<DVDInfo> dvdList= new ArrayList<DVDInfo>();
try{
populateList(dvdList);
}catch(Exception e){
System.out.println("Exception caught"+e.getMessage());
}
System.out.println(dvdList);
}
无限循环问题
这部分代码会导致无限循环。
String line=br.readLine();
while(line !=null) {
DVDInfo = new DVDInfo(t,g,a);
}
这是因为从缓冲区读取一行,然后在该行不为空时执行循环。由于永远不会从缓冲区读取新行,因此该行永远不会为空,从而造成无限循环。这可以使用以下方法修复:
String line = null;
while ((line = br.readLine()) != null) { //check line in loop
}
此循环构造将缓冲区中的行分配给 `Strin
关于java - 从文本文件填充 ArrayList 并删除所需输出中的正斜杠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15473515/