PDA

View Full Version : (Java) problema caricamento file con \t


aduri
17-08-2006, 15:11
Come vi avevo anticipato nell'altro post eccomi di nuova qua'.
Questo codice da me sviluppato a livello didattico sul sort di un array e sulla ricerca binaria e' andato a buon fine caricando l'array direttamente da codice.

Se io volessi caricare da file composto da 2 campi (uno stringa ed un intero) separati da tabulazione (\t) e 82 records (dati4.txt) come posso fare?
Chiaramente devo sempre poter gestire sort e ricerca.
Io ho provato (codice commentato) ma sono riuscito a fare apparire il primo record dopodiche' dava errore; Allego il codice:


Codice:
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
class MyFile {
public int dim;
public String nome;
public MyFile (int dim, String nome) {
this.dim = dim;
this.nome = nome;
}
}

class KeyDim implements Comparator {
public int compare (Object MyFile1, Object MyFile2) {
int dim1 = ((MyFile) MyFile1).dim;
int dim2 = ((MyFile) MyFile2).dim;
return (dim1 < dim2 ? -1 : (dim1 == dim2 ? 0 : 1));
}
}

class KeyNome implements Comparator {
public int compare (Object MyFile1, Object MyFile2) {
String nome1 = ((MyFile) MyFile1).nome;
String nome2 = ((MyFile) MyFile2).nome;
return nome1.compareTo (nome2);
}
public static void main(String[] args) throws
IOException{
KeyDim keydim = new KeyDim();
KeyNome keyNome = new KeyNome();

//Carico file da dati4.txt
// String nomeFile=JOptionPane.showInputDialog("Digita il nome del file da leggere");
//
// BufferedReader fileLettura;
// try
// {
// fileLettura = new BufferedReader(new FileReader(nomeFile));
// while(true)
// {
// String linea = fileLettura.readLine();
// if(linea==null)
// break;
// System.out.println(linea);
// }
// }
// catch (FileNotFoundException e)
// {
// System.out.println("Il file "+nomeFile + " non esiste o non puo' essere aperto");
// }
// catch (IOException e)
// {
// System.out.println("errore di lettura "+e);
// }
//
// System.exit(0);


//Carico file manualmente
MyFile[] a = new MyFile[8];
a[0] = new MyFile (12120, "Acrobat.exe");
a[1] = new MyFile (13152, "Quovadis.txt");
a[2] = new MyFile (13200, "execute.bat");
a[3] = new MyFile (13300, "revers.exe");
a[4] = new MyFile (13400, "equi.bat");
a[5] = new MyFile (13500, "ripple.exe");
a[6] = new MyFile (13600, "gong.mid");
a[7] = new MyFile (12119, "guida.doc");

System.out.println ("Array non ordinato: ");
for (int k = 0; k < a.length; k++)
System.out.println (a[k].dim + " " + a[k].nome);

System.out.println ("Array ordinato rispetto alla Dimensione: ");
Arrays.sort (a, keydim);
for (int k = 0; k < a.length; k++)
System.out.println (a[k].dim + " " + a[k].nome);

System.out.println ("Array ordinato rispetto al nome: ");
Arrays.sort (a, keyNome);
for (int k = 0; k < a.length; k++)
System.out.println (a[k].dim + " " + a[k].nome);

//Implemento la ricerca binaria per nome
System.out.println ("");
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Cerca file con nome: ");
String nomeCercato = in.readLine();

MyFile fileCercato = new MyFile(-1, nomeCercato);
int pos = Arrays.binarySearch (a, fileCercato, keyNome);
if (pos >= 0)
System.out.println ("File trovato (dopo averlo ordinato secondo nome) in posizione: " + pos);
else
System.out.println ("Nome file inesistente");

}
}

DvL^Nemo
17-08-2006, 15:43
Prova questo


import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

class MyFile {
public int dim;
public String nome;
public MyFile (int dim, String nome) {
this.dim = dim;
this.nome = nome;
}
}

class KeyDim implements Comparator {
public int compare (Object MyFile1, Object MyFile2) {
int dim1 = ((MyFile) MyFile1).dim;
int dim2 = ((MyFile) MyFile2).dim;
return (dim1 < dim2 ? -1 : (dim1 == dim2 ? 0 : 1));
}
}

