Reputation: 11
I am trying to retrieve some data from two different websites and want to store them in different arraylists for further processing. So in summary, I have 6 String arrayLists and then I have three arraylists to whom I have added these String arrayLists. I have done so to add data to the String arrayLists using a loop (please see code below). It however is not adding data to the String arrayLists in the manner I expected, obviously my logic is wrong. Just a little guidance would be much appreciated. The relevant Snippets of my code are below :
//creating the String type ArrayLists
private static ArrayList<String> m1=new ArrayList<String>();
private static ArrayList<String> re1=new ArrayList<String>();
private static ArrayList<String> ra1=new ArrayList<String>();
private static ArrayList<String> m2=new ArrayList<String>();
private static ArrayList<String> re2=new ArrayList<String>();
private static ArrayList<String> ra2=new ArrayList<String>();
//creating three ArrayLists of ArrayLists
private static ArrayList<ArrayList> arraylistM = new ArrayList<ArrayList>();
private static ArrayList<ArrayList> arraylistRe = new ArrayList<ArrayList>();
private static ArrayList<ArrayList> arraylistRa = new ArrayList<ArrayList>();
public static void main(String[] args) throws Exception
{
URL[] item=new URL[2];
item[0] = new URL("http://www.example111111.html");
item[1] = new URL("http://www.example222222.html");
arraylistM.add(m1);
arraylistM.add(m2);
arraylistRe.add(re1);
arraylistRe.add(re2);
arraylistRa.add(ra1);
arraylistRa.add(ra2);
for(int loop=0;loop<2;loop++)
{
BufferedReader in = new BufferedReader(
new InputStreamReader(
item[loop].openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
String withoutSpeechMarks=inputLine.replaceAll("\"", "");
String[] parts = withoutSpeechMarks.split("<span x=");
for (int i=0;i<parts.length;i++)
{
if (parts[i].contains("1>"))
{
(arraylistM.get(i)).add(parts[i].substring(13, (parts[i].length())-7));
}//get the first string array in arraylistM and add this data to it
if (parts[i].contains("2>"))
{
(arraylistRe.get(i)).add(parts[i].substring(9, (parts[i].length())-10));
}
if (parts[i].contains("3>"))
{
(arraylistRa.get(i)).add(parts[i].substring(7, (parts[i].length())-7));
}
}//end of inner for loop
} //end of while
in.close();
}
for (int i = 0; i < arraylistM.size(); i++)
{
System.out.println(arraylistM.get(i)+"||"+arraylistRe.get(i)+"||"+arraylistRa.get(i) );
}
The way I want it to be is that from the first website, the extracted results should be added to the string arrays m1, re1, ra1 and then from the second website the data should be added to the string arrays m2, re2, ra2. The data IS being added but all together and is not separated as I wish it to be.
Upvotes: 0
Views: 849
Reputation: 356
It seems that you are facing issue while merging two Arraylist. You can easily join two ArrayList using addAll method.
ArrayList<String> al1=new ArrayList()<String>;
ArrayList<String> al2= new ArrayList()<String>;
al1.addAll(al2);
It would add all the elements of second arraylist to the first arraylist
Upvotes: 0
Reputation: 234795
From your description, it sounds like you should be using
arrayListM.get(loop).add( ... )
arrayListRe.get(loop).add( ... )
arrayListRa.get(loop).add( ... )
in the first loop instead of
arrayListM.get(i).add( ... )
arrayListRe.get(i).add( ... )
arrayListRa.get(i).add( ... )
Also, you should consider declaring your ArrayLists of ArrayLists as:
ArrayList<ArrayList<String>>
instead of
ArrayList<ArrayList>
Upvotes: 2