The code you posted is not working because you only ever read the first line, then loop over that line forever.
Your code, trimmed and annotated:
String line= br.readLine(); // returns the first line of the file
while(line !=null) { // checks if the line is null - it's not at the first line
a= line.split(" "); // deliminator white space
}
// we never get here, because nowhere in the loop do we set line to null
You need to call br.readLine() in a loop until it returns null, something like this:
BufferedReader br=new BufferedReader(new FileReader(fName));
String line= br.readLine(); // reads the first line, or nothing
while(line != null) {
a= line.split(" "); // deliminator white space
String[] arr = line.split("\\s+"); // splits a string by any amount of whitespace
if(arr.length >= 2 && arr[0].equals(lookup)) {
System.out.println(arr[1]);
}
line = br.readLine(); // this will eventually set line to null, terminating the loop
}
The for loop in your original code will not work, if you ever hit it your output would be lee1 or rodney1 respectively. If you changed it to arr[i+1] instead, which I assume you were trying to do, it would crash with an IndexOutOfBoundsException if ever the last item in the array matched pname.
Orginal Answer
This is an ideal use case for a Scanner. It "scans" a string or file for the contents you're looking for, dramatically simplifying file parsing for many use cases, in particular whitespace-delimited files.
public void searchFile(String fName, String lookup){
Scanner in = new Scanner(new File(fName));
// Assumes the file has two "words" per line
while(in.hasNext()){
String name = in.next();
String number = in.next():
if(name.equals(lookup){
System.out.println(number);
}
}
}
If you cannot use scanner to parse each line, you can still use it to simplify reading each line, and then do whatever more complex parsing of the line needs to be done, like so:
public void searchFile2(String fName, String lookup){
Scanner in = new Scanner(new File(fName));
while(in.hasNextLine()){
String line = in.nextLine();
String[] arr = line.split("\\s+"); // splits a string by any amount of whitespace
if(arr[0].equals(lookup)){
System.out.println(arr[1]);
}
}
}
As an aside, if you know the names will be unique, you can use a Map (specifically, a HashMap) to store and lookup mappings like names to numbers efficiently. So instead of having a method which takes a filename and a name to lookup, you have a method which parses the file and returns a mapping of all names to numbers, then you can simply call map.get("name") on the returned map to get a given person's number efficiently, without having to re-read the file every time.