Hello

I have been trying to code this for the last few days, and i am unable to come
across a solution for this. Would you please mind helping me out on this?

There are some records in a notepad, which are as follows

1
Paul
Mac
3
1
222 222
1
333 334
2
111 234

etc....

2 is number of contacts, paul is the first name, Mac is last name, 3 is the
number of contacts he has, 1 is an indication that the other line contains a
mobile number, 222 222 this is the mobile number (which was indicated above), 1
is an indication for a mobile number (again), 333 334 is the mobile number it
self, 2 is an indication for Office number, and 111 234 is the office number.

What i need to do here is, to have the first name and last name in One List (as
a key/value pair), and to add the phone numbers under
different categories (mobile, office) in to another List (as a key/value pair).

For Example the first name, last name list Should have the following values ::

ContactList
=======

Paul
Mac
there could be other names here, it depends on the number of records in the 
list.

PhoneList
=======

1
222 222
1
333 334
2
111 234
there could be other names here, it depends on the number of records in the 
list.

How could i do this. Code or a Pseudo code please.

Dani AI

Generated

Short summary: needs to parse a simple text file into Contact objects (first/last name) and group phone numbers by type. is right that a database can help if this becomes a large, queryable dataset, but for a small file a line-oriented parser and in-memory collections are simpler and faster to implement.

A compact data model works well: one Contact object with firstName, lastName and a Map for phone-type -> List of numbers. Example skeleton:

class Contact {
  String firstName;
  String lastName;
  Map<Integer, List<String>> phones = new HashMap<>();
}

Parsing approach (use line-based reads so phone numbers with spaces are preserved). Read the contact count (or loop until EOF if no count), then for each contact read the name lines, the phone-count line, and then that many (type, number) pairs. Example pseudocode:

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
int total = Integer.parseInt(br.readLine().trim());
for (int i = 0; i < total; i++) {
  String first = br.readLine().trim();
  String last  = br.readLine().trim();
  int phoneCount = Integer.parseInt(br.readLine().trim());
  for (int p = 0; p < phoneCount; p++) {
    int type = Integer.parseInt(br.readLine().trim()); // e.g. 1=mobile,2=office
    String number = br.readLine().trim();
    // add number to contact.phones.get(type)
  }
}
br.close();

Sort and output: sort the List<Contact> by last then first (Comparator that compares lastName then firstName). Common pitfalls: avoid mixing Scanner.nextInt() with nextLine() (consumes newlines unexpectedly); always null-check br.readLine() (EOF); trim lines and wrap Integer.parseInt in try/catch for malformed files. Use a database only if persistence, complex queries, or large volumes are required.

Try creating database and use sql to sort these data.
I think this issue related to DBMSs rather than java programing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.