1
 codes = new Vector<String>();
 titles = new Vector<String>();
 urls = new Vector<String>();
 lecturers = new Vector<String>();
 while (m.find()) {
    String courseCode = m.group(1);
    String courseTitle = m.group(2);
    String courseURL = url;
    String lecturerName = m.group(4);
    codes.add(courseCode);
    titles.add(courseTitle);
    urls.add(courseURL);
    lecturers.add(lecturerName);
 }

I'm trying to get data from like 10 websites and it works alright if you just print out each group by itself eg: system.out.println(courseCode); prints out a list of 10 courseCodes but when I try to add them into these vectors it only adds the last courseCode instead of each one. So each vector SHOULD have like 10 elements but they only have 1. Is there a way to like iterate through the matches?

2
  • Hard to say without seeing more code. My guess is that you're instantiating the Vectors inside a loop. Commented Mar 6, 2010 at 19:53
  • This was rightt, thankyouu very much! Too bad you didn't post this as an answer lol so I can't mark any as correct :[ Hate it when I do this though lol, post a question and the answer ends up being something really obvious! Commented Mar 6, 2010 at 21:22

1 Answer 1

1

Maybe the regex matches only one time instead of 10 times. You can check this if you count how often you iterate throught the while loop. The easiest way is to define a help variable int i=0; and increase this value inside the loop with i++; (and print it inside or outside the loop). Also check the size of the Vectors with list.size() inside the while loop to see how the size is actually growing.

Sign up to request clarification or add additional context in comments.

1 Comment

i know it matches more than once because if i type system.out.println(courseCode); instead of codes.add(courseCode); it will print what i expect, but for some reason if i use codes.add(courseCode) it will only add the last match and no others and the size is always 1.

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.