hey there again,
I am trying to build a small mechanism that lets me store product names and prices on a produktet.txt file. the file should hold lines as follows:

product1 price1
product2 price2
.
.
productN priceN

So, I built a small panel with three rows and one column. first row holds a label for product and a text field allowing a user to input the name of a product, and the second row holds a label for price and a text field for inputing the price. row 3 holds the button.

when button is pushed, information given on the fields should be read and concatenated to form a string, which is then saved as the last line on the produktet.txt file. however, I am getting an "unreported exception" error. I tried adding 'throws IOException' to my button's constructor method, but it won't work. I can't just add it to actionPerformed method headline, because then actionPerformed method of ActionListener is not being overridden. can someone please tell me how to go about declaring the exception, or perhaps my way of building the mechanism is wrong.

here is my code:

//this is my panel class
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;

public class ShtoProdukte extends JPanel{

	public JLabel emri;//product name
	public JLabel cmimi;//product price
	public JTextField em;//product name text field
	public JTextField cm;//product price text field
	public int i = 0;//this variable will hold the number of products on my produktet.txt file
	public String[] produktet;//this array will hold all products extracted from produktet.txt

	public ShtoProdukte() throws IOException{
		super(new GridLayout(3, 2));
		emri = new JLabel("Product name");//""
		cmimi = new JLabel("Product price");
		JButton shto = new ShtoProdButton("Add product", this);
		em = new JTextField();
		cm = new JTextField();
		add(emri);
		add(em);
		add(cmimi);
		add(cm);
		add(shto);
		//ope file here to count how many  products are already in the file produktet.txt
			FileReader file = new FileReader("produktet.txt");
			BufferedReader reader = new BufferedReader(file);
	
			while(reader.ready()){
				reader.readLine();
				i++;
			}
	
			reader.close();
		//---------------------------------------------------------------

		produktet = new String[i + 1];//initialize produktet array with a length one more than
			//the number of products already there, in order to make room for
			//the product being added

		//extract product names and prices already in the file
		//and save them on the array
		FileReader file1 = new FileReader("produktet.txt");
		BufferedReader reader1 = new BufferedReader(file1);
		int k = 0;
		while(reader1.ready()){
			produktet[k] = reader1.readLine();
			k++;
		}
		reader1.close();
		//------------------------------------------
	}



	public void paintComponent(Graphics g){

	}



	public static void main(String[] args) throws IOException{
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
		f.getContentPane().add(new ShtoProdukte());
		f.pack();
		f.setVisible(true);
	}
}

and this is my button:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class ShtoProdButton extends JButton implements ActionListener{

	public ShtoProdukte sh;

	ShtoProdButton(String s, ShtoProdukte a) throws IOException{
		super(s);
		sh = a;
	}

	public void actionPerformed(ActionEvent ev){
		//this is my button. it should save the new product name and price on the file
		//but, I first save it on the array produktet
		sh.produktet[sh.i + 1] = sh.em.getText() + " " + sh.cm.getText();

		//I save here each element of array produktet onto the file
		FileWriter fw = new FileWriter("produktet.txt");
		PrintWriter pw = new PrintWriter(fw);

		for(int j = 0; j < sh.i + 1; j++){
			pw.println(sh.produktet[j]);
		}

		pw.close();
		//---------------------------------------------------------s
	}
}

thank you!

Try moving the I/O code out of the listener to its own thread. There may be a problem doing too much on Swing's EDT thread.

Why don't you add a try{} catch block around the code that throws the exception?


You should use an ArrayList to hold the lines, not an array. Many problems will disappear if you do that.

Edited 5 Years Ago by NormR1: n/a

Comments
thanks for helping

hey Norm,
thank you for your help. I put all the code of my actionPerformed inside a try-block (except for the firs line)), and added a catch block (I am not sure if I am using the right words to describe what I did), and everything is working just fine. thank you very much.

This question has already been answered. Start a new discussion instead.