Hello,
My assignment in class is to write a notepad program in java, using only the .io and .awt extensions (I believe that's what they're called). Now, I've got every part of the program working, except the save and saveAs functions. I've spent roughly 5 hours looking for and trying things that would solve my problem, but have found nothing (This professor doesn't really teach java, he teaches from the glossary, so it's become a learning experience in itself). the code I have so far is taken from the web, and I have modified it a bit to suit my program. I've gotten to the realization that it's not actually saving, and it doesnt know where to save to, and that's where I'm at a loss. How might I get that to change? Code is as follors for the two functions:

       private void doSaveAs() {
           StringBuffer textBuffer = new StringBuffer();
              FileDialog fileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);
              String name = fileDialog.getFile();
                fileDialog.setFilenameFilter(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".txt");
                    }
                });
                fileDialog.setFile("Untitled.txt");
                fileDialog.setVisible(true);
                System.out.println("File: " + fileDialog.getFile());
                setTitle("JavaEdit: " +name);   // reset frame title
                text.setText(textBuffer.toString());
           }
       private void doSave() {
            FileDialog fileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);
            String name = fileDialog.getFile();
            if(name == null)
            {
                doSaveAs();
            }
            else{
                System.out.println("File: " + fileDialog.getFile());
            }
           }

Also, I'm not sure, but I do believe the normal save function brings up the dialog box. How I would I hypothetically have that not happen?

Thanks for your time

You have most of the required bits there, but just not quite in the right order! Here are some suggestions:
Keep an instance variable for fileName, so in your doSave method you can simply use that without the file dialog stuff on lines 17/18
If that fileName is not null or "" then save the file to that fileName, otherwize call doSaveAs
In doSaveAs, line 4 is wrong. You can't get the file name until after the dialog has been displayed.
In both methods, once you have got a valid file name, you need to call a method that takes that name, creates a File from it, and writes all your text to that file.

ps: awt was officially superceeded in 1998 by Swing. If awt is what your prof asks for then there's not much you can do about it, but be aware that in real life you will need to update your skills to the Swing classes that have been used for over a decade. Your prof should be retired, if not actually shot before he does any more damage.

The whole thing was written in awt, so in order to change it to swing, it seems I'd have to redo certain classes. Also, this professor isn't exactly what you'd call... compitent.

And I take it from what you said he's either messing with the class, or teaching us completely dated information, as it seems that every homework assignment he gives uses a rather old and out of date class.

That's a real shame, but there's nothing you can do about it until you have finished the class. If you want to continue with Java you;ll need to update your knowledge of the GUI classes, but the the basic principles remain the same.

This article has been dead for over six months. Start a new discussion instead.