This ClassCastException is driving me crazy.
When invoking the method insert() I get this:
Exception in thread "main" java.lang.ClassCastException:
java.lang.String cannot be cast to Rubrica$Pair
Hope anybody can help without loosing much time :).
class Rubrica implements Dictionary
{
private Object[] v;
private int vSize;
public static int INITSIZE = 1;
/*
verifica se il dizionario contiene almeno una coppia chiave/valore
*/
public Rubrica()
{
v = new Object[INITSIZE];
makeEmpty();
}
private Object[] resize(Object[] v, int length)
{
Object[] newv = new Object[length * 2];
System.arraycopy(v, 0, newv, 0, length);
return newv;
}
/*
svuota il dizionario
*/
public void makeEmpty()
{
for(int i = 0; i < v.length; i++)
v[i] = new Pair(null, -1);
vSize = 0;
}
/*
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
*/
private Object[] insertionSort(Object[] v, int vSize)
{
for(int i = 0; i < vSize; i++)
{
Comparable temp = ((Pair)v[i]).getName();
int j;
for(j = i; j > 0 && temp.compareTo(((Pair)v[j - 1]).getName()) < 0; j--)
v[j] = v[j - 1];
v[j] = temp;
}
return v;
}
public void insert(Comparable key, Object value)
{
if(vSize == v.length)
v = resize(v, vSize);
if(key.equals(null))
throw new IllegalArgumentException();
int index = binaryKeyIndexSearch(v, vSize, key);
if(index == -1)
v[vSize++] = new Pair((String)key, (long)value);
else
v[index] = new Pair((String) key, (long)value);
v = insertionSort(v, vSize);
}
/*
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
*/
private int binaryKeyIndexSearch(Object[] v, int vSize, Comparable value)
{
return binKeySearch(v, 0, vSize - 1, value);
}
private int binKeySearch(Object[] v, int from, int to, Comparable value)
{
if(from > to)
return -1;
int mid = (from + to) / 2;
Comparable midValue = ((Pair)v[mid]).getName(); //errore
if(value.compareTo(midValue) == 0)
return mid;
else if(value.compareTo(midValue) < 0)
return binKeySearch(v, from, mid - 1, value);
else
return binKeySearch(v, mid + 1, to, value);
}
//classe privata Pair: DO NOT MODIFY!!
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;
}
}
The problem is in this method:
private Object[] insertionSort(Object[] v, int vSize)
{
for(int i = 0; i < vSize; i++)
{
Comparable temp = ((Pair)v[i]).getName();
int j;
for(j = i; j > 0 && temp.compareTo(((Pair)v[j - 1]).getName()) < 0; j--)
v[j] = v[j - 1];
v[j] = temp;
}
return v;
}
Look closely, you write to temp the name of v[i] and then assign temp to v[j]so now you have String instead of Pair in v[j].
You could get rid of ClassCastExceptions if you have used the correct type for your array, which is Pair[]. I mean if you swap all Object[] to Pair[] you will see this kind of incorrect assignment in compile time, not in runtime.
Related
My Java teacher asked for an exponentiation exercise. He wants us to use bucle (for or while) for this, not math functions. Well, I have wrote the code but I realised that if I use the constructor, it works fine. But if I try to choose the value from a setter it always return 1.
package potencia;
public class potencia {
private int base = 0;
private int exponente = 0;
private int resultado = 1;
public int getBase() {
return base;
}
public int getExponente() {
return exponente;
}
public int getResultado() { // De la variable resultado solo creamos el getter pues no nos interesa "settear" el resultado, sino que lo calcule el programa
return resultado;
}
public void setBase(int base) {
this.base = base;
}
public void setExponente(int exponente) {
this.exponente = exponente;
}
public potencia(){}
public potencia(int base, int exponente){
this.base = base;
this.exponente = exponente;
if (exponente == 0) { // Si la variable EXPONENTE se inicializa a 0, la variable RESULTADO será 1
this.resultado = 1;
}
if (base == 1) { // Si la variable BASE se inicializa a 1, la variable RESULTADO será 1
this.resultado = 1;
}
if (base > 1 && exponente > 0) {
for (int i=1; i<=exponente; i++) {
resultado *= base; // Uso de operador de asignación "*=", significa lo mismo que "resultado = resultado * base"
}
}
}
public void muestraResultado () {
System.out.println("El resultado es " + resultado);
}
}
The next example works perfectly:
package potencia;
public class calcularPotencia {
public static void main(String[] args) {
potencia MiCalculadora = new potencia(5, 4);
MiCalculadora.muestraResultado();
System.out.println(MiCalculadora.getResultado());
}
}
It returns: "El resultado es 625
625"
But when I use setters:
package potencia;
public class calcularPotencia {
public static void main(String[] args) {
potencia MiCalculadora = new potencia();
MiCalculadora.setBase(5);
MiCalculadora.setExponente(4);
MiCalculadora.muestraResultado();
System.out.println(MiCalculadora.getResultado());
}
}
Returns: "El resultado es 1
1"
I supposed that something went wrong with setters and getters, so I deleted them and inserted them again. It was resultless, the fail persists. I also have checked methods and classes, but I can not see the mistake.
Thank you all for your help, mates.
You aren't calculating the result when you use the setter.
You could remove the member variable for resultado and calculate it in the getter:
public int getResultado() { // De la variable resultado solo creamos el getter pues no nos interesa "settear" el resultado, sino que lo calcule el programa
if (exponente == 0) { // Si la variable EXPONENTE se inicializa a 0, la variable RESULTADO será 1
return 1;
}
if (base == 1) { // Si la variable BASE se inicializa a 1, la variable RESULTADO será 1
return = 1;
}
if (base > 1 && exponente > 0) {
int resultado = 1;
for (int i=1; i<=exponente; i++) {
resultado *= base; // Uso de operador de asignación "*=", significa lo mismo que "resultado = resultado * base"
}
return resultado;
} else {
throw new RuntimeException("base or exponente < 0");
}
}
You could also keep the field and cache the result after calculating it. initialize it to -1, since that is not a valid result. Then do:
public int getResultado() {
if (resultado < 0) {
calculateResultado();
}
return resultado;
}
Where the calculate method will be similar to what you are doing in the constructor (in fact you can call it from the constructor).
I have to write a program for search the correspondent of "Nomi" by entering the "Prezzo" value, but when I do the binary search, I get the error:
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 7 on a binary search
Here is the code:
import java.io.*;
public class Blackfriday {
public static int ricercaBinaria(Double prezzi[], Double chiave) {
int inf = 0, sup = prezzi.length - 1;
while (inf <= sup) {
int med = (inf + sup) / 2;
if (prezzi[med] == chiave)
return med;
if (prezzi[med] < chiave)
inf = med + 1;
else
sup = med - 1;
}
return -1;
}
public static void main(String args[]) {
try {
InputStreamReader isr;
BufferedReader br;
double cerca;
isr = new InputStreamReader(System.in);// abilitato la lettura da tastiera
br = new BufferedReader(isr);// abilitato la lettura un rigo per volta
String nomi[] = { "picones", "vinile di Speranza", "Laurea", "King Mufasa", "Pentium Gold",
"Aethey Wind breaker ORO", "HeelCompletoSpaic1we" };
Double prezzi[] = { 2.0, 13.50, 23.0, 99.50, 120.0, 75.20, 999.99 };
System.out.println("Quanto vuoi spendere?");
String xStringa = br.readLine();// ricevo la digitazione in String
cerca = Double.parseDouble(xStringa);// Trasformo la String in double
System.out.println("Puoi comprare: " + nomi[ricercaBinaria(prezzi, cerca)]);
} catch (Exception e) {
System.out.println(e);
}
}
}
Here is a functioning code :
Always display the stackTrace in case of Exception catch, it will help for the debug
if you manage Objects "Double" and try to compare them with ==, it will compare their reference (address in memory) and not the value contained inside of them. So use simple type double OR compare objects with equals() method.
public class Blackfriday {
public static int ricercaBinaria(double prezzi[], Double chiave) {
int inf = 0, sup = prezzi.length - 1;
while (inf <= sup) {
int med = (inf + sup) / 2;
if (prezzi[med] == chiave)
return med;
if (prezzi[med] < chiave)
inf = med + 1;
else
sup = med - 1;
}
return -1;
}
public static void main(String args[]) {
try {
InputStreamReader isr;
BufferedReader br;
double cerca;
isr = new InputStreamReader(System.in);//abilitato la lettura da tastiera
br = new BufferedReader(isr);//abilitato la lettura un rigo per volta
String nomi[] = {"picones", "vinile di Speranza", "Laurea", "King Mufasa", "Pentium Gold", "Aethey Wind breaker ORO", "HeelCompletoSpaic1we"};
double prezzi[] = {2.0, 13.50, 23.0, 99.50, 120.0, 75.20, 999.99};
System.out.println("Quanto vuoi spendere?");
String xStringa = br.readLine();//ricevo la digitazione in String
cerca = Double.parseDouble(xStringa);//Trasformo la String in double
System.out.println("Puoi comprare: " + nomi[ricercaBinaria(prezzi, cerca)]);
} catch (Exception e) {
e.printStackTrace();
}
}//main
}//classe
There are at least two problems with your code.
A precondition of ricercaBinaria is that the underlying data array is ordered. You are using unordered data in your test
ricercaBinaria will return the index in which a given argument (Double) is located in your array. If it is not found, then -1 will be returned. As such, you need to check the returned index to verify that it is indeed positive before using it as an index array - otherwise the method will fail with the exception you are seeing
Beware of Double equality but that is for another question.
I wrote a program who simulate a card game.
I choose some values for the colors of the card and some values for values of the cards.
I wrote my own Exception, is CarteException.java who have two child, CarteValeurException.java and CarteCouleurException.java depending on the type of Exception at the initialisation of a Carte
If the value of the color is not in 1 or 4, that will be throw an Exception of CarteCouleurExeception.java and same for the other Exception, if the value are not 1 or in 7 and 13, that will be throw a CarteValeurException.java
This is my code for the class Carte.java :
public Carte(int coul, int val) throws CarteException {
if(coul < 1 || coul > 4) {
throw new CarteCouleurException("Erreur d'initialisation lors de la création d'une carte.",coul);
}
if(val < 1 || (val > 1 && val < 7) || val > 13) {
throw new CarteValeurException("Erreur d'initialisation lors de la création d'une carte.",val);
}
this.couleur = coul;
this.valeur = val;
}
This is the code for the Class CarteException.java :
public class CarteException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
protected String message;
protected CarteException() {
this.message = "";
}
public CarteException(String chaine) {
this.message = chaine;
}
public String getMessage() {
return (this.message + " : Erreur non spécifiée de valeur ou de couleur de carte.");
}
}
And finally, the initialisation of a card in Belote.java :
package TP.TP6.Exercice2;
import java.util.Random;
import java.util.Vector;
import java.util.Stack;
import TP.TP5.Exercice1.Question4.Carte;
public class Belote {
private Stack<Carte> tasDistibution;
private Vector<Stack<Carte>> mainsJoueurs;
private Vector<Stack<Carte>> plisJoueurs;
public Belote() {
this.tasDistibution = new Stack<Carte>();
this.mainsJoueurs = new Vector<Stack<Carte>>(4);
this.plisJoueurs = new Vector<Stack<Carte>>(4);
for(int i = 0 ; i < 4 ; i++) {
this.mainsJoueurs.add(i, new Stack<Carte>());
this.plisJoueurs.add(i, new Stack<Carte>());
}
}
private void initialiserTasDistribution() throws CarteException {
for (int i = 0; i <= 5; i++) {
for (int j = 1; j <= 13; j++) {
try {
//Initialisation right here
this.tasDistibution.push(new Carte(i,j));
}
catch(CarteException CE) {
System.err.println("Erreur : " + CE);
}
}
}
}
private void couper() {
Stack<Carte> tas1 = new Stack<Carte>();
Stack<Carte> tas2 = new Stack<Carte>();
Random r = new Random();
int coupe = 1 + r.nextInt(33 - 1);
for (int i = 0; i < coupe; i++) {
Carte carte = this.tasDistibution.peek();
this.tasDistibution.pop();
tas1.push(carte);
}
while (tasDistibution.isEmpty() == false) {
Carte carte = this.tasDistibution.peek();
this.tasDistibution.pop();
tas2.push(carte);
}
while (tas1.isEmpty() == false) {
Carte carte = tas1.peek();
tas1.pop();
this.tasDistibution.push(carte);
}
while (tas2.isEmpty() == false) {
Carte carte = tas2.peek();
tas2.pop();
this.tasDistibution.push(carte);
}
}
private void melanger(int nbMelange) {
Carte tabcarte[] = new Carte[32];
for (int i = 0; i < tabcarte.length; i++) {
Carte cartesommet = this.tasDistibution.peek();
this.tasDistibution.pop();
tabcarte[i] = cartesommet;
}
for (int i = 0; i < nbMelange; i++) {
Random r = new Random();
int pos1 = 1 + r.nextInt(32 - 1);
int pos2 = 1 + r.nextInt(32 - 1);
if (pos1 == pos2) {
System.out.println("Pas de chance");
} else {
Carte temp;
temp = tabcarte[pos1];
tabcarte[pos1] = tabcarte[pos2];
tabcarte[pos2] = temp;
}
}
for (int i = 0; i < tabcarte.length; i++) {
Carte carte = tabcarte[i];
this.tasDistibution.push(carte);
}
}
private void donnerCartesAJoueur(int nbcartedonnes, int numjoueur) {
for (int i = 0; i < nbcartedonnes; i++) {
Carte carte = this.tasDistibution.peek();
this.tasDistibution.pop();
Stack<Carte> stack = this.mainsJoueurs.get(numjoueur);
stack.push(carte);
this.mainsJoueurs.set(numjoueur, stack);
}
}
private void distribuer() {
for (int i = 0; i < 4; i++) {
this.donnerCartesAJoueur(3, i);
}
for (int i = 0; i < 4; i++) {
this.donnerCartesAJoueur(2, i);
}
for (int i = 0; i < 4; i++) {
this.donnerCartesAJoueur(3, i);
}
for (int i = 0; i < 4; i++) {
System.out.println("\n\nDistribution pour joueur : " + (i+1) + " \n\nMain du joueur : " + (i+1));
System.out.println(this.mainsJoueurs.get(i).toString());
}
}
private void assemblerPlisJoueur() {
for (int i = 0; i < 4; i++) {
while (this.plisJoueurs.get(i).isEmpty() == false) {
Carte carte = this.plisJoueurs.get(i).peek();
Stack<Carte> stack = this.plisJoueurs.get(i);
stack.pop();
this.plisJoueurs.set(i, stack);
this.tasDistibution.push(carte);
}
}
}
private void preparerPremiereManche() throws CarteException {
try {
this.initialiserTasDistribution();
}
catch(CarteException CE) {
System.err.println("Erreur d'initilisation du tas à distribuer.");
throw CE;
}
this.melanger(32);
this.couper();
this.distribuer();
}
private void preparerMancheSuivante() {
this.assemblerPlisJoueur();
this.couper();
this.distribuer();
}
private void jouerPli() {
Stack<Carte> tasIntermediaire = new Stack<Carte>();
for (int i = 0; i < 4; i++) {
Carte carte = this.mainsJoueurs.get(i).peek();
Stack<Carte> stack = this.mainsJoueurs.get(i);
stack.pop();
this.mainsJoueurs.set(i, stack);
tasIntermediaire.push(carte);
}
Random r = new Random();
int gagnant = 0 + r.nextInt(4 - 0);
System.out.println("Le joueur " + (gagnant+1) + " a gagné ce pli");
for (int i = 0; i < 4; i++) {
Carte carte = tasIntermediaire.peek();
tasIntermediaire.pop();
Stack<Carte> stack = this.plisJoueurs.get(gagnant);
stack.push(carte);
this.plisJoueurs.set(gagnant, stack);
}
System.out.println("Pli du joueur " + (gagnant+1));
System.out.println(this.plisJoueurs.get(gagnant).toString());
}
private void jouerManche(int nbPlis) {
for (int i = 1; i <= nbPlis; i++) {
System.out.println("\n\nPli numéro : " + i);
this.jouerPli();
}
this.preparerMancheSuivante();
}
public void jouerPartie(int nbManches) throws CarteException {
try {
this.preparerPremiereManche();
}
catch(CarteException CE) {
System.err.println("Erreur d'initialisation de la première manche");
throw CE;
}
for (int i = 1; i <= nbManches; i++) {
System.out.println("\n\nManche numéro : " + i);
this.jouerManche(8);
}
System.out.println("Jeu terminé");
}
}
The problem is in Belote.java, Eclipse send me an error like this here catch(CarteException CE) :
Unreachable catch block for CarteException. This exception is never thrown from the try statement body
But i put right : public Carte(int coul, int val) throws CarteException in the first class so don't understand the problem.
I wrote the class CarteValeurException.java and CarteCouleurException.java but that is practically the same than CarteException.java so that's why i don't put the code of the class right here.
Thank you for your help !
You have to throw or catch your exception.
The signature of your method is:
private void initialiserTasDistribution() throws CarteException
So when the exception is trowed from the constructor in Carte is re-throwed by your method. Thus the catch is unreachable. You have to chose the best approach for your code.
Here a set of rules of thumb: Throws or try+catch
Your problem is that your method is declared to propagate the exception higher up the call stack:
private void initialiserTasDistribution() throws CarteException {
This means that whoever calls the method initialiserTasDistribution will have to handle this exception instead (or do the same and propagate it even higher). It is a form of deferring responsibility. It's saying "I'm not going to handle this error, someone else can".
This directly contradicts the implementation of your method because you do in fact handle the error.
try {
this.tasDistibution.push(new Carte(i,j));
}
catch(CarteException CE) {
System.err.println("Erreur : " + CE);
}
If you change your method signature to remove the throws declaration, you should be okay:
private void initialiserTasDistribution() {
Im trying to read my binary file that i created for a country with at least 5 gold medals. When im trying to read the file and then displaying it. it shows:
?????††††††††††††††???†††††††††††††††† 27 27 38 92. It displays part of the information. This is my code at the moment:
import java.io.*;
import java.util.*;
class Pays
implements Comparable<Pays>
{
private String nom;
private int gold;
private int silver;
private int bronze;
private int sum;
public Pays(String nom,int gold,int silver, int bronze,int sum)
{
this.nom = nom;
this.gold=gold;
this.silver=silver;
this.bronze=bronze;
this.sum=sum;
}
public int compareTo(Pays autre) {
return nom.toUpperCase().trim().compareTo( autre.nom.toUpperCase().trim() );
}
public String getNom(){ return nom; }
public int getGold(){ return gold; }
public int getSilver(){ return silver; }
public int getBronze(){ return bronze; }
public int getSum(){return sum;}
public boolean equals(Object obj)
{
if (this == obj)
return true;
else
if ( ! (obj instanceof Pays))
return false;
else
{
Pays autre = (Pays) obj;
return nom.trim().equalsIgnoreCase(autre.nom.trim());
}
}
public void ecrire(DataOutputStream aCreer)
throws IOException
{
aCreer.writeBytes(nom);
aCreer.writeInt(gold);
aCreer.writeInt(silver);
aCreer.writeInt(bronze);
aCreer.writeInt(sum);
}
}
class numba3{
static LinkedList<Pays> lireCreer(String nomFichier)
throws IOException
{ LinkedList<Pays> liste = new LinkedList<Pays>();
boolean existeFichier = true ;
FileReader fr = null;
try {
fr = new FileReader (nomFichier) ;
}
catch ( java.io.FileNotFoundException erreur) {
System.out.println("Probleme d'ouvrir le fichier " +
nomFichier);
existeFichier = false ;
}
if (existeFichier) {
BufferedReader entree = new BufferedReader(fr);
boolean finFichier = false ;
while ( !finFichier ) {
String uneLigne = entree.readLine();
if (uneLigne == null)
finFichier = true ;
else {
String nom = uneLigne.substring(0, 38);
int gold = Integer.parseInt(uneLigne.substring(38,43).trim());
int silver = Integer.parseInt(uneLigne.substring(43,48).trim());
int bronze = Integer.parseInt(uneLigne.substring(48).trim());
int sum=gold+silver+bronze;
liste.add(new Pays(nom,gold,silver,bronze,sum));
}
}
entree.close();
}
return liste;
}
static void afficher(LinkedList<Pays> liste,int numero){
for(int i=0;i<numero;i++){
System.out.printf("%30s %10d %10d %10d %10d\n",liste.get(i).getNom(),liste.get(i).getGold(),liste.get(i).getSilver(),liste.get(i).getBronze(),liste.get(i).getSum());
}
}
public static void quickSort(LinkedList<Pays> liste, int low, int high) {
if (liste.isEmpty() == true || liste.size()== 0)
return;
if (low >= high)
return;
int middle = low + (high - low) / 2;
int pivot = liste.get(middle).getSum();
int i = low, j = high;
while (i <= j) {
while (liste.get(i).getSum() > pivot) {
i++;
}
while (liste.get(j).getSum() < pivot) {
j--;
}
if (i <= j) {
Pays temp=liste.get(i);
liste.set(i,liste.get(j));
liste.set(j,temp);
i++;
j--;
}
}
if (low < j)
quickSort(liste, low, j);
if (high > i)
quickSort(liste, i, high);
}
static void afficherPays(LinkedList<Pays>liste,String nom){
for(int i=0;i<liste.size();i++){
if(liste.get(i).equals(new Pays(nom, 0, 0, 0,0))){
System.out.printf("%30s %10d %10d %10d %10d\n",liste.get(i).getNom(),liste.get(i).getGold(),liste.get(i).getSilver(),liste.get(i).getBronze(),liste.get(i).getSum());
}
}
}
static void creerBinaire(LinkedList<Pays> liste, int gold,String nomBinaire)
throws IOException
{
DataOutputStream aCreer = new DataOutputStream
( new FileOutputStream(nomBinaire));
for (int i = 0; i < liste.size(); i++)
{
if ( liste.get(i).getGold() >= gold)
liste.get(i).ecrire(aCreer);
}
aCreer.close();
System.out.println("\nOn vient de creer le fichier binaire : " + nomBinaire);
}
static LinkedList<Pays> readBinaire(String nomALire)
throws IOException{
System.out.println("On lit le fichier binaire du nom " + nomALire);
LinkedList<Pays> unVect = new LinkedList<Pays> ();
DataInputStream aLire = new DataInputStream
( new FileInputStream(nomALire));
boolean finFichier = false ;
String nom = "";
int rang = 0;
while ( ! finFichier ) {
try {
for (int i = 0; i < 19; i++)
nom += aLire.readChar();
} catch ( EOFException e ) {
finFichier = true;
}
if (!finFichier) {
int gold=aLire.readInt(),
silver=aLire.readInt(),
bronze=aLire.readInt(),
sum=aLire.readInt();
Pays pers = new Pays(nom, gold, silver, bronze, sum);
unVect.add(pers);
}
}
aLire.close();
return unVect;
}
public static void main(String[]args)throws IOException
{
LinkedList<Pays> liste = lireCreer("Olym_ete.txt");
System.out.println("");
System.out.println("1)Determine et afficher les 5 premier pays qui ont gagne plus de medailles en totale: ");
quickSort(liste,0, liste.size()-1);
afficher(liste,5);
System.out.println("");
System.out.println("2)Afficher les information des pays FRANCE, JAPON, ESPAGNE: ");
afficherPays(liste,"FRANCE");
afficherPays(liste,"JAPON");
afficherPays(liste,"ESPAGNE");
System.out.println("");
System.out.println("3)Cree a partir de la liste un fichier binaire qui ont gagne au moins de 5 medailles en or: ");
creerBinaire(liste, 5,"AtLeastFiveGold.bin");
LinkedList<Pays> plusKeFive = readBinaire("AtLeastFiveGold.bin");
System.out.println(plusKeFive.size());
System.out.printf("%30s %10d %10d %10d %10d\n",plusKeFive.get(1).getNom(),plusKeFive.get(1).getGold(),plusKeFive.get(1).getSilver(),plusKeFive.get(1).getBronze(),plusKeFive.get(1).getSum());
}
}
I can't figure out how to get rid of the symbols. I suspect that i have made a mistake while reading the binary file in readBinaire() but i cannot seem to figure out where i went wrong.
A char in Java is a 16-bit Unicode character. A "byte" is an 8-bit byte.
You're using writeBytes to write a string as 38 bytes, but readChar to read them back in as 19 characters. writeBytes will write the low order 8 bits of each 16-bit character of the string. So if your string was "ABCD", the bytes it would write are
0x41 0x42 0x43 0x44
Then when you read them back in as 16-bit characters, it will read the first two characters either as
0x4142 0x4344
or
0x4241 0x4443
depending on how the byte ordering is handled. Either way, instead of "ABCD", you will get some strange Unicode characters whose code points are U+4142 or U+4241, and U+4344 or U+4443. (They are probably not displaying correctly.)
I'm writing a program that could manage universitary students with courses and subjects. The problem of the class I'm showing you is that when the method
public CorsoStudi(String unNomeCorso, ArrayList unElencoMaterie, int unIdCorso)
is called it throws a NullPointerException at line elencoEsamiDati.add(q);. So, probably the problem is with this line and the line after this:
EsameDato q = new EsameDato(unElencoMaterie.get(contatore), 0);
Eclipse doesn't advice error on writing code.
Creatore.java
import java.util.ArrayList;
import java.util.Scanner;
public class Creatore {
int counterStudenti = 0;
int counterMaterie = 0;
int counterCorsi = 0;
//int counterEsami = 0;
ArrayList<Studente> listaStudenti = new ArrayList<Studente>();
ArrayList<Materia> listaMaterie = new ArrayList<Materia>();
ArrayList<CorsoStudi> listaCorsi = new ArrayList<CorsoStudi>();
//ArrayList<EsameDato> listaEsami = new ArrayList<EsameDato>();
public void iscriviStudente(String nomeStudente, String cognomeStudente, CorsoStudi corsoStudente){
listaStudenti.add(new Studente(nomeStudente, cognomeStudente, counterStudenti, corsoStudente));
counterStudenti++;
}
public void reimmatricolaStudente(int unaMatricola, int unIdCorso){
int c = 0;
CorsoStudi unCorso = null;
for(c = 0; c < listaCorsi.size(); c++){
if(listaCorsi.get(c).getIdCorso() == unIdCorso)
unCorso = listaCorsi.get(c);
}
int contatore = 0;
for(contatore = 0; contatore < listaStudenti.size(); contatore++)
if(listaStudenti.get(contatore).getMatricola() == unaMatricola){
iscriviStudente(listaStudenti.get(contatore).getNome(), listaStudenti.get(contatore).getCognome(), unCorso);
rimuoviStudente(unaMatricola);
};
}
public void rimuoviStudente(int matricola){
int contatore;
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getMatricola() == matricola)
listaStudenti.remove(contatore);
}
}
public Materia creaMateria(String nomeMateria, int crediti){
listaMaterie.add(new Materia( nomeMateria, crediti, counterMaterie));
counterMaterie++;
return listaMaterie.get(counterMaterie - 1);
}
public void creaCorsoStudi(String nomeCorso, ArrayList<Materia> materieCorso){
CorsoStudi q = new CorsoStudi( nomeCorso, materieCorso, counterCorsi);
listaCorsi.add(q);
counterCorsi++;
}
public ArrayList<Studente> cercaStudente(int opzione, String pattern){
int contatore = 0;
ArrayList<Studente> listaRicercati = new ArrayList<Studente>();
//opzione 1 = ricerca per nome
if(opzione == 1)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getNome().equalsIgnoreCase(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
//opzione 2 = ricerca per cognome
if(opzione == 2)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getCognome().equalsIgnoreCase(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
//opzione 3 = ricerca per matricola
if(opzione == 3)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getMatricola() == Integer.parseInt(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
//opzione 4 = ricerca per corsoStudi
if(opzione == 4)
for(contatore = 0; contatore < listaStudenti.size(); contatore++){
if(listaStudenti.get(contatore).getCorsoStudi().getIdCorso() == Integer.parseInt(pattern))
listaRicercati.add(listaStudenti.get(contatore));
};
return listaRicercati;
}
public Materia materiaDaId(int id){
int c = 0;
Materia materiaDaRitornare = null;
for(c = 0; c < listaMaterie.size(); c++){
if(listaMaterie.get(c).getIdMateria() == id)
materiaDaRitornare = listaMaterie.get(c);
}
return materiaDaRitornare;
}
}
CorsoStudi.java
import java.util.ArrayList;
import java.util.Scanner;
public class CorsoStudi {
private String nomeCorso;
private int idCorso;
private ArrayList<Materia> elencoMaterie;
private ArrayList<EsameDato> elencoEsamiDati;
public CorsoStudi(String unNomeCorso, ArrayList<Materia> unElencoMaterie, int unIdCorso){
nomeCorso = unNomeCorso;
elencoMaterie = unElencoMaterie;
idCorso = unIdCorso;
int contatore = 0;
//EsameDato q = null;
for(contatore = 0; contatore < unElencoMaterie.size(); contatore++){
EsameDato q = new EsameDato(unElencoMaterie.get(contatore), 0);
elencoEsamiDati.add(q);
};
}
public String getNomeCorso(){
return nomeCorso;
}
public int getIdCorso(){
return idCorso;
}
public ArrayList<Materia> getElencoMaterie(){
return elencoMaterie;
}
public ArrayList<EsameDato> getElencoEsamiDati(){
return elencoEsamiDati;
}
public String toString(){
String s = "";
s = s + "Ecco le materie di questo Corso di Studi:\n";
int c = 0;
for(c= 0; c < elencoMaterie.size(); c++){
s = s + elencoMaterie.get(c).getIdMateria() + " ";
s = s + elencoMaterie.get(c).getNomeMateria() + " (";
s = s + elencoMaterie.get(c).getCrediti() + " crediti)\n";
}
return s;
}
}
You have not initialized the field elencoEsamiDati therefore the actual value is null.
Before adding elements to an ArrayList you need to create it:
private ArrayList<EsameDato> elencoEsamiDati = new ArrayList<EsameDato>();
You may also need to initialize other fields as well.
BTW it is not a good idea to use your own language when programming, use English instead.