So I am very new to not only java but programming in general. With that being said I am trying to write a program that creates a calendar based note such that depending on the day chosen you can make a note and put it into a file based on the month and year and then retrieve that and any other note on a day by day basis. This has to use an array somehow which I do not know how to fully implement being totally confused as to how to work with arrays( tutorials aren't helping). so here is what I have so far.
First the 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 for 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 successfully to file with 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 the 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 finally the CalendarTest.java file:
public class CalendarTest {
public static void main(String[] args) {
UserInterface calendar = new UserInterface();
}
}
If anyone can tell me what I am doing wrong and also my code has to be as simple as possible without anything I may not have learned so think beginner's java class material.
You did not describe what your exact problem is, but after a quick check i guess the file is not saved. Try
String fileName = month+"_"+year+".dat";
with an underscore instead of a space.