Results 1 to 2 of 2

Thread: Homework Help

  1. #1
    Cyclone16 is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Homework Help

    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
    Java 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
    Java 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!

    EDIT: If I somehow put this in the wrong section, would a mod please move it. Thanks.
    Last edited by Cyclone16; 04-24-2013 at 07:43 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Homework Help

    Java Code:
    PrintWriter out = new PrintWriter("myCalendar.txt");
    According to the PrintWriter API docs the String argument "myCalendar.txt" is The name of the file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

    All of the PrintWriter constructors that take files or filenames as arguments seem to have this behaviour. Perhaps you could create a FileOutputStream and use that to create the PrintWriter.

Similar Threads

  1. Homework Help please!
    By ghjk in forum New To Java
    Replies: 5
    Last Post: 03-19-2012, 03:44 AM
  2. Need help with homework
    By bkim33 in forum New To Java
    Replies: 9
    Last Post: 02-11-2011, 05:50 AM
  3. Homework help
    By rclausing in forum New To Java
    Replies: 26
    Last Post: 11-24-2009, 07:06 AM
  4. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 04:37 PM
  5. help with homework
    By pinkdiamondgail in forum Advanced Java
    Replies: 7
    Last Post: 04-07-2009, 02:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •