Problemas com leitura de Arquivo

1 resposta
mota
package MoisesMota;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.Graphics;
import java.lang.*;


public class AplGrafica extends JFrame {
	JButton treinamento = new JButton("Treinamento");

	JButton teste = new JButton("Teste");

	JButton sair = new JButton("Sair");

	JButton grafico = new JButton("gráfico");

	JPanel painel = new JPanel(new FlowLayout());

	JPanel painel2 = new JPanel();

	JTextArea text = new JTextArea(20, 60);

	JScrollPane scroll = new JScrollPane(text);

	JScrollBar barra = new JScrollBar();

	final double LIMITE = 1.00;

	double vetor[][] = { { 5.1, 3.5, 1.4, 0.2 }, { 4.9, 3.0, 1.4, 0.2 },
			{ 4.7, 3.2, 1.3, 0.2 }, { 4.6, 3.1, 1.5, 0.2 },
			{ 5.0, 3.6, 1.4, 0.2 }, { 5.4, 3.9, 1.7, 0.4 },
			{ 4.6, 3.4, 1.4, 0.3 }, { 5.0, 3.4, 1.5, 0.2 },
			{ 4.4, 2.9, 1.4, 0.2 } };

	Random random = new Random();

	double pesos[] = new double[4];

	double entradas1[] = new double[4];
	
	double entradas1e2[] = new double[4];

	double entradas2[] = new double[4];

	double entradas3[] = new double[4];

	double entradas4[] = new double[4];
	
	double entradas5[] = new double[4];
	
	double entradas6[] = new double[4];

	String nomeArquivo1 = "MoisesMota/arquivo1.txt";
	
	String nomeArquivo2 = "MoisesMota/arquivo2.txt";

	String nomeArquivo3 = "MoisesMota/arquivo3.txt";
	
	String nomeArquivo4 = "MoisesMota/arquivo4.txt";
	
	GravaArquivoSequencial gas1 = new GravaArquivoSequencial(nomeArquivo1);
	
	GravaArquivoSequencial gas2 = new GravaArquivoSequencial(nomeArquivo2);

	GravaArquivoSequencial gas3 = new GravaArquivoSequencial(nomeArquivo3);
	
	GravaArquivoSequencial gas4 = new GravaArquivoSequencial(nomeArquivo4);

	LeArquivoSequencial las1 = new LeArquivoSequencial(nomeArquivo1);

	LeArquivoSequencial las2 = new LeArquivoSequencial(nomeArquivo2);

	LeArquivoSequencial las3 = new LeArquivoSequencial(nomeArquivo3);
	
	LeArquivoSequencial las4 = new LeArquivoSequencial(nomeArquivo4);

	Registro reg1, reg2, reg3, reg4;

	double sum1, sum2, sum3, sum4, correc, str50;

	double ro = 0.5;

	int l, j, i, m, n, o;

	String t0, t1, t2, t3, t4, t5;

	graphic g;

