1

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

1
  • 1
    Please, give more information. Which of the columns is designation? Can any of the columns contain whitespaces? Is there anything other special about any of the columns(for instance is the second always 4 digits)? Commented Sep 20, 2012 at 6:30

3 Answers 3

1
  • Use String.substring(start, end) to get the relevant part of the line.
  • Call trim() on it.
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for ur help. if u can provide me a program please provode it
@Javid Make an attempt, ask questions when you get stuck
Thanks for spelling words like 'your' correctly. If you can provide me money, please provide it.
If you know none of the entries will contain spaces you could use String.split(" "), but I prefer Andrew's approach
@Mad So do I! As soon as the data was formatted and it became obvious this was 'fixed column width', substring() just jumped out at me.
1
  • 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.

Comments

1

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.