Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Create a Text file with java.

  1. #1
    javaforums1 is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Create a Text file with java.

    hi friends

    i am new in java . I'm tring to know java thats why i'm reading a book "Java: How to Learn program by Detiel and detiel " .there is code for Create Text file for sequential data access in this book's chapter 17.

    here i can create text file but i could not input any data in that file . When input data and type <ctrl>z it shows me" invalid data". I think the try block of AddRecord method doesnot work .Where is my wrong ? can any body help me? here my code:

    Java Code:
    public class AccountRecord {
        private int account;
        private String firstName;
        private String lastName;
        private double balance;
        
        public AccountRecord(){this(0," "," ",0.0);}
        
        public AccountRecord(int acct,String first,String last,double bal )
        {setAccount(acct);setFirstName(first);setLastName(last);setBalance(bal);}
        public void setAccount(int acct){account=acct;}
        public int getAccount(){return account;}
        public void setFirstName(String first){firstName=first;}
        public String getFirstName(){return firstName;}
        public void setLastName(String last){lastName=last;}
        public String getLastName(){return lastName;}
        public void setBalance(double bal){balance=bal;}
        public double getBalance(){return balance;}
    }
    Java Code:
    package createtextfiletest;
    
    import com.sun.xml.internal.ws.api.pipe.NextAction;
    import java.io.FileNotFoundException;
    import java.util.Formatter;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class CreateTextFile {
        
        public Formatter output;
        
        public void OpenFile()
        {
            
            try {
                    output = new Formatter("Clients.txt");
                } 
            catch (FileNotFoundException ex) {
                    System.err.printf("\n File Not Found");
                    System.exit(1);
                }
    
        }
        
       
        
        public void AddRecord()
        {
        Scanner input = new Scanner(System.in);
        AccountRecord rcd= new AccountRecord();
            System.out.printf("\nFor windows type <ctrl> z press enter for end of data indicator\n");
            System.out.printf("Enter account no>0,firstname,lastName, and balace\n");
            while(input.hasNext())
            {
            try
            {
                rcd.setAccount(input.nextInt());
                rcd.setFirstName(input.next());
                rcd.setLastName(input.next());
                rcd.setBalance(input.nextDouble());
                
                if(rcd.getAccount()>0){
                
                    output.format("%d %s %s %.2f\n",rcd.getAccount(),rcd.getFirstName(),rcd.getLastName()
                            ,rcd.getBalance());
                }
                else{
                
                    System.out.printf("\n Invalid input. Try again");
                    input.nextLine();
                    
                }
            }
            
            catch(Exception X){
            System.out.printf("\n Invalid data\n");
            input.nextLine();
           System.out.printf("Enter account no>0,firstname,lastName, and balace\n");
            
            }
            
            }
            
            
            
        }
        
         public void CloseFile()
        {if (output !=null)
            output.close();
        
        }
        
    }


    Java Code:
    package createtextfiletest;
    
    public class CreateTextFileTest {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            CreateTextFile app =new CreateTextFile();
            app.OpenFile();
            app.AddRecord();
            app.CloseFile();
            
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Create a Text file with java.

    You might want to reply to answers to your previous question in this forum, as doing this shows that you're taking the time to read the replies of others and often motivates folks to help you further.
    DarrylBurke likes this.

Similar Threads

  1. Replies: 12
    Last Post: 09-23-2012, 03:36 PM
  2. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  3. How to create an exe file in java
    By pads in forum New To Java
    Replies: 4
    Last Post: 07-09-2010, 05:20 PM
  4. How to create a hyperlink from a text in Java
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-05-2009, 02:12 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •