java - My array list to .txt file program is giving me several errors -
i have inventory application sets list of strings based on user input. supposed export strings .txt file. have never used filewriter before, , stuck.
any appreciated.
here relevant code:
the 3 lines near end have 'writer.' in them giving me unreported exception ioexception; must caught or declared thrown
.
import java.util.*; import java.io.filewriter; public class mventory extends javax.swing.jframe { /** * creates new form mventory */ public mventory() { initcomponents(); } public class productinfo { string name; string des; string id; string num; public productinfo(string name, string des, string id, string num) { this.name = name; this.des = des; this.id = id; this.num = num; } } public static void inventory() { } //creat array arraylist<string> inventory = new arraylist<string>(); private void addgoactionperformed(java.awt.event.actionevent evt) { // add item string name, description, identification, number, item; name = namein.gettext(); description = desin.gettext(); identification = idin.gettext(); number = numin.gettext(); item = "" + name + "," + description + "," + identification + "," + number + "."; inventory.add(new string(item)); namein.settext(""); desin.settext(""); idin.settext(""); numin.settext(""); } private void exportgoactionperformed(java.awt.event.actionevent evt) { filewriter writer; writer = new filewriter("inventory.txt"); (string str : inventory) { writer.write(str); } writer.close(); } }
you need try/catch statement around writer.
such as:
private void exportgoactionperformed(java.awt.event.actionevent evt) { filewriter writer = null; try { writer = new filewriter("inventory.txt"); (string str : inventory) { writer.write(str); } } catch (ioexception e) { //handle exception, rethrow or log e.printstacktrace(); } { try { writer.close(); } catch (ioexception ignore){ } } }
Comments
Post a Comment