By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

How to find a part of a string in an arraylist ?

P: 1
Hi all,

This steps I made:
1. Read a file in
2. Put each line of the file in an arraylist
3. Make a collection of the arraylist so you can sort them
4. Search for a word

Now my problem is, the text file will be something like this:

001.name:els
002.year:19
003.road:weg
004.roadnr349
005.-------------------
006.name:jan
007.year:69
008.road:disbeekweg
009.road nr:10
010.-------------------


When my function writes this text into this file, it writes automaticly the numbers cause when i sort later on this array, it wil sort by the alfabeth and I want the notes by each user.

So now my question is, is there a solution so I don't have to give up the whole string ( exemple: "006.name:jan") if i want this user and his notes ?

It would be great if I just can say, give me the following 3 lines from the row that has the word "jan" in it ?


My working code:

public static void search(){

try {

ArrayList<String> v = new ArrayList<String>();

FileReader input = new FileReader("doit.txt");
BufferedReader bufRead = new BufferedReader(input);

String line;
int count = 0;

line = bufRead.readLine();
count++;

while (line != null){

v.add(line);
line = bufRead.readLine();
count++;
}

Collections.sort(v);
int whereFound = Collections.binarySearch(v, "008.jan");
//System.out.println( whereFound );


for (int i = 0; i<6; i++){
System.out.println(v.get(whereFound++));
}


bufRead.close();

}catch (ArrayIndexOutOfBoundsException e){

System.out.println("Usage: java ReadFile filename\n");

}catch (IOException e){

e.printStackTrace();
}

}
Nov 23 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.