	public AplGrafica() {
		super("Perceptron");
		setSize(700, 420);
		setLocation(100, 100);
		add(painel);

		painel2.add(treinamento);
		getContentPane().add(painel2, "South");
		treinamento.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				geraNumeros();
				perceptron();

			}
		});

		painel2.add(teste);
		getContentPane().add(painel2, "South");
		teste.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});

		// mostra o gráfico
		painel2.add(grafico);
		getContentPane().add(painel2, "South");
		grafico.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
                 
				
			}
		});

		painel2.add(sair);
		getContentPane().add(painel2, "South");
		sair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			};
		});
		painel.add(scroll);
		text.setEditable(false);
		text.setBorder(BorderFactory.createTitledBorder("Treinamento/Teste"));
		setVisible(true);

	}

	void geraNumeros() {
		String str = "";
		for (int i = 0; i < 4; i++) {
			pesos[i] = random.nextDouble();
			str += pesos[i] + " ";
		}
		t0 = str;
		text.setText(text.getText() + String.valueOf(t0));

	}

	void perceptron() {

		las1.openFile(); // abre arquivo1
		las2.openFile(); // abre arquivo2
		while (las1.leRegistro()) {
			reg1 = las1.getRegistro();
			entradas1[0] = reg1.valor1;
			entradas1[1] = reg1.valor2;
			entradas1[2] = reg1.valor3;
			entradas1[3] = reg1.valor4;
			while (true) {
				sum1 = somatorio(entradas1);

				if (sum1 <= 0) {
					// text.append
					t1 = ((entradas1[0] = reg1.valor1) + " "
							+ (entradas1[1] = reg1.valor2) + " "
							+ (entradas1[2] = reg1.valor3) + " "
							+ (entradas1[3] = reg1.valor4) + " " + " \n"
							+ "Este vetor pertence a classe 1" + "\n" + "\n");
					text.setText(text.getText() + String.valueOf(t1));

					break;

				} else {
					correcao(sum1);
					sum1 = somatorio(entradas1);
				}

			}

			las2.leRegistro();
			reg2 = las2.getRegistro();
			entradas2[0] = reg2.valor1;
			entradas2[1] = reg2.valor2;
			entradas2[2] = reg2.valor3;
			entradas2[3] = reg2.valor4;
			while (true) {
				sum2 = somatorio(entradas2);

				if (sum2 > 0) {

					t2 = ((entradas2[0] = reg2.valor1) + " "
							+ (entradas2[1] = reg2.valor2) + " "
							+ (entradas2[2] = reg2.valor3) + " "
							+ (entradas2[3] = reg2.valor4) + " " + " \n"
							+ "Este vetor pertence a classe 2" + "\n" + "\n");
					text.setText(text.getText() + String.valueOf(t2));

					break;
				} else {
					correcao(sum2);
					sum2 = somatorio(entradas2);
				}
			}

		}

		las1.closeFile();
		las2.closeFile();
		JOptionPane.showMessageDialog(null, "terminou");

	}

	double somatorio(double entradas[]) {

		double sum = 0;
		for (int i = 0; i < pesos.length; i++) {
			sum += (pesos[i] * (entradas[i] + ro));
		}
		return sum;
	}

	void correcao(double sum) {
		String str50 = "";

		if (sum > 0) {

			for (int j = 0; j < pesos.length; j++) {

				correc = pesos[j] - ((ro) * (1) * entradas1[i]);
				pesos[j] = correc;
				str50 += pesos[j] + " ";
			}
		} else {

			for (int l = 0; l < pesos.length; l++) {
				correc = pesos[j] - ((ro) * (-1) * entradas2[i]);
				pesos[l] = correc;
				str50 += pesos[l] + " ";
			}
		}

		t3 = "pesos=" + " " + str50 + "\n";
		text.setText(text.getText() + String.valueOf(t3));

	}

	void Teste() {

		las3.openFile(); // abre arquivo1
		las4.openFile(); // abre arquivo2
		while (las3.leRegistro()) {
			reg3 = las3.getRegistro();
			entradas3[0] = reg3.valor1;
			entradas3[1] = reg3.valor2;
			entradas3[2] = reg3.valor3;
			entradas3[3] = reg3.valor4;
			while (true) {
				sum3 = somatorio2(entradas3);

				if (sum3 <= 0) {
					t4 = ((entradas3[0] = reg3.valor1) + " "
							+ (entradas3[1] = reg3.valor2) + " "
							+ (entradas3[2] = reg3.valor3) + " "
							+ (entradas3[3] = reg3.valor4) + " " + " \n"
							+ "Este vetor pertence a classe 1" + "\n" + "\n");
					text.setText(text.getText() + String.valueOf(t4));
					break;

				}
			}

			las4.leRegistro();
			reg4 = las4.getRegistro();
			entradas4[0] = reg4.valor1;
			entradas4[1] = reg4.valor2;
			entradas4[2] = reg4.valor3;
			entradas4[3] = reg4.valor4;
			while (true) {
				sum4 = somatorio(entradas4);

				if (sum4 > 0) {
					t5 = ((entradas4[0] = reg4.valor1) + " "
							+ (entradas4[1] = reg4.valor2) + " "
							+ (entradas4[2] = reg4.valor3) + " "
							+ (entradas4[3] = reg4.valor4) + " " + " \n"
							+ "Este vetor pertence a classe 2" + "\n" + "\n");
					text.setText(text.getText() + String.valueOf(t5));
					break;
				} 
			}

		}

		las3.closeFile();
		las4.closeFile();
		JOptionPane.showMessageDialog(null, "terminou");

	}

	double somatorio2(double entradas[]) {

		double sum = 0;
		for (int j = 0; j < pesos.length; j++) {
			sum += (pesos[l] * (entradas[j] + ro));
		}
		return sum;
	}
	void gravaArquivo(GravaArquivoSequencial gas) {
		gas.openFile();
		for (int i = 0; i < vetor.length; i++) {
			gas.gravaRegistro(vetor[i][0], vetor[i][1], vetor[i][2], vetor[i][3]);
		}
		gas.closeFile();
	}
	public static void main(String[] args) {
		AplGrafica ag = new AplGrafica();
		ag.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

}
package MoisesMota;
import java.util.*;
import java.io.*;
public class LeArquivoSequencial {
	Scanner in;
	String nomeFile;
	Registro r;
	String str = "";
	public LeArquivoSequencial(String nomeFile) {
		this.nomeFile = nomeFile;
	}
	void openFile() {
		try {
			in = new Scanner(new File(nomeFile));
		} catch (Exception e) {}
	}
	boolean leRegistro() {
		if (in.hasNext()) {
			r = new Registro(in.nextDouble(), in.nextDouble(), in.nextDouble(), in.nextDouble());
			return true;
		} else {
			return false;
		}
	}
	void closeFile() {
		in.close();
	}
	Registro getRegistro() {
		return r;
	}
}
package MoisesMota;
import java.util.*;
public class GravaArquivoSequencial {
	Formatter out;
	String nomeArquivo;
	Registro r;
	public GravaArquivoSequencial(String nomeArquivo) {
		this.nomeArquivo = nomeArquivo;
	}
	void openFile() {
		try {
			out = new Formatter(nomeArquivo);
		} catch(Exception e) {
			
		}
	}
	void gravaRegistro(double valor1,double valor2,double valor3,double valor4) {
		r = new Registro(valor1, valor2, valor3, valor4);
		out.format("%2.2f %2.2f %2.2f %2.2f\n", r.valor1, r.valor2, r.valor3, r.valor4);
	}
	void closeFile() {
		out.close();
	}
}
package MoisesMota;
public class Registro {
	double valor1;
	double valor2;
	double valor3;
	double valor4;
	public Registro() {}
	public Registro(double valor1,double valor2,double valor3,double valor4) {
		this.valor1 = valor1;
		this.valor2 = valor2;
		this.valor3 = valor3;
		this.valor4 = valor4;
	}
}

Bem gente, eu estou precisando de ajuda urgente !!! Quando eu executo a classe AplGrafica e quando executo o treinamento, ele dá um problema dizendo que n consegue ler o arquivo !! Queria saber o pq !! Algu´qm pode me ajudar ?!

1 Resposta

mota

Gente por favor n se assustem com esse codigo, ele tah grande assim pq n tenho experiencia com a liguagem e então programo como se não fosse orientado a objetos !! e aí coloco tudo numa classe só !!!

Criado 10 de abril de 2008
Ultima resposta 10 de abr. de 2008
Respostas 1
Participantes 1