Bom dia Pessoal!
Estou com duvida nesta exercicio que (estou a mais de 6hrs tentando resolve-lo) envolve Arquivo + Collections + token.
Criei o seguinte arquivo File file
Donnie Darko/sci-fi/Gyllenhall, Jake
Raiders of the Lost Ark/action/Ford, Harrison
2001/sci-fi/??
Caddy Shack/comedy/Murray, Bill
Star Wars/sci-fi/Ford, Harrison
Lost in Translation/comedy/Murray, Bill
Patriot Games/action/Ford, Harrison
E gostaria de exibi-lo dessa forma…
Donnie Darko sci-fi Gyllenhall, Jake
Raiders of the Lost Ark action Ford, Harrison
2001 sci-fi ??
Caddy Shack comedy Murray, Bill
Star Wars sci-fi Ford, Harrison
Lost in Translation comedy Murray, Bill
Patriot Games action Ford, Harrison
OBS: Esta com espaço no lugar das barras normais. Não posso usar String.replaceAll() apenas split() para tokenizar
A segunda parte do exercicio é exibir a mesma lista em ordem de titulo. Perdoem-me se minha codificação esta ridícula :?
[code] package colecoes;
import java.util.;
import java.io.;
public class DVDInfo {
String title;
String genre;
String leadActor;
ArrayList al = new ArrayList();
/*DVDInfo(String t, String g, String l) {
title = t;
genre = g;
leadActor = l;
}*/
//Populando a Lista
public void populateList() {
try {
File file = new File("dvdinfo.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
file.createNewFile();
bw.write("Donnie Darko/sci-fi/Gyllenhall, Jake");
bw.write("Raiders of the Lost Ark/action/Ford, Harrison");
bw.write("2001/sci-fi/??");
bw.write("Caddy Shack/comedy/Murray, Bill");
bw.write("Star Wars/sci-fi/Ford, Harrison");
bw.write("Lost in Translation/comedy/Murray, Bill");
bw.write("Patriot Games/action/Ford, Harrison");
bw.flush();
bw.close();
} catch(Exception exception) {
exception.printStackTrace();
}
try {
File fileR = new File("dvdinfo.txt");
BufferedReader br = new BufferedReader(
new FileReader(fileR));
String token;
String[] tokens;
while((token = br.readLine()) != null) {
tokens = token.split(" "); // Lê a linha inteira e joga dentro de al
for(String palavra : tokens) {
al.add(palavra);
}
}
br.close();
} catch(Exception exception) {
exception.printStackTrace();
}
}
public void setTitle(String t) {
title = t;
}
public String getTitle() {
return title;
}
public void setGenre(String g) {
genre = g;
}
public String getGenre() {
return genre;
}
public void setLeadActor(String la) {
leadActor = la;
}
public String getLeadActor() {
return leadActor;
}
public String toString(){
return title + " " + genre + " " + leadActor;
}
} [/code]
[code] package colecoes;
import java.util.*;
public class TesteDVDInfo {
public static void main(String[] args) {
DVDInfo dvdLista = new DVDInfo();
dvdLista.populateList();
dvdLista.populateList();
System.out.println(dvdLista.al);
}
} [/code]