java - Using FileWriter with try catch, for IOExeption with several issues -
as said above have several issues. trying export array list 'inventory' .txt file. here code, more welcome!
import java.io.filewriter.*; import java.io.*; private void exportgoactionperformed(java.awt.event.actionevent evt) { filewriter writer; try { writer = new filewriter("inventory.txt"); for(string str: inventory) { writer.write(str); } writer.close(); }catch (ioexeption e){ alertout.settext("i'm afraid file not writen @ time."); } { try { writer.close(); } catch (ioexception ignore){} } public static void main(string args[]) {
on line 12: can not find symbol: class ioexeption.
on line 18: reached end of file while parsing.
on line 20: illegal start of expression.
here full code:
/* * change template, choose tools | templates * , open template in editor. */ import java.util.*; import java.io.filewriter.*; import java.io.*; /** * * @author matthewmurchison */ 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> (); /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jlabel1 = new javax.swing.jlabel(); jlabel2 = new javax.swing.jlabel(); jlabel3 = new javax.swing.jlabel(); jlabel4 = new javax.swing.jlabel(); jlabel5 = new javax.swing.jlabel(); namein = new javax.swing.jtextfield(); idin = new javax.swing.jtextfield(); desin = new javax.swing.jtextfield(); numin = new javax.swing.jtextfield(); jlabel14 = new javax.swing.jlabel(); jlabel15 = new javax.swing.jlabel(); addgo = new javax.swing.jbutton(); exportgo = new javax.swing.jbutton(); alertout = new javax.swing.jlabel(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jlabel1.settext("jlabel1"); jlabel2.settext("name:"); jlabel3.settext("id"); jlabel4.settext("descripion"); jlabel5.settext("number"); namein.settext("jtextfield1"); idin.settext("jtextfield2"); idin.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { idinactionperformed(evt); } }); desin.settext("jtextfield3"); numin.settext("jtextfield4"); jlabel14.settext("add"); jlabel15.settext("export .txt"); addgo.settext("add"); addgo.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { addgoactionperformed(evt); } }); exportgo.settext("export"); exportgo.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { exportgoactionperformed(evt); } }); alertout.settooltiptext(""); org.jdesktop.layout.grouplayout layout = new org.jdesktop.layout.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(org.jdesktop.layout.grouplayout.leading) .add(org.jdesktop.layout.grouplayout.trailing, layout.createsequentialgroup() .add(0, 0, short.max_value) .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.leading) .add(org.jdesktop.layout.grouplayout.trailing, layout.createsequentialgroup() .add(jlabel14) .add(245, 245, 245)) .add(org.jdesktop.layout.grouplayout.trailing, layout.createsequentialgroup() .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.trailing) .add(jlabel15) .add(addgo) .add(exportgo)) .add(233, 233, 233)))) .add(layout.createsequentialgroup() .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.leading) .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.trailing) .add(layout.createsequentialgroup() .add(225, 225, 225) .add(jlabel1) .add(117, 117, 117)) .add(layout.createsequentialgroup() .addcontainergap() .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.leading) .add(jlabel2) .add(layout.createsequentialgroup() .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.leading) .add(jlabel4) .add(jlabel5) .add(jlabel3)) .add(18, 18, 18) .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.leading, false) .add(desin) .add(numin) .add(namein) .add(idin, org.jdesktop.layout.grouplayout.preferred_size, 202, org.jdesktop.layout.grouplayout.preferred_size)))))) .add(layout.createsequentialgroup() .add(150, 150, 150) .add(alertout, org.jdesktop.layout.grouplayout.preferred_size, 225, org.jdesktop.layout.grouplayout.preferred_size))) .addcontainergap(149, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(org.jdesktop.layout.grouplayout.leading) .add(layout.createsequentialgroup() .addcontainergap() .add(jlabel1) .addpreferredgap(org.jdesktop.layout.layoutstyle.related, 22, short.max_value) .add(jlabel14) .add(18, 18, 18) .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.baseline) .add(jlabel2) .add(namein, org.jdesktop.layout.grouplayout.preferred_size, org.jdesktop.layout.grouplayout.default_size, org.jdesktop.layout.grouplayout.preferred_size)) .addpreferredgap(org.jdesktop.layout.layoutstyle.related) .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.baseline) .add(jlabel3) .add(idin, org.jdesktop.layout.grouplayout.preferred_size, org.jdesktop.layout.grouplayout.default_size, org.jdesktop.layout.grouplayout.preferred_size)) .addpreferredgap(org.jdesktop.layout.layoutstyle.related) .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.baseline) .add(jlabel4) .add(desin, org.jdesktop.layout.grouplayout.preferred_size, org.jdesktop.layout.grouplayout.default_size, org.jdesktop.layout.grouplayout.preferred_size)) .addpreferredgap(org.jdesktop.layout.layoutstyle.related) .add(layout.createparallelgroup(org.jdesktop.layout.grouplayout.trailing) .add(numin, org.jdesktop.layout.grouplayout.preferred_size, org.jdesktop.layout.grouplayout.default_size, org.jdesktop.layout.grouplayout.preferred_size) .add(jlabel5)) .addpreferredgap(org.jdesktop.layout.layoutstyle.related) .add(addgo) .add(18, 18, 18) .add(jlabel15) .add(40, 40, 40) .add(exportgo) .add(18, 18, 18) .add(alertout, org.jdesktop.layout.grouplayout.preferred_size, 37, org.jdesktop.layout.grouplayout.preferred_size) .add(194, 194, 194)) ); pack(); }// </editor-fold> private void idinactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: } 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; try { writer = new filewriter("inventory.txt"); for(string str: inventory) { writer.write(str); } writer.close(); }catch (ioexception e){ alertout.settext("i'm afraid file not writen @ time."); } { try { writer.close(); } catch (ioexception ignore){} } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(mventory.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(mventory.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(mventory.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(mventory.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new mventory().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jbutton addgo; private javax.swing.jlabel alertout; private javax.swing.jtextfield desin; private javax.swing.jbutton exportgo; private javax.swing.jtextfield idin; private javax.swing.jtextfield namein; private javax.swing.jtextfield numin; private javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel14; private javax.swing.jlabel jlabel15; private javax.swing.jlabel jlabel2; private javax.swing.jlabel jlabel3; private javax.swing.jlabel jlabel4; private javax.swing.jlabel jlabel5; // end of variables declaration }
the problem can solve misspelled ioexception
in line 12.
Comments
Post a Comment