Dúvida no exercício de: Arquivo + Collections + token?

5 respostas
diegohsi

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 :?
package colecoes;
import java.util.*;
import java.io.*;

public class DVDInfo {
	String title;
	String genre;
	String leadActor;
	ArrayList<String> al = new ArrayList<String>();
	
	/*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;
	}
}
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);
	}	
}

5 Respostas

ViniGodoy

E a dúvida qual é mesmo?

diegohsi

Minha duvida é como retirar “/” e substitui-las por " "(espaço). Se escrevi corretamente no arquivo. Eu vi que nao implementei Camparator ou Camparable e nem sobrescrevi compareTo().

V

Cara vc pode fazer o seguinte

Quando vc quebrar a string vc concatena um espaço nela.

Deve ter uma forma com mais classe de resolver isso … rs mais acho que isso resolve seu problema…

String [] strings = "Donnie Darko/sci-fi/Gyllenhall, Jake".split("/");
		
		for (int i = 0; i < strings.length; i++) {
			
			System.out.print(strings[i] + " ");
		}
V

toma como base esse exemplo de Comparable que utilizei em um projeto que fiz

@Override
	public int compareTo(Projeto o) {
		if (o.getId() == id) {
			return 0;
		}else if (o.getId() < id) {
			return 1;
		} else {
			return -1;
		}
	}

moderador migra esse post para java basico

diegohsi

Obrigado pelas dicas Vistor, eu tinha pensado nessa forma mas no exercicio 2 vou precisar de calssificar por genero vou ter que refazer todo exercicio mais o seu exemplo me deu uma ideia

obrigado novamente

te mais

Criado 16 de outubro de 2010
Ultima resposta 16 de out. de 2010
Respostas 5
Participantes 3