Domanda java.lang.OutOfMemoryError

Stato
Discussione chiusa ad ulteriori risposte.

zotto

Utente Electrum
21 Settembre 2009
430
45
7
169
Ciao a tutti, il mio problema è il seguente: devo creare una classe eseguibile che mi permetta di memorizzare uno archivio studenti (con le relative classi da me scritte). è un paio di giorni però, che dopo aver sistemato i problemi di compliazione ho dei problemi in esecuzione ed in particolare, oggi, ho avuto il problema java.lang.OutOfMemoryError. Il codice delle classi è il seguente:

ProvaArchivio
Codice:
import java.util.Scanner;

public class ProvaArchivioStudenti
{
    public static void main(String[] args)
    {
        final int ECC_VALUE = 27;
        final String TERMINE = "end";
        Scanner in = new Scanner(System.in);
        ArchivioStudenti archivio = new ArchivioStudenti();
        
        System.out.println("***ARCHVIO STUDENTI***");
        while(true)
        {
            System.out.println("Matr, Nome, Prova, Voto, Prova, Voto...");
            String line = in.nextLine();
            
            if(line.equals(TERMINE))
                break;
            
            Scanner stud = new Scanner(line);
            String mat = stud.next();
            String nome = stud.next();
            Studente tempStudente = new Studente(mat, nome);
                
            while(true)
            {
                if(stud.hasNextLine())
                {
                    Scanner es = new Scanner(stud.nextLine());
                    String prova = es.next();
                    int voto = es.nextInt();
                    Esame ESAME = new Esame(prova, voto);
                    tempStudente.aggiungiEsame(ESAME);
                    es.close();
                }
                else
                    break;
            }
                archivio.aggiungi(tempStudente);
                stud.close();
        }
        
        ArchivioStudenti eccellenze = new ArchivioStudenti();
        ArchivioStudenti nuovoarchivio = new ArchivioStudenti();
        
        while(!archivio.isEmpty())
        {
            Studente tempstud = archivio.rimuovi();
            nuovoarchivio.aggiungi(tempstud);
            if(tempstud.media() > ECC_VALUE)
                eccellenze.aggiungi(tempstud);
        }
        if(archivio.isEmpty())
            System.out.println("Non ci sono studenti nell'archivio");
        else
        {
            if(eccellenze.isEmpty())
                System.out.println("Non ci sono eccellenze");
            else
                System.out.println("Le eccellenze sono: "+eccellenze);
        }
    
        in.close();
    }
}

ArchivioStudenti
Codice:
import java.util.Scanner;

public class ArchivioStudenti
{
    private Studente[] s;
    private int sSize;
    
    public ArchivioStudenti()
    {
        s = new Studente[1];
        sSize = 0;
    }
    
    public ArchivioStudenti(Scanner reader)
    {
        while(reader.hasNextLine())
        {
            String line = reader.nextLine();
            if(line == null)
                throw new IllegalArgumentException();
            
            Scanner tok1 = new Scanner(line);
            String matr = reader.next();
            String nome = reader.next();
            
            Studente tempStud = new Studente(matr, nome);
            
            while(tok1.hasNext())
            {
                Scanner tok2 = new Scanner(tok1.next());
                Esame exam = new Esame(tok2.next(), tok2.nextInt());
                tempStud.aggiungiEsame(exam);
                tok2.close();
            }
            
            tok1.close();
        }
    }
    
    public void aggiungi(Studente aStud)
    {
        if(aStud == null)
            throw new IllegalArgumentException();
        
        if(sSize >= s.length)
            s = resize(s);
    
        s[sSize] = aStud;
        sSize++;
    }
    
    public boolean isEmpty()
    {
        return sSize == 0;
    }
    
    public Studente rimuovi()
    {
        if(isEmpty())
            return null;
        
        Studente temp = s[0];
        temp = s[0];
        
        for(int i = 0; i < sSize-1; i++)
            s[i] = s[i+1];
            
        return temp;
    }
    