class Prova implements Comparator {
public int compare (Object MyFile1, Object MyFile2) {
String nome1 = ((MyFile) MyFile1).nome;
String nome2 = ((MyFile) MyFile2).nome;
return nome1.compareTo (nome2);
}
public static void main(String[] args) throws
IOException{
KeyDim keydim = new KeyDim();
Prova keyNome = new Prova();

// Carico file da dati4.txt
// String nomeFile=JOptionPane.showInputDialog("Digita il nome del file da leggere");
String nomeFile="c:\\Dati4.txt";
Vector vArr=new Vector();
MyFile arMyFile[];
MyFile myFile;

BufferedReader fileLettura;
try
{
fileLettura = new BufferedReader(new FileReader(nomeFile));
String sLinea;
while((sLinea=fileLettura.readLine())!=null){
String sAppo[]=sLinea.split("\t");
myFile = new MyFile (Integer.parseInt(sAppo[1]), sAppo[0]);
vArr.add(myFile);
}
}
catch (FileNotFoundException e)
{
System.out.println("Il file "+nomeFile + " non esiste o non puo' essere aperto");
}
catch (IOException e)
{
System.out.println("errore di lettura "+e);
}

arMyFile=new MyFile[vArr.size()];
for(int i=0;i<arMyFile.length;i++){
arMyFile[i]=(MyFile) vArr.get(i);
}

// System.exit(0);


// Carico file manualmente
// MyFile[] a = new MyFile[8];
// a[0] = new MyFile (12120, "Acrobat.exe");
// a[1] = new MyFile (13152, "Quovadis.txt");
// a[2] = new MyFile (13200, "execute.bat");
// a[3] = new MyFile (13300, "revers.exe");
// a[4] = new MyFile (13400, "equi.bat");
// a[5] = new MyFile (13500, "ripple.exe");
// a[6] = new MyFile (13600, "gong.mid");
// a[7] = new MyFile (12119, "guida.doc");

System.out.println ("Array non ordinato: ");
for (int k = 0; k < arMyFile.length; k++)
System.out.println (arMyFile[k].dim + " " + arMyFile[k].nome);

System.out.println ("Array ordinato rispetto alla Dimensione: ");
Arrays.sort (arMyFile, keydim);
for (int k = 0; k < arMyFile.length; k++)
System.out.println (arMyFile[k].dim + " " + arMyFile[k].nome);

System.out.println ("Array ordinato rispetto al nome: ");
Arrays.sort (arMyFile, keyNome);
for (int k = 0; k < arMyFile.length; k++)
System.out.println (arMyFile[k].dim + " " + arMyFile[k].nome);

// Implemento la ricerca binaria per nome
System.out.println ("");
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Cerca file con nome: ");
String nomeCercato = in.readLine();

MyFile fileCercato = new MyFile(-1, nomeCercato);
int pos = Arrays.binarySearch (arMyFile, fileCercato, keyNome);
if (pos >= 0)
System.out.println ("File trovato (dopo averlo ordinato secondo nome) in posizione: " + pos);
else
System.out.println ("Nome file inesistente");

}
}

aduri
17-08-2006, 15:49
Grazie, :D
Che velocita'.
appena posso lo compilo.
S questa macchina non posso. :muro:
Ha installato s.o.W98SE e CPU Duron 1300MHz e non mi permette di installare JDK1.4 e 1.5; mi dice che non ho abbastanza privilegi.
Sapete dirmi qualcosa????? :mc:

DvL^Nemo
17-08-2006, 15:53
Beh il grosso del lavoro lo hai fatto te', ho dovuto aggiungere si e no 4 righe di codice..
Quello del W98 dovrebbe essere un problema di autorizzazione, dovresi chiedere quindi all'amministratore ( sempre che ne esista uno ) di darti appunto l'aturizzazione con il tuo utente di installare programmi.
Ciao !

DvL^Nemo
17-08-2006, 15:55
Visto che non puoi provarlo ti posto pure l'output


