Boa tarde?!
Gostaria de pedir um favor a todos, se puderem me ajudar é claro...
estou com um problema no meu código, estou fazendo ele com uma seqüência de caracteres, para me retornar as freqüência dos caracteres.
Só que esse meu código está compilando legal, certinho.. só que, na hora que gostaria de executar, ele não executa e fica dando problema de projeto principal. Se puderem estar observando qual é o erro que eu estou fazendo, é a 4 vez que me acontece isso, e não estou conseguindo resolver esse problema.
public class Main {
public static void main(String args[])throws Exception{
int i;
int[] vetor = new int[256];
while( (i=System.in.read()) != -1)//Enquanto houver Caracteres, leia
{
vetor[i]++; //Armazena quantos caracteres se repetiram
}
Huffman huffman = new Huffman(vetor);//instancia do objeto Huffman, "vetor" como parametro
}
}
class Huffman {
private int[] vet;
private int tamanho = 0, i=0;
private NodeHuffman aux;
private Heap heap;
public Huffman() {
}
//criando um construtor huffman
Huffman(int[] vetor) {
boolean exp = false;
while (exp) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
public void conta_frequencia() {
try {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);//passa para usuário selecionar o arquivo a ser compactado
FileReader fr = new FileReader(fc.getSelectedFile());//pegando o arquivo selecionado
vet = new int[256];
Arrays.fill(vet, 0);// corre todo o vetor colocando o valor 0
int c;
do {
c = fr.read();//pega uma letra (em hexa)
if (c > 0) {
vet[c]++;
}
} while (c >= 0);
fr.close();//fecha o arquivo
for (i = 0; i <= 255; i++) {
if (vet[i] > 0) {
tamanho++;//percorre o vetor e conta quantos caracteres são diferente
}
}
heap = new Heap(tamanho);
} catch (Exception e) {
e.printStackTrace();
}
}
//inserir no heap o nó com a frequencia de caracter
public void insere() {
NodeHuffman no;
for (i = 0; i <= 255; i++) {
if (vet[i] > 0) {
char arm = ((char) i);
no = new NodeHuffman();
no.setCarac(arm);
no.setFreq(vet[i]);
heap.insere(no);
}
}
arvore();
}
//funcao que pega as menores frequencias somando-os e inserindo novamente no heap
//construindo a arvore de huffman
private void arvore() {
NodeHuffman esq, dir;
int total;
do{
aux = new NodeHuffman();
esq = heap.remove();
dir = heap.remove();
aux.setEsq(esq);
aux.setDir(dir);
aux.setFreq(aux.getEsq().getFreq() + aux.getDir().getFreq());
total = heap.insere(aux);
}while(total!=1);
}
//funcao que chama a impressao do codigo binario
String msg="";
public String imprime() {
String cod = "";
msg = imprime(aux, cod);
return msg;
}
//funcao que retorna a codificação das string's
private String imprime(NodeHuffman p, String codigo) {
if (p.getEsq() == p.getDir()) {
msg+= p.getCarac() + " " + codigo +"\n";
return msg;
} else {
imprime(p.getEsq(), codigo + "0");
imprime(p.getDir(), codigo + "1");
}
return msg;
}
}
a Classe NodeHuffman é por referencia né.. os métodos get e set, não preciso colocar né..
class Heap {
private NodeHuffman Heap[] ;
private int tamanho, pai = 0, i = 0, cauda = 0, filho = 0;
public Heap() {
}
public Heap(int tam) {
//inicializa o vetor alocando em cada nó
this.tamanho = tam + 1;
Heap = new NodeHuffman[tamanho];
for (i = 0; i < tamanho; i++) {
Heap[i] = new NodeHuffman();
}
}
//insere o node no heap
public int insere(NodeHuffman n) {
cauda++;
Heap[cauda] = n;
sobe(cauda);
return cauda;
}
//remove node do heap
public NodeHuffman remove() {
NodeHuffman tem1;
tem1 = new NodeHuffman();
tem1 = Heap[1];
Heap[1] = Heap[cauda];
cauda--;
desce(1);
return tem1;
}
//funcao que pega o valor e desce para posição correta ao ser removido
public void desce(int p) {
NodeHuffman temp;
temp = new NodeHuffman();
filho = 2 * p;
if (filho > cauda) {
return;
} else if (((filho + 1) <= cauda) && (Heap[filho + 1].getFreq() <= Heap[filho].getFreq())) {
filho++;
}
if (Heap[p].getFreq() > Heap[filho].getFreq()) {
temp = Heap[p];
Heap[p] = Heap[filho];
Heap[filho] = temp;
desce(filho);
}
}
//funcao que pega o valor e sobe para posição correta ao ser inserido
private void sobe(int pos) {
pai = pos / 2;
if ((pai > 0) && (Heap[pai].getFreq() > (Heap[pos].getFreq()))) {
NodeHuffman temp2;
temp2 = Heap[pai];
Heap[pai] = Heap[pos];
Heap[pos] = temp2;
sobe(pai);
} else {
return;
}
}
}
public class Prinncipal extends javax.swing.JFrame {
Huffman h;
/** Creates new form Prinncipal */
public Prinncipal() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
visual = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
selecionar = new javax.swing.JButton();
imprimir = new javax.swing.JButton();
sair = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
visual.setColumns(20);
visual.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
visual.setRows(5);
jScrollPane1.setViewportView(visual);
jLabel1.setFont(new java.awt.Font("Berlin Sans FB", 1, 18)); // NOI18N
jLabel1.setForeground(new java.awt.Color(102, 102, 102));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setLabelFor(this);
jLabel1.setText("Digita um texto que queria codificar.");
selecionar.setFont(new java.awt.Font("Simplified Arabic", 1, 12)); // NOI18N
selecionar.setText("Arquivo a ser selecionado");
selecionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
selecionarActionPerformed(evt);
}
});
imprimir.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
imprimir.setText("Gerar Codificação");
imprimir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
imprimirActionPerformed(evt);
}
});
sair.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
sair.setText("Sair");
sair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sairActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(imprimir, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
.addComponent(sair, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
.addComponent(selecionar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(10, 10, 10)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(selecionar)
.addGap(18, 18, 18)
.addComponent(imprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
.addComponent(sair, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void selecionarActionPerformed(java.awt.event.ActionEvent evt) {
h = new Huffman();
visual.setText("");
h.conta_frequencia();
h.insere();
}
private void imprimirActionPerformed(java.awt.event.ActionEvent evt) {
visual.setText(h.imprime());
}
private void sairActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
// Variables declaration - do not modify
private javax.swing.JButton imprimir;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton sair;
private javax.swing.JButton selecionar;
private javax.swing.JTextArea visual;
// End of variables declaration
}
Se puder me ajudar, eu agradeço, não aguento mais tentar ver se acho esse "problema" que eu acabo caindo e não resolvendo.
muito obrigado pela atenção!