raniero
16-09-2005, 13:39
ciao, vi spiego il tutto: devo scrivere i valori di una matrice creata in tempi random su un file .txt. La matrice visualizzata su schermo funziona, il problema e' che quando lancio il programma, il + delle volte non mi scrive i valori della matrice nel file .txt , ma mi scrive tutti zero.. e qualche volta ( a caso!) mi scrive (nel file .txt) la prima linea della matrice e basta! Il programma e' costituito da due file Lavori.java (dove si crea la matrice) e il file Main.java (dove creo gli oggetti e faccio partire il tutto). HELP ME!
File Lavori.java :
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
public class Lavori {
Timer timer;
int x = 0;
public static int v[]=new int[5];
public static int macchine[][] = new int[101][6];
public static int b=0;
public static int s=1;
public void scrivi() throws IOException { //metodo che scrive su file
FileWriter w;
w=new FileWriter("ceppo.txt");
BufferedWriter q;
q=new BufferedWriter (w);
for(int f = 1; f < 6; f++){
for(int r = 0; r < 6; r++){
s=(macchine[f][r]);
q.write( s+ " ");
q.flush();}}
System.out.println(" test ---------> valore macchina prima riga pos 1 " +macchine[1][2]);
} //fine metodo scrivi
public void Reminder(int seconds) { // metodo che crea il tempo casuale
Lavori risultato= new Lavori();
timer = new Timer();
x= (int)Math.round(10*Math.random());
timer.schedule(new RemindTask(), x*100, seconds*100);
} //fine reminder
public class RemindTask extends TimerTask {
public void run() { //metodo che crea la matrice
int co=0;
int i=0;
while ( i < 4 ) {
v[i]= (int)Math.round(3*Math.random());
i++;
} // fine while
macchine[++b][0]=b;
macchine[b][1]=x;
macchine[b][2]=v[0];
macchine[b][3]=v[1];
macchine[b][4]=v[2];
macchine[b][5]=v[3];
System.out.println (" macchina " +b + " minuti " +x+ " pos1 " +v[0]+
" pos2 " +v[1]+ " pos3 " +v[2]+ " pos4 " +v[3]);
++co;
if (co==1){
timer.cancel();}
} // fine run
} // fine remindtask
} // fine crea
File Main.java :
public class Main {
static Lavori t;
public static void main(String args[]) throws IOException {
int i=0;
for(i=0;i<5;i++) {
t = new Lavori();
t.Reminder(1);
t.scrivi();
}
}
}
File Lavori.java :
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
public class Lavori {
Timer timer;
int x = 0;
public static int v[]=new int[5];
public static int macchine[][] = new int[101][6];
public static int b=0;
public static int s=1;
public void scrivi() throws IOException { //metodo che scrive su file
FileWriter w;
w=new FileWriter("ceppo.txt");
BufferedWriter q;
q=new BufferedWriter (w);
for(int f = 1; f < 6; f++){
for(int r = 0; r < 6; r++){
s=(macchine[f][r]);
q.write( s+ " ");
q.flush();}}
System.out.println(" test ---------> valore macchina prima riga pos 1 " +macchine[1][2]);
} //fine metodo scrivi
public void Reminder(int seconds) { // metodo che crea il tempo casuale
Lavori risultato= new Lavori();
timer = new Timer();
x= (int)Math.round(10*Math.random());
timer.schedule(new RemindTask(), x*100, seconds*100);
} //fine reminder
public class RemindTask extends TimerTask {
public void run() { //metodo che crea la matrice
int co=0;
int i=0;
while ( i < 4 ) {
v[i]= (int)Math.round(3*Math.random());
i++;
} // fine while
macchine[++b][0]=b;
macchine[b][1]=x;
macchine[b][2]=v[0];
macchine[b][3]=v[1];
macchine[b][4]=v[2];
macchine[b][5]=v[3];
System.out.println (" macchina " +b + " minuti " +x+ " pos1 " +v[0]+
" pos2 " +v[1]+ " pos3 " +v[2]+ " pos4 " +v[3]);
++co;
if (co==1){
timer.cancel();}
} // fine run
} // fine remindtask
} // fine crea
File Main.java :
public class Main {
static Lavori t;
public static void main(String args[]) throws IOException {
int i=0;
for(i=0;i<5;i++) {
t = new Lavori();
t.Reminder(1);
t.scrivi();
}
}
}