Results 1 to 9 of 9
  1. #1
    Anthony5347 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Using the IO package

    So I made a project for myself. To type in a favorite color and store it in a file. The only problem is that when I write the data to the file its not written in it. I have found that when I do not use the do-while loop it works fine. Thats the thing though; I want to use the do-while loop to ask if the user has more info to enter. I have been tinkering with it, but I cannot figure it out.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    //Going to use io class to write data to a file.
    
    
    public class favoriteColors
    {
       public static void main(String[] args)throws IOException 
       {
          //Variable Declaration and create a Scanner object
          String color,input, fileName;
          char repeat;
          Scanner userInput = new Scanner(System.in);
          
         /** 
          System.out.print("Enter the name of the file: ");
          fileName = userInput.nextLine();
         */ 
          
          //Open the file
          PrintWriter outputFile = new PrintWriter("FavoriteColors.txt");
         
          do
          {    
             System.out.println("Enter your favorite color: ");
             color = userInput.nextLine();
             
              //write the data to the file 
             outputFile.println(color);
              
             System.out.println("Do you have another color you want to enter?");
             System.out.print("Please enter only Y/y or N/n: ");
             input = userInput.nextLine();
             repeat = input.charAt(0);
             
             
             //input validiation if user enters anything except y or n
             while(repeat != 'Y' && repeat != 'y' && repeat != 'n' && repeat != 'N' )
             {
                System.out.println("Invalid letter.");
                System.out.println("Please enter either Y/y or N/n");
                input = userInput.nextLine();
                repeat = input.charAt(0);
             }
             
             //terminate the program if the user enters n
             if (repeat == 'N' || repeat == 'n')
                System.exit(0);
                
                
          }while (repeat == 'Y' || repeat == 'y');   
          
          outputFile.close();   
       
       }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Using the IO package

    The problem you are experiencing is because you are exiting the loop without closing the file. The does
    a flush which empties the output buffer. You may also want to convert your repeat input prompt to either
    upper or lower case to make you checks easier.

    And by convention, class names start with an upper case letter.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Anthony5347 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Using the IO package

    So basically by placing the outputFile.close(); inside the do-while loop it should work? Yea I was going to fix the repeat input prompt, but first I wanted to get this issue worked out. Thanks alot for the feedback!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Using the IO package

    Quote Originally Posted by jim829 View Post
    The problem you are experiencing is because you are exiting the loop without closing the file.
    Are you sure?
    I'm looking at the code and the pseudo is:
    Java Code:
    open file
    do while user wants to
        print colour to file
    close file
    so the close is there in the code, exactly where it should be if the idea is to allow several colours to be entered and added to the file.

    The question is, when does the OP actually look in the file?
    If it's while the code is executing then the stuff will not be visible as it's likely in the buffer.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Using the IO package

    Quote Originally Posted by Tolls View Post
    Are you sure?
    Note the System.exit()
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Using the IO package

    Ahhhh!
    So it's not that they're exiting the loop...they're shutting down the app.
    Remove that and the whole thing should work as required.
    After all, it's a rather pointless line.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Using the IO package

    Quote Originally Posted by Tolls View Post
    So it's not that they're exiting the loop...they're shutting down the app.
    Once again I should have been more precise.. sigh...

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Using the IO package

    Quote Originally Posted by jim829 View Post
    Once again I should have been more precise.. sigh...

    Regards,
    Jim
    And I need new glasses...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Using the IO package

    Group hug!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 07-29-2013, 09:21 AM
  2. Replies: 2
    Last Post: 03-26-2013, 06:07 PM
  3. Replies: 3
    Last Post: 03-27-2012, 09:27 AM
  4. run package inside anthor package
    By AhmedAdel in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2010, 11:52 AM
  5. Replies: 7
    Last Post: 11-09-2009, 07:51 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
  •