BlueJ trouble or program trouble (Combining Arraylists)
I'm doing is on BlueJ and it won't work. I've waited for 15 min. It compiles and there's no error when I hit main(), but it just keeps loading. Is it my code (does it have a super slow runtime?). If it is my code, I think that it is the way I combined the two Arraylists. Is there a better way to combine the lists is ascending order?
Code :
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class MergeInventoriesTester
{
private ArrayList<Product> Store1;
private ArrayList<Product> Store2;
private Product store1;
private static Product store2;
public static void main() throws FileNotFoundException
{
ArrayList<Product> Store1 = new ArrayList<Product>();
ArrayList<Product> Store2 = new ArrayList<Product>();
getTextFile(Store1, Store2);
mergeArrayLists(Store1, Store2);
}
public static void getTextFile(ArrayList<Product> Store1, ArrayList<Product> Store2)throws FileNotFoundException
{
String id;
String quantity;
String unitPerPrice;
Scanner inF = new Scanner(new File("C:\\Users\\Caili\\Documents\\Sophmore\\AP Computer Science\\Chapter 14\\Store1.txt"));
while(inF.hasNextLine())
{
id = inF.nextLine();
quantity = inF.nextLine();
unitPerPrice = inF.nextLine();
Store1.add(new Product(id, quantity, unitPerPrice));
}
Scanner inE = new Scanner(new File("C:\\Users\\Caili\\Documents\\Sophmore\\AP Computer Science\\Chapter 14\\Store2.txt"));
while(inE.hasNextLine())
{
id = inE.nextLine();
quantity = inE.nextLine();
unitPerPrice = inE.nextLine();
Store2.add(new Product(id, quantity, unitPerPrice));
}
}
public static void mergeArrayLists(ArrayList<Product> Store1, ArrayList<Product> Store2) throws FileNotFoundException
{
ArrayList<Product> Merged = new ArrayList<Product>();
int a = 0;;
int b = 0;
while(a < Store1.size()&& b < Store2.size())
{
if(Store1.get(a).returnID().compareTo(Store2.get(B).returnID())==1)
{
Merged.add(Store1.get(a));
a++;
}
if(Store1.get(a).returnID().compareTo(Store2.get(B).returnID())==-1)
{
Merged.add(Store2.get(B));
b++;
}
if(Store1.get(a).returnID().compareTo(Store2.get(B).returnID())==0)
{
Merged.add(Store1.get(a));
a++;
b++;
}
}
for(int i=0; i<Merged.size();i++)
{
Product person = Merged.get(i);
System.out.println(person.returnID());
}
}
}
Code :
public class Product
{
private String id;
private double quantity;
private double unitPerPrice;
private double value;
public Product(String num,String amount,String perPrice)
{
id = num;
quantity = Double.parseDouble(amount);
unitPerPrice = Double.parseDouble(perPrice);
}
public String returnID()
{
return id;
}
}
Re: BlueJ trouble or program trouble (Combining Arraylists)
Quote:
it just keeps loading
Do you mean the program goes into a loop and stops responding?
Otherwise please explain what "keeps loading" means.
Add some println statements that will show where the program is executing.
Re: BlueJ trouble or program trouble (Combining Arraylists)
If you wanna merge 2 lists, why dont you just use:
Collections.copy(destinationList, sourceList);
Re: BlueJ trouble or program trouble (Combining Arraylists)
Does the copy method merge or overlay the destination list?