Javid
Javid

Reputation: 11

Java Program to read text file

I have a text file like

vinoth   5001   chennai    programmer
ramesh   7755   madurai    tester
suresh   7452   namakal    designer
salim    4652   salem      programmer

I want to find with their designation but want to save their other details in an array. how can i do that? any suggestions

Upvotes: 1

Views: 398

Answers (3)

Frankline
Frankline

Reputation: 40985

Read each line using buffered reader and then do that:

HashMap<String, String> values = new HashMap<String, String>;

// read the line here
String line = ...;
String strings[] = line.split(" ");
String designation = strings[3];
values.put(strings[0], strings[1], strings[2]);

Upvotes: 1

Alex
Alex

Reputation: 25613

  • Read the file line by line
  • call String.split to split the different informations into an array
  • You can store these information in a Map whose key would be the username vinoth and values would be the array you just received from String.split
  • Even better you store these informations in an object that represents something with the properties you'd like.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168815

  • Use String.substring(start, end) to get the relevant part of the line.
  • Call trim() on it.

Upvotes: 1

Related Questions