By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

File Writing

P: 2
hi, im relatively new at programming.
Im using bluej.

i have manged to write the following data to a file.

0213 - Sumair Seth
9801 - John Smith
7373 - Jason Porter
1283 - Allison Fairth

I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
Jan 13 '07 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
hi, im relatively new at programming.
Im using bluej.

i have manged to write the following data to a file.

0213 - Sumair Seth
9801 - John Smith
7373 - Jason Porter
1283 - Allison Fairth

I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
Have you written any code to read the file. Post the code you have written so far.
Jan 13 '07 #2

P: 2
yes this is the code to enter the details into the file:

Expand|Select|Wrap|Line Numbers
  1.  import java.io.*;
  2. public class RegisterEmployees
  3. {
  4. static    InputStreamReader in=new InputStreamReader(System.in);
  5. static     BufferedReader br = new BufferedReader(in);
  6.     public static void enterEmployeeNames() throws IOException
  7.     {
  8.         File EmpDatabase = new File("C:\\Documents and Settings\\All Users\\Desktop");
  9.  
  10.         BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\All Users\\Desktop\\Database.txt", true));
  11.         System.out.println("Kindly Enter Number of Employees whose records need to be Entered");
  12.         int numberofUpdates = Integer.parseInt(br.readLine());
  13.         for(int i=1;i<=numberofUpdates ; i++)
  14.         {
  15.  
  16.         System.out.println("Enter four digit Employee Code");
  17.        String EmpCode = br.readLine();
  18.  
  19.        if(EmpCode.length()<4)
  20.        {
  21.         System.out.println("Code to small please begin again and enter a four digit code");
  22.     enterEmployeeNames();    
  23.     }
  24.     if(EmpCode.length()>4)
  25.        {
  26.         System.out.println("Code to large please begin again and enter a four digit code");
  27.     enterEmployeeNames();    
  28.     }   
  29.     out.write(EmpCode);
  30.  
  31.         out.write(" - ");
  32.         System.out.println("Enter Employee Name");
  33.         out.write(br.readLine());
  34.                 System.out.println("Thank You, Employee Database has been updated");
  35.     }
  36. out.write("\n");
  37.     out.close();     }
  38.  
  39.     }
  40.  


basically the number will always be 4 digits, to make reading it easier. no i havent written any code to read the file as of now
Jan 13 '07 #3

10K+
P: 13,264
yes this is the code to enter the details into the file:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class RegisterEmployees
  3. {
  4. static InputStreamReader in=new InputStreamReader(System.in);
  5. static BufferedReader br = new BufferedReader(in);
  6. public static void enterEmployeeNames() throws IOException
  7. {
  8. File EmpDatabase = new File("C:\\Documents and Settings\\All Users\\Desktop");
  9.  
  10. BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\All Users\\Desktop\\Database.txt", true));
  11. System.out.println("Kindly Enter Number of Employees whose records need to be Entered");
  12. int numberofUpdates = Integer.parseInt(br.readLine());
  13. for(int i=1;i<=numberofUpdates ; i++)
  14. {
  15.  
  16. System.out.println("Enter four digit Employee Code");
  17. String EmpCode = br.readLine();
  18.  
  19. if(EmpCode.length()<4)
  20. {
  21. System.out.println("Code to small please begin again and enter a four digit code");
  22. enterEmployeeNames(); 
  23. }
  24. if(EmpCode.length()>4)
  25. {
  26. System.out.println("Code to large please begin again and enter a four digit code");
  27. enterEmployeeNames(); 
  28. out.write(EmpCode);
  29.  
  30. out.write(" - ");
  31. System.out.println("Enter Employee Name");
  32. out.write(br.readLine());
  33. System.out.println("Thank You, Employee Database has been updated");
  34. }
  35. out.write("\n");
  36. out.close(); }
  37.  
  38. }
  39.  


basically the number will always be 4 digits, to make reading it easier. no i havent written any code to read the file as of now
Write the part for reading the file post that. You will need to use
Expand|Select|Wrap|Line Numbers
  1.  
  2. BufferedReader br = new BufferedReader(new FileReader(String nameOfFile));
  3.  
Then you read a line using
Expand|Select|Wrap|Line Numbers
  1. br.readLine();
Jan 13 '07 #4

Expert 100+
P: 1,510

I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
Are you talking about another program which is reading this data or do you need to create an array within this program to hold the employer code?
In the latter case when you write the employer code
Expand|Select|Wrap|Line Numbers
  1.     out.write(EmpCode);
  2.  
you could store the value in a vector, e.g.
Expand|Select|Wrap|Line Numbers
  1.     Vector<Integer> v = new Vector<Integer>();
  2.    ....
  3.     out.write(EmpCode);
  4.     Integer i=new Integer(EmpCode);
  5.     v.add(i);
  6.  
Jan 13 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.