help me for debug this code

T

TC

Some one can help me for debug this code?
Is a program that read a text file and create a tree of the word.
The size of text file is 1.5 mb.
with a small text file the program is ok, but with a large file
I have a problem with stack.

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author Marcello Cinesi
* @version 1.0
*/
public class Albero {

//Struttura Albero
public String parola;
public int freq;
public Albero d;
public Albero s;

public Albero()
{
this.parola="";
this.freq=0;
this.d=null;
this.s=null;
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public Albero(String Parola, int Freq)
{
this.parola = Parola;
this.freq = Freq;
this.d = null;
this.s = null;
}

private void jbInit() throws Exception {
}
}

/**
* <p>Title: </p>
*
* <p>Description: Programma che legge file di testo, ne estrapola le
parole e ne conta le occorrenze.
* In fine stampa la lista delle parole trovate in ordine alfabetico ed
in ordine decrescente di frequenza.</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author Marcello Cinesi
* @version 1.0
*/

import java.io.*;
import java.lang.Character.*;
import java.lang.String;


public class Testo
{
public Testo()
{

}
//albero ordinato per Alfabeto
static Albero treeA;
//albero ordianto per Frequenze
static Albero treeF;
//contatore per la stampa delle parole
static int count;

public static void main(String[] args)
{
File pf;
FileInputStream fs; //Canale di input del file
try
{

//Canale di lettura dati
InputStreamReader Tast;
//Buffer di lettura
BufferedReader Input;
//Dichiarazione della variabile nomefile
String NomeFile;
String in = new String();
//Variabile per l'acquisizione dell'input del menu
char Scelta;

System.out.println("Avvio del programma");
System.out.println("Inserire il nome del file di testo che si
desidera utilizzare (Esempio: Testo.txt)");

//Creazione del buffer per la lettura dell'input da tastiera
Tast = new InputStreamReader(System.in);
Input = new BufferedReader(Tast);
NomeFile = Input.readLine();

//Apertura File

pf = new File(NomeFile); //Oggetto File
fs = new FileInputStream(pf); //Canale di input del file

do {
count = 0;
//Menù
System.out.println("1- Scansione del file di testo ");
System.out.println(
"2- Creazione del file delle parole ordinato per
frequenza ");
System.out.println(
"3- Stampa a video del file delle parole ordinato
per frequenza ");
System.out.println(
"4- Stampa a video del file delle parole ordinato
per ordine alfabetico ");
System.out.println("5- Esci ");

do {

in = Input.readLine();
Scelta = in.charAt(0);

} while (Scelta != '1' && Scelta != '2' && Scelta != '3' &&
Scelta != '4' &&
Scelta != '5');

switch (Scelta) {
case ('1'):

Scansione(fs);
System.out.println(" Scansione EFFETTUATA! ");
break;
case ('2'):

CreaAlberoF(treeA);
break;
case ('3'):

Print(treeF);
break;
case ('4'):

Print(treeA);
break;
default:
System.exit(0);
}
} while (Scelta != 5);
}
catch (FileNotFoundException e)
{
System.out.println("Impossibile trovare il file specificato");
System.exit( -1);
}
catch (IOException e)
{
System.out.println("Si è verificato un errore durante la
lettura del file: " + e);
System.exit( -1);
}

}

/*Funzione che effettua la scansione del file per individuare le parole*/
public static void Scansione(FileInputStream fs)
{
String parola = new String();
try
{

//Variabile per la letura dei caratteri dal file di testo
int dato;
//Variabile che memorizza il carattere letto dal file
char carattere;
//Variabile che indica se è terminata la creazione di una parola
boolean fineparola;

//Ciclo di lettura del file
while ((dato =(int) fs.read()) != -1)
{
fineparola = true;
carattere=(char)dato;

//Controllo se il carattere è presente nell'alfabeto
if (Character.isLetter(carattere))
{
//Concatenzazione dei caratteri per formare la parola
parola = parola + carattere;
fineparola = false;
}


//Inserimento della parola
if ((fineparola == true) && (parola != ""))
{
//Lowercase
parola = parola.toLowerCase();

//Richiamo della funzione che aggiunge le parole nell'albero
if (treeA == null)
{
treeA = AggNodo_TreeA(treeA,parola);
} else
{
AggNodo_TreeA(treeA,parola);
}

//Reinizializzazione della variabile parola
parola = "";
fineparola = false;
}

}
//Chiusura file
fs.close();
}
catch (IOException e)
{
System.out.println("Si è verificato un errore durante la scansione
del file: " + e);
System.exit( -1);
}
}

//Funzione di aggiunta di un nodo all'albero
public static Albero AggNodo_TreeA( Albero nodo, String parola)
{
if (nodo == null)
{
//Creazione nuovo nodo dell'albero
nodo = new Albero(parola,1);
}
else
{
//Variabile che restituisce il valore della comparazione parola
int conf;
//Comparazione della parola
conf = nodo.parola.compareTo(parola);

//Se le parole sono uguali incremento il valore della frequenza
if (conf == 0)
{
nodo.freq++;
}

if ( conf > 0)
{
nodo.s = AggNodo_TreeA(nodo.s,parola);
}
if (conf < 0)
{
nodo.d = AggNodo_TreeA(nodo.d,parola);
}
}
return nodo;
}

//Funzione di stampa dell'albero
public static void Print(Albero nodo)
{
try
{
//Canale di lettura da tastiera
InputStreamReader Tast;
//Buffer di lettura
BufferedReader Input;
//Dato
String in = new String();

Tast = new InputStreamReader(System.in);
Input = new BufferedReader(Tast);
if (nodo != null) {
//Richiamo della funzione sul ramo sinistro
Print(nodo.s);

if (count == 10) {
System.out.println("\n Premere un tasto per
continuare...\n");
in = Input.readLine();
count = 0;
} else {
count++;
}
//Stampa della parola
System.out.println("PAROLA: " + nodo.parola + " FREQUENZA: " +
nodo.freq);

//Richiamo della funzione sul ramo destro
Print(nodo.d);
}
}
catch (IOException e)
{
System.out.println("Si è verificato un errore di input: " + e);
System.exit( -1);
}

}

public static Albero AggNodo_TreeF(Albero sorg, Albero dest)
{

if (dest == null)
{
//Creazione del nuovo nodo dell'albero
dest = new Albero(sorg.parola,sorg.freq) ;
}
else
{
//Comparazione Frequenza
if ( dest.freq < sorg.freq)
{
dest.s = AggNodo_TreeF(sorg,dest.s);
}
else
{
dest.d = AggNodo_TreeF(sorg,dest.d);
}
}
return dest;
}

//Funzione che crea l'abero delle frequenze partendo dall'abero ordinato
per alfabeto
public static void CreaAlberoF(Albero sorg)
{
if (sorg != null)
{
CreaAlberoF(sorg.s);
//Richiamo della funzione che aggiunge un nodo all'albero delle
frequenze
if (treeF == null)
{
treeF = AggNodo_TreeF(sorg,treeF);
}
else
{
if (sorg.parola.compareTo("inchiodar")==0)
{
System.out.println("OK");
}
AggNodo_TreeF(sorg,treeF);
}

CreaAlberoF(sorg.d);
}
}

}
 
J

J. Verdrengh

Some one can help me for debug this code?
Is a program that read a text file and create a tree of the word.
The size of text file is 1.5 mb.
with a small text file the program is ok, but with a large file
I have a problem with stack.

What problem? Error or exception? Where is it thrown? What does it say?

Check for recursive function calls (if there are any)...

You should be more specific...
 
R

ricky.clarkson

You get a StackOverflowError, caused by CreaAlberoF calling itself many
many times.

Suppose that the Java stack (the thing that holds local variables) is
1000 elements long. That stack doesn't grow.

I'll give a silly little example:

public final class Recurse
{
public static void main(final String[] args)
{
System.out.println(add(5,6000));
}

private static int add(final int original,final int arg)
{
if (arg==1)
return original+1;

return add(original+1,arg-1);
}
}

This will, in a recursive way, add two numbers together. If I add 6000
to 5, I will have 6000 invocations of add running at once, and there
are around 5 stack variables in that method (intermediate computations
as well as parameters, etc.). So that's 6000*5=30000 stack variables.

6000 works, 12000 gives a StackOverflowError. So recursion is not very
scalable. Iteration is more scalable:

public static int add(final int original,final int arg)
{
int answer=original;

for (int a=0;a<arg;a++)
answer++;

return answer;
}

Of course for this trivial example, original+arg would suffice.

The above code, in terms of memory, is scalable. It wouldn't care if
arg were in the millions. Of course, it might take a long time.

So iterative solutions are generally more scalable than recursive
solutions, but of course a mathematical (no looping/recursion) solution
is better still.

It can be quite hard to rewrite a recursive solution to use iteration.

If you specify what you want this code to do, in English, not Italian,
we might be able to help you further. "A tree of the word" is very
ambiguous.
 
T

TC

The program reads a text file and extrapolates the words of it.
For every found word the number of times that repeats in the text is
counted (Frequency).
The word and its frequency are inserted in a tree (treeA) orderly for
alphabet.
The user can also create a second tree (treeF) orderly for frequency.

CreaAlberoF is the function that beginning from the orderly tree for
alphabet it creates the orderly tree for frequency.
Cannot the StackOverflow have revolved increasing stack?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,101
Latest member
MarcusSkea
Top