#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Deleting a saved file


    I am trying to write a GPA calculator, that can remember any data that you added to it using its add method for instance if I added gpa.addCourse("ENG 112", 3, "A+"); it would use three different ArrayList to remember that course data. So far I am able to get it to remember an individual data entry, but I also want to be able to remove a course data entry. But if I delete it from the ArrayList it will still be in the file that I am saving it to.

    This is my code so far

    Code:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    
    public class Gpa {
    
    	private ArrayList<String> courses;
    	private ArrayList<Integer> creditHours;
    	private ArrayList<String> grade;
    
    	private File file;
    
    	public Gpa() {
    
    		this.courses = new ArrayList<String>();
    		this.creditHours = new ArrayList<Integer>();
    		this.grade = new ArrayList<String>();
    
    		this.file = new File("ArrayList.ser");
    
    		this.load();
    
    	}
    
    	public void addCourse(String course, int creditHour, String grade) {
    
    		this.courses.add(course);
    		this.creditHours.add(creditHour);
    		this.grade.add(grade);
    
    		this.save();
    
    	}
    
    	public void removeCourse(int courseIndex) {
    		
    		this.courses.remove(courseIndex);
    		this.creditHours.remove(courseIndex);
    		this.grade.remove(courseIndex);
    
    	}
    
    	public void printCourses() {
    
    		for(int i = 0; i < this.courses.size(); i++) {
    
    			System.out.print(this.courses.get(i) + " ");
    			System.out.print(this.creditHours.get(i) + " ");
    			System.out.print(this.grade.get(i));
    			System.out.println("");
    
    		}
    
    	}
    
    	private void save() {
    
    		try {
    
    			FileOutputStream fileOutputStream = new FileOutputStream(file, false);
    			ObjectOutputStream os = new ObjectOutputStream(fileOutputStream);
    
    			boolean isDeleted = file.delete();
    			
    			if(isDeleted) {
    
    				System.out.println("Yaya");
    
    
    			}
    			else {
    				os.writeObject(this.courses);
    				os.writeObject(this.creditHours);
    				os.writeObject(this.grade);
    			}
    			os.close();
    
    		}
    		catch(IOException ex) {
    
    			ex.printStackTrace();
    
    		}
    
    	}
    
    	private void load() {
    
    		try {
    
    			FileInputStream fileInputStream = new FileInputStream(file);
    			ObjectInputStream is = new ObjectInputStream(fileInputStream);
    
    			this.courses = (ArrayList<String>) is.readObject();
    			this.creditHours = (ArrayList<Integer>) is.readObject();
    			this.grade = (ArrayList<String>) is.readObject();
    
    			is.close();
    
    		}
    		catch (IOException e) {
    
    			e.printStackTrace();
    
    		} 
    		catch (ClassNotFoundException e) {
    
    			e.printStackTrace();
    
    		}
    
    
    	}
    
    }
    I know that some of the stuff I am doing with the boolean in the save method doesn't make any sense in its current state, basically I was just playing around and trying to see if I could delete the file from the same method before saving the data again(which didn't work).

    I know my logic may be way off, but I am just learning this stuff.

    Thanks for any help!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Why don't you create something like
    Code:
    private class Student {
        String course;
        Integer creditHours;
        String grade;
        Student ( String c, Integer h, String g ) {
            course = c;
            creditHours = h;
            grade = g;
        }
        // other methods
    }
    Then you have a single arraylist of that class.

    Which makes adding, removing, printing, saving or what have you an atomic operation on a single array element (as far as the outer class is concerned).

    That is, unless using parallel arrays is the whole point of the exercise.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    Why don't you create something like
    Code:
    private class Student {
        String course;
        Integer creditHours;
        String grade;
        Student ( String c, Integer h, String g ) {
            course = c;
            creditHours = h;
            grade = g;
        }
        // other methods
    }
    Then you have a single arraylist of that class.

    Which makes adding, removing, printing, saving or what have you an atomic operation on a single array element (as far as the outer class is concerned).

    That is, unless using parallel arrays is the whole point of the exercise.
    Thank you for the recommendation, I will certainly do that, it will make my code easier to read. However I would still have the problem of removing an entry from the file I am saving to. Do you know a way to delete a file completely. For instance if I have the file ArrayList.ser and I am saving my ArrayList to it how do I delete it so that I can recreate the file without the romoved array element.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    All you wanted to know about File, but were afraid to ask
    Your arraylist is your master copy, which you insert/delete/append to as the program is running.

    When you change arraylist in some way, then you serialise it again to a file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo