• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Datas in My text file -Java

 
Chaos Knight
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi can someone help me !

I am stack with this problem.. All I want is that I want to view Employees Data. The Data was save in my Notepad and this is what is in my Notepad.

#Employee.txt



I can show all the data's in my Employee.txt by having this code: ...



this is the output of the program:



But I want that when I run the program It will ask to enter a FullName then If a FullName is entered It will search in my Employee.txt and if found it will display the Data's of the Employee.

I want to have this output:



I am hoping and waiting for an answer.
Thank You..
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chaos,

Welcome to the Ranch

Here's one of the approaches you can think of. You may also divide it into multiple methods, with each method doing a specific task.
1. Get employee name as user input.
2. Assuming, the data in the file is consistent across all the lines, find the user entered name in any line. Lets call it as findEmployee().
3. check if there exists a line in which the user entered name was found - lets call it as exists()
4. if the employee name is found, print it into the console as you are doing for all the employees in a do while loop. lets call this method as printEmployee()
5. if not found, print a message.
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative approach (if the whole file can be loaded and stored in memory and you are planning on looking up multiple names) would be to read in the file, store the details of each employee in instances of an Employee class and then store the instances in a Map keyed on the user name.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chaos Knight,

following code is the code for the output you required

[MODERATOR EDIT] Removed full code solution.

James Peterson, please remember this site is NotACodeMill(←click). Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic