Salve, non riesco ad individuare gli errori riportati in esecuzione dopo la corretta compilazione di
questo codice, qualcuno saprebbe dirmi dove sbaglio?
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;// nome e cognome del candidato, matricola, data, numero della postazione
public class RubricaTester
{ public static void main(String[] args)
{
if(args.length != 2)
{
System.out.println("uso : inserire nomi dei due file di testo");
System.exit(1);
}
Rubrica rub1 = new Rubrica();
Rubrica rub2 = new Rubrica();
try
{
Scanner in = new Scanner(new FileReader(args[0]));
while(in.hasNextLine())
{
Scanner linescan = new Scanner(in.nextLine());
linescan.useDelimiter(":");
while(linescan.hasNextLine())
{
String nome = in.next();
String numero = in.next();
rub1.insert(nome, numero);
}
in.close();
}
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
Scanner in = new Scanner(System.in);
boolean done = false;
while(!done)
{
System.out.println("inserire Q per uscire, altrimenti inserire nome da cercare");
String nome = in.next();
if(nome.equalsIgnoreCase("Q"))
{
done = true;
System.out.println("stampo la rubrica su file 2");
}
else
{
Object numero = rub1.find(nome);
rub2.insert(nome, numero);
rub1.remove(nome);
}
}
try
{
PrintWriter out = new PrintWriter(args[1]);
out.println(rub2);
out.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
}
}
class Rubrica implements Dictionary
{
public Rubrica()
{
v = new Pair[10];
vSize = 0;
}
public boolean isEmpty()
{
return vSize == 0;
}
public void makeEmpty()
{
vSize = 0;
}
//metodi di Rubrica ......da completare ......
public String toString()
{
String s = "";
for(int i = 0; i<vSize;i++)
s = s + v.getName() + ":" + v.getPhone() + "\n";
return s;
}
public void insert(Comparable key, Object value)
{
if(key == null) throw new IllegalArgumentException();
try
{
remove(key);
}
catch(DictionaryItemNotFoundException e)
{}
if (vSize == v.length) v = resize(2*v.length);
int j = vSize;
while(j > 0 && v[j-1].getName().compareTo((String) key) > 0)
{
v[j] = v[j-1];
j--;
}
v[j] = new Pair((String) key, (Long)value);
vSize++;
}
public void remove(Comparable key)
{
int pos = binarySearch(v, 0, vSize-1, key);
for(int i = pos; i<vSize-1; i++)
{
v = v[i+1];
}
vSize--;
}
private int binarySearch(Pair [] v, int from, int to, Comparable chiave)
{
int mid = (from+to)/2;
Comparable middle = v[mid].getName();
if(middle.compareTo(chiave) == 0) return mid;
else if(middle.compareTo(chiave) > 0)
return binarySearch(v, from, mid-1, chiave);
else
return binarySearch(v, mid+1, to, chiave);
}
public Object find(Comparable key)
{
int pos = binarySearch(v, 0, vSize-1, key);
if(pos<0) throw new DictionaryItemNotFoundException();
else
return v[pos].getPhone();
}
protected Pair [] resize(int newlength)
{
if(newlength<0) throw new IllegalArgumentException ();
Pair [] newObj = new Pair[newlength];
System.arraycopy(v, 0, newObj, 0, v.length);
return newObj;
}
private int vSize;
private Pair [] v;
// ..... da completare .........
//campi di esemplare di Rubrica ..... da completare ......
//classe privata Pair: non modificare!!
private class Pair
{ public Pair(String aName, long aPhone)
{ name= aName;
phone = aPhone;
}
public String getName()
{ return name; }
public long getPhone()
{ return phone; }
/*
Restituisce una stringa contenente
- la nome, "name"
- un carattere di separazione ( : )
- il numero telefonico, "phone"
*/
public String toString()
{ return name + " : " + phone; }
//campi di esemplare
private String name;
private long phone;
}
}
interface Dictionary
{
/*
verifica se il dizionario contiene almeno una coppia chiave/valore
*/
boolean isEmpty();
/*
svuota il dizionario
*/
void makeEmpty();
/*
Inserisce un elemento nel dizionario. L'inserimento va sempre a buon fine.
Se la chiave non esiste la coppia key/value viene aggiunta al dizionario;
se la chiave esiste gia' il valore ad essa associato viene sovrascritto
con il nuovo valore; se key e` null viene lanciata IllegalArgumentException
*/
void insert(Comparable key, Object value);
/*
Rimuove dal dizionario l'elemento specificato dalla chiave key
Se la chiave non esiste viene lanciata DictionaryItemNotFoundException
*/
void remove(Comparable key);
/*
Cerca nel dizionario l'elemento specificato dalla chiave key
La ricerca per chiave restituisce soltanto il valore ad essa associato
Se la chiave non esiste viene lanciata DictionaryItemNotFoundException
*/
Object find(Comparable key);
}
class DictionaryItemNotFoundException extends RuntimeException {}
Exception in thread "main" java.lang.NullPointerException
at Rubrica.binarySearch(RubricaTester.java:135)
at Rubrica.remove(RubricaTester.java:124)
at Rubrica.insert(RubricaTester.java:107)
at RubricaTester.main(RubricaTester.java:30)
Exception in thread "main" java.lang.NullPointerException
at Rubrica.binarySearch(RubricaTester.java:135)
at Rubrica.remove(RubricaTester.java:124)
at Rubrica.insert(RubricaTester.java:107)
at RubricaTester.main(RubricaTester.java:30)
questo codice, qualcuno saprebbe dirmi dove sbaglio?
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;// nome e cognome del candidato, matricola, data, numero della postazione
public class RubricaTester
{ public static void main(String[] args)
{
if(args.length != 2)
{
System.out.println("uso : inserire nomi dei due file di testo");
System.exit(1);
}
Rubrica rub1 = new Rubrica();
Rubrica rub2 = new Rubrica();
try
{
Scanner in = new Scanner(new FileReader(args[0]));
while(in.hasNextLine())
{
Scanner linescan = new Scanner(in.nextLine());
linescan.useDelimiter(":");
while(linescan.hasNextLine())
{
String nome = in.next();
String numero = in.next();
rub1.insert(nome, numero);
}
in.close();
}
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
Scanner in = new Scanner(System.in);
boolean done = false;
while(!done)
{
System.out.println("inserire Q per uscire, altrimenti inserire nome da cercare");
String nome = in.next();
if(nome.equalsIgnoreCase("Q"))
{
done = true;
System.out.println("stampo la rubrica su file 2");
}
else
{
Object numero = rub1.find(nome);
rub2.insert(nome, numero);
rub1.remove(nome);
}
}
try
{
PrintWriter out = new PrintWriter(args[1]);
out.println(rub2);
out.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
}
}
class Rubrica implements Dictionary
{
public Rubrica()
{
v = new Pair[10];
vSize = 0;
}
public boolean isEmpty()
{
return vSize == 0;
}
public void makeEmpty()
{
vSize = 0;
}
//metodi di Rubrica ......da completare ......
public String toString()
{
String s = "";
for(int i = 0; i<vSize;i++)
s = s + v.getName() + ":" + v.getPhone() + "\n";
return s;
}
public void insert(Comparable key, Object value)
{
if(key == null) throw new IllegalArgumentException();
try
{
remove(key);
}
catch(DictionaryItemNotFoundException e)
{}
if (vSize == v.length) v = resize(2*v.length);
int j = vSize;
while(j > 0 && v[j-1].getName().compareTo((String) key) > 0)
{
v[j] = v[j-1];
j--;
}
v[j] = new Pair((String) key, (Long)value);
vSize++;
}
public void remove(Comparable key)
{
int pos = binarySearch(v, 0, vSize-1, key);
for(int i = pos; i<vSize-1; i++)
{
v = v[i+1];
}
vSize--;
}
private int binarySearch(Pair [] v, int from, int to, Comparable chiave)
{
int mid = (from+to)/2;
Comparable middle = v[mid].getName();
if(middle.compareTo(chiave) == 0) return mid;
else if(middle.compareTo(chiave) > 0)
return binarySearch(v, from, mid-1, chiave);
else
return binarySearch(v, mid+1, to, chiave);
}
public Object find(Comparable key)
{
int pos = binarySearch(v, 0, vSize-1, key);
if(pos<0) throw new DictionaryItemNotFoundException();
else
return v[pos].getPhone();
}
protected Pair [] resize(int newlength)
{
if(newlength<0) throw new IllegalArgumentException ();
Pair [] newObj = new Pair[newlength];
System.arraycopy(v, 0, newObj, 0, v.length);
return newObj;
}
private int vSize;
private Pair [] v;
// ..... da completare .........
//campi di esemplare di Rubrica ..... da completare ......
//classe privata Pair: non modificare!!
private class Pair
{ public Pair(String aName, long aPhone)
{ name= aName;
phone = aPhone;
}
public String getName()
{ return name; }
public long getPhone()
{ return phone; }
/*
Restituisce una stringa contenente
- la nome, "name"
- un carattere di separazione ( : )
- il numero telefonico, "phone"
*/
public String toString()
{ return name + " : " + phone; }
//campi di esemplare
private String name;
private long phone;
}
}
interface Dictionary
{
/*
verifica se il dizionario contiene almeno una coppia chiave/valore
*/
boolean isEmpty();
/*
svuota il dizionario
*/
void makeEmpty();
/*
Inserisce un elemento nel dizionario. L'inserimento va sempre a buon fine.
Se la chiave non esiste la coppia key/value viene aggiunta al dizionario;
se la chiave esiste gia' il valore ad essa associato viene sovrascritto
con il nuovo valore; se key e` null viene lanciata IllegalArgumentException
*/
void insert(Comparable key, Object value);
/*
Rimuove dal dizionario l'elemento specificato dalla chiave key
Se la chiave non esiste viene lanciata DictionaryItemNotFoundException
*/
void remove(Comparable key);
/*
Cerca nel dizionario l'elemento specificato dalla chiave key
La ricerca per chiave restituisce soltanto il valore ad essa associato
Se la chiave non esiste viene lanciata DictionaryItemNotFoundException
*/
Object find(Comparable key);
}
class DictionaryItemNotFoundException extends RuntimeException {}
Exception in thread "main" java.lang.NullPointerException
at Rubrica.binarySearch(RubricaTester.java:135)
at Rubrica.remove(RubricaTester.java:124)
at Rubrica.insert(RubricaTester.java:107)
at RubricaTester.main(RubricaTester.java:30)
Exception in thread "main" java.lang.NullPointerException
at Rubrica.binarySearch(RubricaTester.java:135)
at Rubrica.remove(RubricaTester.java:124)
at Rubrica.insert(RubricaTester.java:107)
at RubricaTester.main(RubricaTester.java:30)