Java - Ayuda porfavor

 
Vista:

Ayuda porfavor

Publicado por bach (2 intervenciones) el 24/03/2014 22:10:56
Buenas tardes:

En este programa debo leer un archivo de texto y luego cada vez que en cuentre la palabra C eb el archivo cree una istancia de canica la cual el constructor de canica tiene una ArrayList y debe almacenarcen todas las canicas q sea creadas , el problema q tengo es q Al hacer esto solo me almacena a la lista la ultima canica y necesito almacenarlas todas aca va el codigo por favor alguien me puede ayudar ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package Canicas;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
 
 
 
public class Principal {
private static ArrayList <Integer> miLista = new ArrayList<Integer>();
private static ArrayList <Integer> miLista2 = new ArrayList<Integer>();
 
public void leerArchivo(String nombreArchivo) {
BufferedReader br = null;
 
try {
 
String linea;
 
br = new BufferedReader(new FileReader(nombreArchivo));
 
while ((linea = br.readLine()) != null) {
String[] elementos = linea.split(",");
 
for (int i = 0; i < elementos.length; i++) {
if(elementos[i].equals("T")){
int x=Integer.parseInt(elementos[i+1]);
int y=Integer.parseInt(elementos[i+2]);
miLista.add(x);
miLista.add(y);
 
}
 
if(elementos[i].equals("C")){
int x,y,d,p;
x=Integer.parseInt(elementos[i+1]);
y=Integer.parseInt(elementos[i+2]);
d=Integer.parseInt(elementos[i+3]);
p=Integer.parseInt(elementos[i+4]);
miLista2.add(x);
miLista2.add(y);
miLista2.add(d);
miLista2.add(p);
 
 
}
 
 
 
 
 
 
}
}
 
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
 
public static void main(String[] args) {
Scanner lector= new Scanner(System.in);
String Iniciar,Juego;
Tablero t = null;
Canicas c = null;
System.out.println("BIENVENIDO AL JUEGO DE CANICAS");
do{
System.out.println("PRESIONE LA TECLA 'I' Y A CONTINUACION ENTER PARA EMPEZAR");
Iniciar = lector.nextLine();
}while(!Iniciar.equals("I"));
Principal p = new Principal();
p.leerArchivo("d:\\config.txt");
System.out.println("ESTADO DEL JUEGO : ");
 
// recorre la lista de el tablero
for (int i = 0; i <miLista.size(); i++) {
if(i==0){
int x = miLista.get(i);
int y = miLista.get(i+1);
t = new Tablero(x,y);
}
}
 
// recorre la lista de las canicas y crea la instancia canica
for (int i = 0; i <miLista2.size(); i++) {
if(((i+1)%4)==0){
int x = miLista2.get(i-3);
int y =miLista2.get(i-2);
int d = miLista2.get(i-1);
int r = miLista2.get(i);
c = new Canicas(x,y,d,r);
t.agregarCanica(c);
 
}
//aca va la clase canica
 
 
package Canicas;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
 
public class Canicas extends Direction implements Moveable {
private int x,y;
private Direction dir;
private Position pos;
private ArrayList <Canicas> miLista = new ArrayList<Canicas>();
 
 
 
public Canicas(int x,int y , int d,int p){
super(d,p);
miLista.add(this);
this.x=x;
this.y=y;
 
 
}


puse un pedazo de codigo por favor ayudemen no he podido hacerlo

aca esta el achivo de texto

T,5,8
C,3,2,1,0
C,2,1,0,1
C,5,5,-1,0
B,5,4
B,3,3
I,4,5
D,2,2
D,6,5

osea la lista de canica debe tener 3 canicas
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder