java - creating proper array for file input and output to JTextArea -
so new not java programming in general. being said trying write program creates calendar based note such depending on day chosen can make note , put file based on month , year , retrieve , other note on day day basis. has use array somehow not know how implement being totally confused how work arrays( tutorials aren't helping). here have far.
first userinterface.java file:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class userinterface extends jframe implements actionlistener { private string[] months = {"january","february","march","april","may","june","july","august","september","october","november","december"}; private string[] days = {"1", "2", "3", "4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"}; private jbutton save, retrieve; private jtextfield year; private jtextarea entry; private jcombobox month = new jcombobox(months); private jcombobox day = new jcombobox(days); public userinterface() { jpanel mpanel = new jpanel(new gridlayout(2,1)); mpanel.add(new jlabel("month")); mpanel.add(month); month.addactionlistener(this); jpanel dpanel = new jpanel(new gridlayout(2,1)); dpanel.add(new jlabel("day")); dpanel.add(day); month.addactionlistener(this); jpanel ypanel = new jpanel(new gridlayout(2,1)); ypanel.add(new jlabel("year")); ypanel.add(year = new jtextfield(4)); month.addactionlistener(this); jpanel p1 = new jpanel(new flowlayout(flowlayout.left, 30, 10)); p1.add(new jlabel("set date entry:")); p1.add(mpanel); p1.add(dpanel); p1.add(ypanel); jpanel p2 = new jpanel(new flowlayout(flowlayout.left, 60, 10)); p2.add(save = new jbutton("save")); save.addactionlistener(this); p2.add(retrieve = new jbutton("retrieve")); retrieve.addactionlistener(this); jpanel full = new jpanel(new borderlayout()); full.add(p1, borderlayout.north); full.add(p2, borderlayout.south); full.add(entry = new jtextarea(10, 10), borderlayout.center); jframe frame = new jframe(); frame.settitle("calendar manager"); frame.setsize(500, 400); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable( false ); frame.setvisible(true); frame.add(full); } public void actionperformed(actionevent e) { if(e.getactioncommand().equals("save")) { string m = (string)month.getselecteditem(); string d = (string)day.getselecteditem(); string y = year.gettext(); string data = entry.gettext(); calendarmanager.save(m, d, y, data); entry.settext("data written file name "+ month+" "+year+".txt"); } else if(e.getactioncommand().equals("retrieve")){ string m = (string)month.getselecteditem(); string d = (string)day.getselecteditem(); string y = year.gettext(); string data = ""; string result = calendarmanager.retrieve(m, d, y, data); entry.settext(result); } } }
then calendarmanager.java file:
import java.io.*; public class calendarmanager { private string[] calobject = new string[31]; public static boolean save(string month, string day, string year, string data) { string filename = month+" "+year+".dat"; int daynum = integer.parseint(day); try { file file = new file(filename); if(!file.exists()) { file.createnewfile(); } objectoutputstream output = new objectoutputstream(new fileoutputstream(file)); for(int i=0; i<31; i++){ output.writeutf(month+"-"+day+"-"+year+": "+data); } } catch (ioexception e) { e.printstacktrace(); } return true; } public static string retrieve(string month, string day, string year, string data) { string filename = month+" "+year+".dat"; int daynum = integer.parseint(day); try { file file = new file(filename); if(!file.exists()) { return "file not found"; } objectinputstream input = new objectinputstream(new fileinputstream(file)); for(int i=0; i<31; i++){ if(input == null){ return "entry not found"; } else{ data = input.readutf(); } } } catch (ioexception e) { e.printstacktrace(); } return data; } }
then calendartest.java file:
public class calendartest { public static void main(string[] args) { userinterface calendar = new userinterface(); } }
if can tell me doing wrong , code has simple possible without may not have learned think beginner's java class material.
you did not describe exact problem is, after quick check guess file not saved. try string filename = month+"_"+year+".dat";
underscore instead of space.
Comments
Post a Comment