Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with I/O program

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Location
    So Cal
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with I/O program

    Hi peeps... I'm trying to write a program that enables the user to write and retrieve memos with calendar dates. Here is what I got so far...
    //Test class
    import javax.swing.JFrame.*;
     
    public class CalendarTest{
     
    	public static void main(String[] args) {
    		userInterface uI = new userInterface();
    		uI.setSize(600, 300);
    		uI.setLocationRelativeTo(null);
    		uI.setVisible(true);
    		uI.getDefaultCloseOperation();
      }
    }
    //user interface class
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class userInterface extends JFrame implements ActionListener
    {
     
    private JButton save, retrieve;
    private JLabel sidl, namel, instl;
    private JTextField sidtf, nametf, insttf;
    private JLabel commentl;
    private JTextArea commentta;
    private JScrollPane commentsp;
     
    public userInterface()
    {
    super.setTitle("Memo Generator");
    buildTop();
    buildBottom();
    buildRight();
     
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    }
     
    private void buildTop()
    {
    Dimension dim = new Dimension(60,25);
    JPanel tp = new JPanel();
    sidl = new JLabel("Month");
    namel = new JLabel("Day");
    instl = new JLabel("Year");
    sidtf = new JTextField(8);
    nametf = new JTextField(8);
    insttf = new JTextField(8);
    sidl.setPreferredSize(dim);
    namel.setPreferredSize(dim);
    instl.setPreferredSize(dim);
    sidl.setHorizontalAlignment(SwingConstants.RIGHT);
    namel.setHorizontalAlignment(SwingConstants.RIGHT);
    instl.setHorizontalAlignment(SwingConstants.RIGHT);
    tp.add(sidl);
    tp.add(sidtf);
    tp.add(namel);
    tp.add(nametf);
    tp.add(instl);
    tp.add(insttf);
    add(tp, BorderLayout.NORTH);
    }
     
    private void buildBottom()
    {
    Dimension dim = new Dimension(90,25);
     
    JPanel bp1 = new JPanel();
    save = new JButton("Save");
    retrieve = new JButton("Retrieve");
     
    save.setPreferredSize(dim);
    retrieve.setPreferredSize(dim);
     
    bp1.add(save);
    bp1.add(retrieve);
     
    JPanel bp3 = new JPanel(new GridLayout(2,1));
    bp3.add(bp1);
     
    add(bp3, BorderLayout.SOUTH);
    }
     
    private void buildRight()
    {
    JPanel rp = new JPanel();
    rp.setLayout(new BorderLayout());
    rp.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
    commentl = new JLabel("Message: ");
    commentl.setHorizontalAlignment(SwingConstants.CENTER);
    rp.add(commentl, BorderLayout.NORTH);
    commentta = new JTextArea(6,30);
    commentsp = new JScrollPane(commentta);
    rp.add(commentsp, BorderLayout.CENTER);
    add(rp, BorderLayout.CENTER);
    }
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
    }

    These two seem to work as intended. Its this last class where the I/O is supposed to take place that is giving me difficulty...

    import java.io.*;
     
    public class CalendarManger 
    {
    	public userInterface uI = new userInterface();
        public CalendarManger() 
        {
        	/*Set the base directory where the calendar manager’s data files will reside
            Create a file object for the base directory and make sure that the directory
    	exists, and if it doesn’t, use the mkdir method on the file object to create it.*/
        	File fileDir = new File ("c:\\temp\\");
        }
     
        private void getFile(String month, String year)
        {
    	/*This method trys to get the string array from the specified file
    	Formulate the filename from the base directory, month, and year – make sure you 
    		use double backslashes after the base directory
    	Create a file object using the filename
    	If the file does not exist, create a new array of 31 strings for the member 
    variable
                else create a FileInputStream and ObjectInputStream on the file and use the 
    		readObject method of the object input stream to get an array of 
    		strings from the file – remember to cast the value returned by the 
    		readObject method to a String [ ] and assign it to the string array
    member variable. Close the streams.
    If an exception occurs, create a new array of 31 strings for the
    member variable*/
        }
     
        public void getEntry(String month, String day, String year)
        {  
    /*Call the getFile method to try to get the string array from the file 
    specified by the month and year.
    Convert the day string to an integer
    Remember, days go from 1 to 31, while the string array contains entries 0
     	thru 30
    Get the entry from the string array member variable
    If it is not null, return that string
    Else return an error string indicating that the entry does not exist*/
     
        }
     
        public void saveEntry(String month, String day, String year, String entry)
        {
    /*Call the getFile method to try to get the string array from the file 
    specified by the month and year.
    Convert the day string to an integer
    Remember, days go from 1 to 31, while the string array contains entries 0
     	thru 30
    Save the entry to the string array member variable
    Create a FileOutputStream object using the base directory, month, and year.
    Create an ObjectOutputStream object on top of the file output stream.
    Use the writeObject method to write the string array member variable to the
     file.
    Close the object stream and the file stream object.
    If everything goes well, return a string indicating success.
    If there was an exception, return a string indicating an error occurred.*/
        }
    }

    I have pseudo code that was given to help guide me but I'm still stuck. Any help will be greatly appreciated.


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Help with I/O program

    Please, post a simple example that would abstract your problem from your project. There is really no need to give us your GUI code when you know that the problem is in I/O. And define the problem precisely. "Its this last class where the I/O is supposed to take place that is giving me difficulty..." gives us little or no information about the problem.

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 06:16 AM
  2. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 02:56 PM
  3. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  4. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  5. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 8th, 2012, 11:47 PM