Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: BlueJ trouble or program trouble (Combining Arraylists)

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    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());
            }
     
       }
    }
    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;
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: BlueJ trouble or program trouble (Combining Arraylists)

    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.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ trouble or program trouble (Combining Arraylists)

    If you wanna merge 2 lists, why dont you just use:
    Collections.copy(destinationList, sourceList);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: BlueJ trouble or program trouble (Combining Arraylists)

    Does the copy method merge or overlay the destination list?

Similar Threads

  1. Having trouble with my program, can someone please help?
    By cbutcher0007 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 21st, 2012, 12:48 PM
  2. Trouble sending an email from my program
    By java99 in forum Java Networking
    Replies: 5
    Last Post: January 11th, 2012, 11:22 PM
  3. Trouble sending an email from my program
    By java99 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 11th, 2012, 05:27 PM
  4. Trouble with Java payroll program
    By Shawn Wray in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2012, 12:57 AM
  5. Trouble with law of Cosine program
    By Delstateprogramer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 06:07 PM