Array non ordinato:
20 convert.c
44 tables.o
420 latex2rtf
4 xref.h
32 xref.o
28 tables.c
20 util.o
0 stamp-build
20 ignore.o
8 ignore.c
16 definitions.c
4 util.h
4 README.DOS
44 commands.o
44 ChangeLog
36 convert.o
4 README.Mac
56 equation.o
4 preamble.h
24 main.c
4 encode.h
48 chars.o
40 parser.c
4 chars.h
4 counters.h
4 Copyright
4 l2r_fonts.h
4 tables.h
48 main.o
20 direct.o
8 stack.c
48 funct1.c
8 temp.tmp
4 mygetopt.h
24 l2r_fonts.c
4 direct.c
56 encode.c
4 letterformat.h
4 parser.h
20 stack.o
36 graphics.c
20 counters.o
20 letterformat.o
4 direct.h
4 cfg.h
32 equation.c
4 graphics.h
4 stack.h
68 encode.o
32 definitions.o
4 ignore.h
8 report.rtf
12 xref.c
4 version.h
4 README
8 report.tex
4 equation.h
4 main.h
4 counters.c
72 funct1.o
8 util.c
32 preamble.c
4 mygetopt.c
4 commands.h
20 mygetopt.o
32 encode_tables.h
4 lengths.c
16 cfg.c
8 funct1.h
8 letterformat.c
60 graphics.o
56 preamble.o
36 l2r_fonts.o
32 commands.c
28 cfg.o
4 convert.h
56 parser.o
12 Makefile
4 lengths.h
24 chars.c
4 definitions.h
20 lengths.o
Array ordinato rispetto alla Dimensione:
0 stamp-build
4 xref.h
4 util.h
4 README.DOS
4 README.Mac
4 preamble.h
4 encode.h
4 chars.h
4 counters.h
4 Copyright
4 l2r_fonts.h
4 tables.h
4 mygetopt.h
4 direct.c
4 letterformat.h
4 parser.h
4 direct.h
4 cfg.h
4 graphics.h
4 stack.h
4 ignore.h
4 version.h
4 README
4 equation.h
4 main.h
4 counters.c
4 mygetopt.c
4 commands.h
4 lengths.c
4 convert.h
4 lengths.h
4 definitions.h
8 ignore.c
8 stack.c
8 temp.tmp
8 report.rtf
8 report.tex
8 util.c
8 funct1.h
8 letterformat.c
12 xref.c
12 Makefile
16 definitions.c
16 cfg.c
20 convert.c
20 util.o
20 ignore.o
20 direct.o
20 stack.o
20 counters.o
20 letterformat.o
20 mygetopt.o
20 lengths.o
24 main.c
24 l2r_fonts.c
24 chars.c
28 tables.c
28 cfg.o
32 xref.o
32 equation.c
32 definitions.o
32 preamble.c
32 encode_tables.h
32 commands.c
36 convert.o
36 graphics.c
36 l2r_fonts.o
40 parser.c
44 tables.o
44 commands.o
44 ChangeLog
48 chars.o
48 main.o
48 funct1.c
56 equation.o
56 encode.c
56 preamble.o
56 parser.o
60 graphics.o
68 encode.o
72 funct1.o
420 latex2rtf
Array ordinato rispetto al nome:
44 ChangeLog
4 Copyright
12 Makefile
4 README
4 README.DOS
4 README.Mac
16 cfg.c
4 cfg.h
28 cfg.o
24 chars.c
4 chars.h
48 chars.o
32 commands.c
4 commands.h
44 commands.o
20 convert.c
4 convert.h
36 convert.o
4 counters.c
4 counters.h
20 counters.o
16 definitions.c
4 definitions.h
32 definitions.o
4 direct.c
4 direct.h
20 direct.o
56 encode.c
4 encode.h
68 encode.o
32 encode_tables.h
32 equation.c
4 equation.h
56 equation.o
48 funct1.c
8 funct1.h
72 funct1.o
36 graphics.c
4 graphics.h
60 graphics.o
8 ignore.c
4 ignore.h
20 ignore.o
24 l2r_fonts.c
4 l2r_fonts.h
36 l2r_fonts.o
420 latex2rtf
4 lengths.c
4 lengths.h
20 lengths.o
8 letterformat.c
4 letterformat.h
20 letterformat.o
24 main.c
4 main.h
48 main.o
4 mygetopt.c
4 mygetopt.h
20 mygetopt.o
40 parser.c
4 parser.h
56 parser.o
32 preamble.c
4 preamble.h
56 preamble.o
8 report.rtf
8 report.tex
8 stack.c
4 stack.h
20 stack.o
0 stamp-build
28 tables.c
4 tables.h
44 tables.o
8 temp.tmp
8 util.c
4 util.h
20 util.o
4 version.h
12 xref.c
4 xref.h
32 xref.o