Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Bag empty errors

807603Oct 8 2007 — edited Nov 23 2007
I have an assignment that requires you to have two sets (A and B). The user inputs the max number of elements in a set and then the user enters a string which I tokenize and put into the Bag. I have methods that get the union, intersection, and difference of the different bag sets. If I call multiple methods I either get an error:

(Exception in thread "main" java.util.NoSuchElementException: Bag grab(): bag empty
at ds.util.Bag.grab(Bag.java:184)
at ch08.mckeown.DemoSets.difference(DemoSets.java:119)
at ch08.mckeown.DemoSets.main(DemoSets.java:74)

or the wrong answer.

I think my methods mess with the bags params that are passed in. I'm not sure how to solve this problem...Here is my code.
package ch08.mckeown;

import ds.util.Bag;
import ds.util.Iterator;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.lang.String;
import java.util.ArrayList;

/*
 * User enters the items for each of two sets of doubles.
 * The program forms their Union, Intersection, and Difference sets.
 * The program displays all the sets involved using the Bag toString() method.
 * 
 * CSC241-01-Ahlborn
 * 10-9-07
 * @author Tim McKeown
 */

public class DemoSets
{

	public static void main(String[] args)
	{   
		//String tokenizer for setA
		StringTokenizer tokenizedSetA; 
		
		//String tokenizer for setB
	    StringTokenizer tokenizedSetB;	
	    
	    //Creates a scanner object to user input
		Scanner keyboard = new Scanner(System.in);
		
		//Size parameter for bagB
		int numMaxB = 0;
		
		//Size parameter for bagA
        int numMaxA = 0;
        
        //The double converted from the token to be added to the bags
        double numAdd;  
        
        //String to be tokenized and added to setA
        String numAddSetA;
        
        //String to be tokenized and added to setB
        String numAddSetB;
     
        //Asks the user for max elements in setA and reads in with numMaxA
        System.out.println("Maximum number of elements in SetA: ");
		numMaxA = keyboard.nextInt();
		keyboard.nextLine();
		
		//Creates bagA with the correct parameters
		Bag<Double> bagA = new Bag<Double>(numMaxA);
		 
		System.out.println("Enter elements for SetA, pair-wise separated " +
									"by commas: ");	
		numAddSetA = keyboard.nextLine();
		
		   //Adds tokens from the string numAddSetA with delimiter ", "
           tokenizedSetA = new StringTokenizer(numAddSetA, ", ");
           while (tokenizedSetA.hasMoreTokens())
           {
        	   numAdd = Double.parseDouble(tokenizedSetA.nextToken());
        	   bagA.add(numAdd);      	   
           }  
         
         //Asks the user for max elements in setB and reads in with numMaxB   
        System.out.println("Maximum number of elements in SetB: ");
		numMaxB = keyboard.nextInt();
		keyboard.nextLine();
		
		//Creates bagB with the correct parameters
		Bag<Double> bagB = new Bag<Double>(numMaxB);
		
		System.out.println("Enter elements for SetB, pair-wise separated " +
									"by commas: ");	
		numAddSetB = keyboard.nextLine();
		
		   //Adds tokens from the string numAddSetB with delimiter ", "
           tokenizedSetB = new StringTokenizer(numAddSetB, ", ");  
           while (tokenizedSetB.hasMoreTokens())
           {
        	   numAdd = Double.parseDouble(tokenizedSetB.nextToken());
        	   bagB.add(numAdd);
           }
           
       //Displays all the required content for the project   
       System.out.println("Bag B elements: " + noDupes(bagB));
       System.out.println("Bag B elements: " + noDupes(bagA));
       System.out.println("Union A and B: " + noDupes(union(bagA, bagB)));
       System.out.println("Intersection: " + noDupes(intersection(bagA, bagB)));
       System.out.println("A-B: " + noDupes(difference(bagA, bagB)));
       System.out.println("B-A: " + noDupes(difference(bagB, bagA)));
	}
	 
	/*
	 * The union method will take both bags and combine them
	 * 
	 * @param a bag that is added to setUnion
	 * @param another bag that is added to setUnion
	 */
	
	 public static <T> Bag<T> union(Bag<T> setA, Bag<T> setB)
	 {
		 //Create and initialize setUnion
		 Bag<T> setUnion = new Bag<T>(setA.size() + setB.size());
		 
		 //Create object  item with T
		 T item;
		 
		 //Adds the items in setA to setUnion
		 do{
			 item = setA.grab();
			 setUnion.add(item);
			 setA.remove(item);
		 }while(!setA.isEmpty());
		 
		 //Adds the items in setB to setUnion
		 do{
			 item = setB.grab();
			 setUnion.add(item);
			 setB.remove(item);
		 }while(!setB.isEmpty());
			  
		 return setUnion;
	 }
	 
	 /*
	  * The intersection method will take a bag parameter (setA) and 
	  * compare it with the elements in the second bag parameter (setB).  
	  * If any items match then they will be added to the setIntersection bag.
	  * 
	  * @param the bag that grabs an item to be compared
	  * @param the bag that sees if the item is contained in
	  */
	 
	 public static <T> Bag<T> intersection(Bag<T> setA, Bag<T> setB)
	 {
		 //Create and initialize setIntersection
		 Bag<T> setIntersection = new Bag<T>(setA.size() + setB.size());
		 
		 //Create object item with T
		 T item;
		 
		 //Adds item to intersection if the random element from setA is 
		 //contained in setB
		 do{
			 item = setA.grab();
			 if(setB.contains(item))
			 {
				 setIntersection.add(item);
			 }
			setA.remove(item);
			 
		 }while(!setA.isEmpty());
		 
		 return setIntersection;
	 }
	 
	 /*
	  * Takes the first bag parameter(setA) and subtracts the elements from the
	  * second bag parameter(setB).
	  * 
	  * @param the bag to be subtracted from
	  * @param the bag to be subtracted
	  * @return the bag that takes setA and subtracts elements from setB
	  */
	 
	 public static <T> Bag<T> difference(Bag<T> setA, Bag<T> setB)
	 {
		 Bag<T> setDifference = new Bag<T>(setA.size() + setB.size());
		 T item;
		 do{
			 item = setA.grab();
			 if(!setB.contains(item))
			 {
				 setDifference.add(item);
			 }
			 setA.remove(item);
		 }while(!setA.isEmpty());
		 
		 return setDifference;
	 }
	 
	 /*
	  * The noDupes method will take a Bag<T> setX parameter and grab an
	  * element from setX and compare to noDupe 
	  */
	 
	 public static <T> Bag<T> noDupes(Bag<T> setX)
	 {
		 Bag<T> noDupeBag = new Bag<T>(setX.size());
		 T item;
		 do{
			 item = setX.grab();
			 if(noDupeBag.contains(item))
			 {
				 noDupeBag.remove(item);
			 }
			 if(!noDupeBag.contains(item))
			 {
				 noDupeBag.add(item);
			 }
			 setX.remove(item);
		 }while(!setX.isEmpty());
		 
		 return noDupeBag;
		 
	 }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 21 2007
Added on Oct 8 2007
5 comments
177 views