-1

help im stuck.. we are to make two programs the 1st one we ask the user to input how many employees, 1st name, lastname, id,..then store it into a file called names.db. ...i was able to get this done...im stuck on the 2nd program...which suppose to do this....retrieve the employee database by asking the user to input the employees 1st name and if the employee is found then print that info...if not then print not found.

import java.util.Scanner;
import java.io.*;

public class RetrieveInfo
{
    public static void main(String[] args) throws IOException
    {

    //create scanner object for keyboard input
    Scanner keyboard = new Scanner(System.in);

    //open file
    File file = new File("Employee.db");
    Scanner inputFile = new Scanner(file);

    //ask for employee name
    System.out.print("enter the name of the employee. ");
    String firstName =keyboard.nextLine();

    //here is where im stuck...read the file and check of the empoyee is     
    here. We are learning about loops some im pretty sure its going to be a loop


    }

}
RPresle
  • 2,436
  • 3
  • 24
  • 28
fer
  • 7
  • 2

2 Answers2

0

You should probably get through your lines and if the data is in the current line then print it. After reading your file :

String expectedemployee = null
for(String line : Files.readAllLines(Paths.get("/path/to/file.txt")) {

    if(line.contains(firstName) {
         expectedemployee = line;
         break;
    }
}
if(expectedemployee != null) {
    // print
} else {
    // you don't have any employee corresponding
}

Or you can use BufferedReader.

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null || expectedemployee != null) {
       if(line.contains(firstName) {
          expectedemployee = line;
          break;
       }
    }
}
RPresle
  • 2,436
  • 3
  • 24
  • 28
0

Using Database rather than file would be better for this type.if you want files then you should try with Object Input/Output Stream.

RAJKUMAR NAGARETHINAM
  • 1,408
  • 1
  • 15
  • 26