does anyone know why this does not work?

public class AddStudentListener implements ActionListener
     {
        public void actionPerformed (ActionEvent event)
        {
            //Changes title
            Title.setText("Add Student");
            
            //Obtians student information from the user
            Object[] possibilities = {"IB Computer Science", "AP Computer Science", "Algebra II"};
            StudentClass = (String)JOptionPane.showInputDialog(All, "What class is \nthe new student in?", "Info", JOptionPane.PLAIN_MESSAGE, null, possibilities, "IB Compter Science");
            newStudent= (String)Messages.showInputDialog(All, "Please enter the full name\nof the new student.", "Info", JOptionPane.PLAIN_MESSAGE);
            newGrade= (String)Messages.showInputDialog(All, "Please enter the grade\nof the new student.", "Info", JOptionPane.PLAIN_MESSAGE);
            
            //Determines proper file to add information to
            if (StudentClass.equals("IB Computer Science"))
            {
                ClassFile="CompSciIB.txt";
            }
            else if (StudentClass.equals("AP Computer Science"))
            {
                ClassFile="CompSciAP.txt";
            }
            else if (StudentClass.equals("Algebra II"))
            {
                ClassFile="AlgebraII.txt";
            }
            else
            {
                //assume student is in IB Computer Science, the teacher's most popular class
                ClassFile="CompSciIB.txt";
            }
            
            //Writes the new information to the file
            try
            {
                FileOutputStream WritingStream = new FileOutputStream(ClassFile);
                PrintStream printer=new PrintStream(WritingStream);
                printer.println(newStudent);
                printer.println(newGrade);
                WritingStream.close();
            }
            catch (Exception e)
            {
                System.err.println("Error: " + e.getMessage());
            }
        }
    }

I have the strings declared in other places. The program runs but does not write to the file when I check it. Also, does anyone know how to add to an existing file, not just write over it?

Thanks for that bit on the appending. The above segment of code is in an applet and does not work. When I copied and pasted it into another class and simply ran the same code through the console, it worked perfectly. Is there a way to write to a file via applets?

applets are generally run embedded in a web page, so for security reasons, it won't give you access to the client's filesystem. there may be a workaround, but this type of thing wouldn't be a good idea.

Yeah, but what if I just run it in an appletviewer, not a web browser? I really need this to work for my CompSci class.

http://www.captain.at/programming/java/

I haven't tried the example shown, but it looks by creating and accepting a certificate, you can give your applet read/write access. see how that goes.

Thanks but I really don't understand what that website is talking about at all.
PS. I dont know if the issue is with the applet because in an other segment of my code, the program reads from the same file just fine. It is just the writing to the file that is giving it trouble.

but you didn't you in a normal jvm environment the code reads & writes fine?

Yes. If this doesn't work...
My teacher told me this project had to be in an applet and it had to have the database of students saved to a file. She never taught us anything like that link you posted for me. I don't know how she expects me to do this.

Sorry to be bothersome. So the permission code looks like this:

grant {
  permission java.util.PropertyPermission 
	"user.home", "read";
  permission java.io.FilePermission 
	"${user.home}/text.txt", "read,write";
};

How do I modify that to work with my program? Th classname is GradeBook and the three files I am trying to access are called CompSciIB.txt, CompSciAP.txt, and AlgebraII.txt. Once I have created my permission code, where do I put it?

Thanks alot.

I am not certain, since I haven't really done this (I'm essentially just googling on your behalf).

From the looks, I would say you just replicate the FilePermission line 3 times for each of your files. Just note this is not in code, it is simply in a "policy file". good luck again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.