    public String toString()
    {
        String descrizione = "";
        for(int i = 0; i < sSize; i++)
            descrizione = descrizione+"\n"+s[i];
        
        return descrizione;
    }    
    
    private Studente[] resize(Studente[] vector)
    {
        Studente[] temp = new Studente[2 * vector.length];
        for(int i = 0; i < sSize; i++)
            temp[i] = vector[i];
        
        return temp;
    }
}

Studente
Codice:
public class Studente
{
    private Esame e[];
    private int eSize;
    private String nome;
    private String matricola;
    
    public Studente(String n, String m)
    {
        if(n == null || m == null)
            throw new IllegalArgumentException();
        
        nome = n;
        matricola = m;
        e = new Esame[1];
        eSize = 0;
    }
    
    public void aggiungiEsame(Esame exam)
    {
        if(exam == null)
            throw new IllegalArgumentException();
        
        if(eSize >= e.length)
            resize();
        
        e[eSize] = exam;
        eSize++;
    }
    
    public boolean isEmpty()
    {
        return eSize == 0;
    }
    
    public double media()
    {
        int somma = 0;
        double m;
        if(isEmpty())
            m = 0;
        else
        {
            for(int i = 0; i < eSize; i++)
                somma += e[i].getVoto();
            
            m = somma/(numeroEsami());
        }
        return m;
    }
    
    public int numeroEsami()
    {
        return eSize;
    }
    
    public String toString()
    {
        String descrizione = "Matr: "+matricola+", Nome: "+nome+"\nn° "+numeroEsami()+" esami sostenuti";
        
        for(int i = 0; i < eSize; i++)
            descrizione = descrizione+"\n"+e[i].toString();
        
        return descrizione;
    }
    
    private void resize()
    {
        Esame[] temp = new Esame[2*e.length];
        for(int i = 0; i < eSize; i++)
            temp[i] = e[i];
        
        e = temp;
    }
}

Esame
Codice:
public class Esame
{
    private String prova;
    private int voto;
    
    public Esame(String p, int v)
    {
        if(p == null || v <= 0)
            throw new IllegalArgumentException();
        
        prova = p;
        voto = v;
    }
    
    public int getVoto()
    {
        return voto;
    }
    
    public String toString()
    {
        return prova+": "+voto;
    }
}

riuscite a salvarmi da questo baratro? L'errore che mi da è il seguente:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at ArchivioStudenti.resize(ArchivioStudenti.java:82)
at ArchivioStudenti.aggiungi(ArchivioStudenti.java:46)
at ProvaArchivioStudenti.main(ProvaArchivioStudenti.java:50)

io ho ricontrollato ma non mi sembra di aver sbagliato nulla, eppure l'errore c'è...dove?
 
Codice:
    public Studente rimuovi()
    {
        if(isEmpty())
            return null;
        
        Studente temp = s[0];
        temp = s[0];

        int i;
        for(i = 0; i < sSize-1; i++)
            s[i] = s[i+1];

        s[i+1] = null; // libero un po' di spazio sulla ram
        sSize--; // ho rimosso un elemento, la dimensione si abbassa di uno
            
        return temp;
    }

Ti faccio presente che gli array hanno dimensione fissa, quindi questo remove non fa un vero e proprio remove. Per fare le cose per bene o si ricrea tutto l'array come hai fatto con resize, o ancora meglio si usa un altra struttura dati più appropriata tipo Vector o List.
 
  • Mi piace
Reazioni: zotto
Ultima modifica:
Ok, perfetto. Questo è parte del problema, grazie mille. Ora però ne ho un altro. La compilazione è OK, l'esecuzione è OK, ma quando io faccio per inserire più esami (da input i dati che devo inserire sono: matricola, nome, prova1, voto1, prova2, voto2, prova3, voto3 ecc...) è come se mi leggesse solo il primo e gli altri non me li conta...perchè?

Edit: risolto. Lo scanner che dava fastidio era lo scanner es.
 
Stato
Discussione chiusa ad ulteriori risposte.