Salve!!
Ho creato un programma che leggere delle stringhe ordinate su di un file e le compara ognuna con le successive in modo tale da eliminare i doppioni....
Il programma funziona benissimo però quando arriva che leggo la stringa che non ha più stringhe dopo il programma non mi va bene e non mi esegue la parte di codice di chiusura. Ho creato anche una variabile tmp, ma nulla di fatto.
codice:
/* elimina le stringhe doppie da un file di stringhe ordinate*/
package matteo.fileoperation;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class mercurio{
/* proprietà della classe */
private Path src;
private Path dst;
/* costruttore della classe */
public mercurio(String source,String destination){
this.src = Paths.get(source);
this.dst = Paths.get(destination);
}
/* unico metodo pubblici*/
public void clean()
{
//scelta del set di caratteri da utilizzare
Charset charset = Charset.forName("ISO-8859-1");
// stringhe da comparare; solo first viene strascritta
String first="";
String second="";
try{
//lettore bufferizzato del sorgente
BufferedReader reader = Files.newBufferedReader(this.src, charset);
//scrittore bufferizzato del sorgente
BufferedWriter writer = Files.newBufferedWriter(this.dst, charset);
// null flag
Boolean empty = false;
//compare flag
int equ = 0;
if((first = reader.readLine()) == null)
{
empty = true;
}
if(empty == false)
{
if((second = reader.readLine()) == null)
{
empty = true;
}
}
while(empty == false)
{
equ = first.compareToIgnoreCase(second);
if(equ==0)
{
if((second = reader.readLine()) == null)
{
empty = true;
}
}
else
{
writer.append(first, 0, first.length());
writer.newLine();
first = second;
String tmp = "";
if((tmp = reader.readLine()) == null)
{
writer.append(first, 0, first.length());
writer.newLine();
System.out.println("\n\npatatracchete\n\n");
empty = true;
}
else
{
second = tmp;
}
}
}
writer.flush();
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
}
public static void main(String[] v)
{
String source;
String destination;
Scanner scan = new Scanner(System.in);
System.out.println("<--- apertura \'Double cleaner\' in corso... --->\n\n");
System.out.println("inserire l'indirizzo del sorgente:");
source = scan.nextLine();
System.out.println("\n\n inserire l'indirizzo della destinazione:");
destination = scan.nextLine();
mercurio Planet = new mercurio(source,destination);
System.out.println("\n\n lavori in corso...\n\n");
Planet.clean();
System.out.println(" termine lavori\n\n" +
" <--- chiusura \'double cleaner\' in corso...--->");
}}
Vorrei sapere inoltre come poter realizzare per questo programma un installer e un eseguibile che mi avvi l'applicazione senza dover scrivere nulla nel terminale o ricorrere all'IDE.
Grazie!!!