-4
 Chris
0122222222
Kathryn
0122222222
Jean
0122222222
swaggy
0122222222 

i have succesfull run the textfile and it show me this result . Now, how do i Retrieving the phone number from the file by giving the name for example :

Enter name: chris
phone number: 012222222

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
user130875
  • 87
  • 1
  • 9

1 Answers1

0

Firstly, these links will be helpful for read data from file:

Reading a plain text file in Java

Reading and displaying data from a .txt file

Secondly, these links will be helpful for get input from command line:

How to get input via command line in Java?

How can I get the user input in Java?

And the last, a simple code sample:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader("list.txt"))) {

      String sCurrentLine;
      while ((sCurrentLine = br.readLine()) != null) {
        list.add(sCurrentLine.trim());
      }

      System.out.print("Enter name: ");
      String name = scanner.next();
      for (int i = 0; i < list.size(); i++) {
        String s = list.get(i);
        if (s.equals(name.trim())) {
          System.out.println("phone number: " + list.get(i + 1));
          break;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Community
  • 1
  • 1
javasenior
  • 1,410
  • 1
  • 16
  • 20