|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
[JAVA] Split(); Qualcosa che non quadra..
Salve qualcosa non quadra...
Qui sotto ho allegato il codice di HighScores.java L'obbiettivo dell'unico metodo che ho lasciato è semplice. Dovrebbe aprire un file, controllare se il punteggio inserito è magiore di uno qualsiasi del file e in caso inserirlo al poso di quello piu basso.. Qialcosa non quadr nel tutto.. Ma non so cosa.. :P ho chiesto ad un paio di miei amici ma non hanno saputo aiutarli... L'errore che mi restituisce è semplice anche da comprendere: >>Errore: java.lang.NumberFormatException: For input string: "M" Ovvero che non può estrarre l'int da una stringa contenente M.. Ok, ma perchè split non funzioa a dovere?? Sotto ho allegato anche il file "punteggi.dat" HighScores.java Vedi sotto, file aggiornato. punteggi.dat Vedi sotto, file aggiornato. Punteggi.dat funziona secondo questa struttura: "posizione|nome|cognome|punteggio" CìVi ringrazio per l'ennesimo aiuto.. :P
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 Ultima modifica di jj.69kt : 24-06-2008 alle 18:15. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2003
Messaggi: 4907
|
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
I'M an IDIOT
edit
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 Ultima modifica di jj.69kt : 25-06-2008 alle 21:37. |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Codice:
package Project;
import java.io.*;
public class HighScores {
private File puntiFile;
private String testoHTML, buffLine;
private String[] rigaIn, rigaOut = new String[4];
private BufferedReader buffReader;
private FileOutputStream outFile;
public HighScores(){}
/**
* Aggiunge *se necessario* il punteggio fornito nella posizione corretta.
* @param name nome dell'utente
* @param surname cognome dell'utente
* @param point punteggio de''utente
*/
public void addPoint(String name, String surname, Integer point){
int i = 0;
int punti = point;
rigaOut[1] = name;
rigaOut[2] = surname;
rigaOut[3] = point.toString();
try{
puntiFile = new File("./punteggi.dat");
buffReader = new BufferedReader(new FileReader(puntiFile));
while(buffReader.ready())
{
buffLine = buffReader.readLine();
rigaIn = buffLine.split("|");
rigaOut[0] = rigaIn[0];
Integer puntiRead = Integer.parseInt(rigaIn[3]);
if (puntiRead > punti)
i++;
else if (puntiRead <= punti) {
try {
outFile = new FileOutputStream(puntiFile);
PrintStream output = new PrintStream(outFile);
output.println(rigaIn[0]+"|"+rigaOut[1]+"|"+rigaOut[2]+"|"+rigaOut[3]);
i++;
} catch (IOException e) {
System.out.println("Errore: " + e);
}
}
}
} catch (Exception exe){
System.out.println("Errore: "+ exe);
}
}
__________________
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
edit
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 Ultima modifica di jj.69kt : 25-06-2008 alle 21:37. |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
BENE!
edit
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 Ultima modifica di jj.69kt : 25-06-2008 alle 21:37. |
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
prova così:
Codice:
import java.io.*;
public class HighScores {
private File puntiFile;
private String testoHTML, buffLine, doc;
private String[] rigaIn, rigaOut = new String[4];
private BufferedReader buffReader;
private FileOutputStream outFile;
public HighScores(){
}
public void addPoint(String name, String surname, Integer point){
int i = 1;
int punti = point;
doc = "";
rigaOut[1] = name;
rigaOut[2] = surname;
rigaOut[3] = point.toString();
try{
puntiFile = new File("./punteggi.dat");
buffReader = new BufferedReader(new FileReader(puntiFile));
outFile = new FileOutputStream(puntiFile);
PrintStream output = new PrintStream(outFile);
List<String> fileContent = new ArrayList<String>();
while(buffReader.ready() && i<=10)
{
fileContent.add(buffReader.readLine().split("\\|"));
}
for (int i = 0; i < fileContent.size(); i++) {
String[] line = fileContent[i];
if(Integer.parseInt(line[3]) < punti) {
line.set(i, new String[4] {line[0], name, surname, point.toString()});
}
for (String[] line : fileContent) {
for (String value : line) {
output.print(value + "|");
}
output.println("");
}
output.flush();
output.close();
} catch (Exception exe){
System.out.println("Errore: "+ exe);
}
}
public static void main(String[]args){
HighScores hi = new HighScores();
hi.addPoint("Pippo", "Oppip", 200);
}
}
__________________
Ultima modifica di ^TiGeRShArK^ : 24-06-2008 alle 13:15. |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
edit
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 Ultima modifica di jj.69kt : 25-06-2008 alle 21:32. |
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
FINITO!!!!
Ce l'ho fatta!!
Ecco il codice se servisse a qualcuno: Codice:
public class HighScores {
private File puntiFile;
private String testoHTML, buffLine, doc;
private String[] rigaFile = new String[3], rigaOut = new String[3];
private BufferedReader buffReader;
private FileOutputStream outFile;
public HighScores() {}
/**
* Aggiunge *se necessario* il punteggio fornito nella posizione corretta.
* @param name nome dell'utente
* @param surname cognome dell'utente
* @param point punteggio de''utente
*/
public void addPoint(String name, String surname, Integer point) {
int i = 1;
int punti = point;
doc = "";
rigaOut[0] = name;
rigaOut[1] = surname;
rigaOut[2] = point.toString();
boolean scritto = false;
boolean skip = false;
int letture=0, scritture=0, aggiorn=0;
puntiFile = new File("./punteggi.dat");
try {
/* IF (file *NON* esiste)
* THEN ~> crea il file e scrive i dati in input.
*/
if(!puntiFile.exists()) {
puntiFile.createNewFile();
doc = rigaOut[0] + "|" + rigaOut[1] + "|" + rigaOut[2];
System.out.println(i + "* Creato Nuovo");
scritture++;
} else {
try {
buffReader = new BufferedReader(new FileReader(puntiFile));
//finche il buffer è diverso da null e siamo a meno di 10 cicli lui fà:
while (buffReader.ready() && i <= 10) {
buffLine = buffReader.readLine();
rigaFile = buffLine.split("\\|");
Integer puntiDelFile = Integer.parseInt(rigaFile[2]);
/* Se i dati in input solo IDENTICI ai dati appena letti lo segnala per evitare di
* aggiungerli nuovamente
*/
if(rigaOut[0].equals(rigaFile[0]) && rigaOut[1].equals(rigaFile[1]) &&
rigaOut[2].equals(rigaFile[2])) {
skip = true;
System.out.println(i + "* skip Found!!!!");
}
if(skip) {
doc += rigaFile[0] + "|" + rigaFile[1] + "|" + rigaFile[2];
if(i < 10) {
doc += "\n";
}
System.out.println(i + " * Lettura (byskip)");
letture++;
}
/* IF (abbiamo trovato l'input nel file)
*/
if(!skip) {
/* IF (il punteggio letto è maggiore dell'input)
* THEN ~> Riscrive i dati in letti senza modifiche.
*/
if(puntiDelFile > punti) {
doc += rigaFile[0] + "|" + rigaFile[1] + "|" + rigaFile[2];
if(i < 10) {
doc += "\n";
}
System.out.println(i + "* Lettura (byBigger)");
letture++;
}
/* IF (punteggio letto è minore o uguale all'input)
*/
if(puntiDelFile <= punti) {
/* IF (*NON* abbiamo scritto)
* THEN ~> Aggiunge la *NUOVA* riga e segnala la scrittura
*/
if(!scritto) {
doc += rigaOut[0] + "|" + rigaOut[1] + "|" + rigaOut[2];
if(i < 10) {
doc += "\n";
}
System.out.println(i + "* Aggiunta");
scritture++;
scritto = true;
}
/* IF (Abbiamo scritto)
* THEN ~> Aggiunge al documento la riga LETTA in precedenza.
*/
if(scritto) {
System.out.println(i + "* Aggionamento");
aggiorn++;
doc += rigaFile[0] + "|" + rigaFile[1] + "|" + rigaFile[2];
if(i < 10) {
doc += "\n";
}
}
}
}
i++;
}
if(!scritto && i <= 10 && !skip) {
doc += rigaOut[0] + "|" + rigaOut[1] + "|" + rigaOut[2];
System.out.println(i + "* Scr out-while");
scritture++;
}
} catch (Exception exxe) {
System.out.println("Errore file: " + exxe);
}
}
outFile = new FileOutputStream(puntiFile);
PrintStream output = new PrintStream(outFile);
output.print(doc);
output.flush();
output.close();
System.out.println(i + "* Salvato tutto");
System.out.println("Letture:"+letture+" & Scritture:"+scritture+" & Aggiornamenti:"+aggiorn);
System.out.println();
} catch (Exception exe) {
System.out.println("Errore exe: " + exe);
}
}
}
Provate a fare questo main... (Il file punteggi.dat viene creato in automatico) E vedrete: Codice:
public static void main(String[] args) {
HighScores hi = new HighScores();
hi.addPoint("uno", "due", 252);
hi.addPoint("pèdante", "posticcio", 40);
hi.addPoint("uno", "due", 252);
hi.addPoint("tre", "prova", 1000);
hi.addPoint("nome", "cognome", 123);
hi.addPoint("ertoide", "polimorfo", 40);
}
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 |
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
ehmm..
il difetto + grande che vedo è che ha una complessità ciclomatica tendente ad infinito ed una leggibilità tendente a zero..
__________________
|
|
|
|
|
|
#11 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Adesso sarebbe interessante & utile un po' di refactoring, anche se ammetto che per stare nella scadenza del compito non è essenziale, mentre sarebbe utilissimo a scopo "didattico".
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
|
#12 |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
Beh alla fine passa al tutti gli elementi una volta per ogni aggiunta ovvero O(n)..
Non è una complessità assurda.. :P Comunque ho intenzione *dopo aver terminato il progetto* di provare a sistemare TUTTO il codice del progetto con soluzioni piu *furbe*... Comunque l'unica cosa che mi interessa ora è che funzioni... :P Quel difetto nella scrittura non penso capiterà molto spesso..... Dovrebbero arrivare ALMENO due volte un punteggio uguale con nomi diversi... Nnon mi sono spiegati un granche comunque penso che in percentuale sia molto meno dello 0.5%...... :P :P Grazie a chi mi ha aiutato..
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 |
|
|
|
|
|
#13 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Solo una precisazione:
Complessità ciclomatica != Complessità algoritmica Inoltre: la leggibilità è sempre un must da ricercare nella stesura del codice, imho
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#14 | |
|
Senior Member
Iscritto dal: Oct 2007
Messaggi: 677
|
Quote:
Ho interpretato male.... *apre wikipedia* Ah ok.. Comunque per la leggibilità sono d'accordo.. :P Anche se non capisco esattamente come potrei migliorarlo per la lettura.. o.0 Ci lavorerò su.. EDIT: Vediamo se ho capito: V(G) = 14 Giusto? :P Oppure: V(G) = 13+n (dove "n" è il numero di while che fa?)
__________________
Desktop: (In Vendita) iMac (12,2) 27" --> i5 QaudCore 2,7GHz, 8GB RAM, 1TB HDD.. Laptop (In Pensione): MacBook PRO (3,1) 15.4" (Glossy, ma pentito) --> 2.2Ghz, 4GB Ram.. Camera: Nikon D80 with Nikkor 18-55mm 1:3-5.6 GII + Nikkor 55-200mm 1:4-5.6 VR "Mobile": iPhone 5 64Gb MyEbay: 100%! • Mercatino (Green = Good; Red = Bad) UncleSam, Boxi80, Zuckerbaer, lamerone, danidj, maxmax80 Ultima modifica di jj.69kt : 26-06-2008 alle 11:07. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 20:26.




















