Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Quick question pertaining to Arrays


    For my programming assignment I am to basically supposed to be able to write to a text file (used like a calendar). I have all my methods and now I'm working on the main method to put it all together. I'm supposed to be able to addAppointment, removeAppointment, printDay (prints the appointment times for a certain day), printAll (prints all the days + appointments), and quit. For some reason when I go to use addAppointment, it deletes all the information in my text file. What am I doing wrong?

    Here is the method code
    Code:
    public static boolean addAppointment(ArrayList<String> appointmentList, 
    			 							 String specifiedDay,
    			 							 String specifiedTime) throws FileNotFoundException{
    		if(hasAppointmentAt(appointmentList, specifiedDay, specifiedTime)){
    			return false;
    		}
    		String s = specifiedDay + "@" + specifiedTime;
    		appointmentList.add(s);
    		PrintWriter out = new PrintWriter("myCalendar.txt");
    		out.close();
    		for(int i = 0; i < appointmentList.size(); i++){
    			out.println(appointmentList.get(i));
    		}
    		return true;
    and then what I have for the main method
    Code:
    System.out.println("Please enter one of the following:");
    		System.out.println("addAppointment, removeAppointment, printDay, printAll, quit");
    		Scanner stdin = new Scanner(System.in);
    		String userInput = stdin.next();
    		
    		while(userInput != "quit"){
    			//addAppointment
    			if(userInput.equals("addAppointment")){
    				System.out.println("Please enter a day and time");
    				String day = stdin.next(); //Specifies the day, Monday-Sunday
    				String time = stdin.next(); //Specifies the time
    				if(hasAppointmentAt(readFromFile(), day, time) == true){
    					System.out.println("There is another appointment at this time, can not add.");
    				}
    				while(hasAppointmentAt(readFromFile(),day,time) == false){
    				addAppointment(readFromFile(),day, time);
    				
    				}
    The hasAppointmentAt states whether or not I have an appointment at that time, if I do, it returns true, if I don't it returns false. Any help would be appreciated. Thanks!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    it deletes all the information in my text file
    Are you saying that every time you create a new text file the contents of the old text file is overwritten with the new data?
    Try using the FileOutptuStreaam i/o class with a constructor that has an option to APPEND the data to an existing file instead of replacing the file's contents.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by NormR
    Are you saying that every time you create a new text file the contents of the old text file is overwritten with the new data?
    Try using the FileOutptuStreaam i/o class with a constructor that has an option to APPEND the data to an existing file instead of replacing the file's contents.
    Whenever I try to edit the existing file it deletes all of the content that was previously in the file. For example, we were to type in Monday@5PM, Tuesday@2PM, Friday@6PM. When I try to add an appointment it deletes all of that information and leaves the file blank.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Did you try using the append option for the output file?

    Can you make a small, complete program that compiles, executes and shows the problem?

    Code:
    out.close();
    That statement closes the file for output. I don't know why any attempt to write to the file after that is allowed.
    Last edited by NormR; April 24th, 2013 at 08:00 AM.
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    One thing I notice in your method is this.

    Code:
    PrintWriter out = new PrintWriter("myCalendar.txt");
    		out.close();
    		for(int i = 0; i < appointmentList.size(); i++){
    			out.println(appointmentList.get(i));
    		}
    You're creating the PrintWriter to write to the file, then immediately close it, and then write to it.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    I've commented all of my main method out and just ran the addAppointment(readFromFile(), "Monday", "6PM") method with the parameters. I switched the out.close to within the loop, outside of the loop and even commented it out. Also, I used the out.append and it is still truncating the file
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    Have you tried using a FileWriter instead of PrintWriter? It has a constructor that allows you to append.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    Code:
    public static boolean addAppointment(ArrayList<String> appointmentList, 
    			 							 String specifiedDay,
    			 							 String specifiedTime) throws IOException, FileNotFoundException{
    		if(hasAppointmentAt(appointmentList, specifiedDay, specifiedTime)){
    			return false;
    		}
    		String s = specifiedDay + "@" + specifiedTime;
    		appointmentList.add(s);
    		//PrintWriter out = new PrintWriter("myCalendar.txt");
    		FileWriter out = new FileWriter("myCalendar.txt");
    		//out.close();
    		for(int i = 0; i < appointmentList.size(); i++){
    			out.append(appointmentList.get(i));
    			//out.close();
    		}
    		//out.close();
    		return true;
    Everywhere you see the //out.close() I've tried using (not all at once, but one at a time)
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Use the close() method AFTER all writing has been completed and the program is done writing anything to the file.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    I guess I'm just confused as to what you mean by that
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    When writing to a file, do NOT close the file until all the writing has been done to the file.

    Open the file
    begin loop
    write to the file
    end loop
    close the file
  22. #12
  23. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    What I meant about the FileWriter is that is has a constructor that accepts a string for filename and a boolean. If the boolean is true, then instead of overwriting existing content, content is appended. Generally what I do is create the FileWriter, and then wrap that in a PrintWriter. Then when you call println on the PrintWriter instance, what you will write will be appended to the file.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    I talked to one of my classmates and he said that what he did was have it overwrite everything and then have the information added back in with a loop
  26. #14
  27. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    What is the content of appointmentList?
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    Code:
    public static ArrayList<String> readFromFile() throws FileNotFoundException{
    
    			
    		File f = new File("myCalendar.txt");
    		Scanner fileIn = new Scanner(f);
    		ArrayList<String> myAppointmentList = new ArrayList<String>();
    		while(fileIn.hasNext())
    		{
    			myAppointmentList.add(fileIn.next());
    	}
    		return myAppointmentList;
    	}
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo