Results 1 to 9 of 9
  1. #1
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Question Help with ArrayList

    I've been assigned a program in which I make a Spy Class, each of which has certain perameters, including an array list of aliases. Furthermore, there are SpyRings, which contain spies. A final driver class prints out all elements of a spyring. When I attempt to print the SpyRing, it compiles w/ no errors, but prints nothing. Any help?

    Here's the Spy Class:
    Java Code:
    import java.util.*;
    public class Spy
    {
    	private String inname;
    	private String inspyNum;
    	private  static ArrayList<String> aliases;
    
    public Spy(String spyNum, String name)
    {
    	inname=name;
    	inspyNum=spyNum;
    	aliases = new ArrayList<String>();
    }
    
    public String toString()
    {
    	return "Name: "+inname +" Number: "+inspyNum+" Aliases: "+aliases;
    }
    
    public static void addAlias(String newAlias)
    {
    	aliases.add(newAlias);
    
    }
    
    public String getName()
    {
    	return inname;
    }
    
    public String getSpyNum()
    {
    	return inspyNum;
    }
    
    public ArrayList<String> getAliases()
    {
    	return aliases;
    }
    
    
    
    }
    Here's the SpyRing class:

    Java Code:
    import java.util.*;
    public class SpyRing
    {
    	private static  ArrayList<Spy> Spies;
    	private String incountry;
    
    
    public SpyRing(String country)
    {
    	Spies = new ArrayList<Spy>();
    	incountry=country;
    }
    
    public void addSpy(String spynum, String spyname)
    {
    	Spy x=new Spy(spynum, spyname);
    	Spies.add(x);
    }
    
    public static ArrayList<Spy> getSpyList()
    {
    	return Spies;
    }
    
    
    
    
    }
    Here's the driver class:

    Java Code:
    import java.util.*;
    import cs1.Keyboard;
    public class SpyRingMaker
    {
    public static void main (String[]args)
    {
    
    boolean contin=true;
    boolean continuity=true;
    int count=0;
    
    while(contin)
    {
    	System.out.println("1. Enter a spy for your SpyRing");
    	System.out.println("2. Print SpyRing");
    	System.out.println("3. Quit");
    	int choice=Keyboard.readInt();
    	String sNum, sName;
    	SpyRing Goldfinger= new SpyRing("Australia");
    	switch(choice)xz
    	{
    		case 1:
    		System.out.println("Enter the name of the Spy");
    			sName=Keyboard.readString();
    		System.out.println("Enter the Number of the Spy");
    			sNum=Keyboard.readString();
    
    		Goldfinger.addSpy(sNum, sName);
    		Goldfinger.getSpyList().get(count);
    		while(continuity)
    		{
    			System.out.println("1. Enter an alias for this spy");
    			System.out.println("2. Done with this Spy");
    			int choices=Keyboard.readInt();
    
    			switch(choices)
    			{
    				case 1:
    				System.out.println("Enter an alias");
    					String sAlias=Keyboard.readString();
    				Goldfinger.getSpyList().get(count).addAlias(sAlias);
    				break;
    
    				case 2:
    				continuity=false;
    
    				break;
    			}
    			count++;
    		}
    
    		break;
    
    		case 2:
    
    		for (Spy each: Goldfinger.getSpyList())
    		{
         		System.out.println(each);
       		}
    
    		break;
    
    		case 3:
    		System.out.println("Goodbye");
    		contin=false;
    
    		break;
    
    		default:
    		break;
    	}
    }
    
    }
    
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with ArrayList

    When I attempt to print the SpyRing, it compiles w/ no errors, but prints nothing
    Where is the code that is trying to print the SpyRing? Are there any elements in the ArrayList to be printed? If not where does the code add any elements to the arraylist? If the code adds some elements to an ArrayList, is it the same ArrayList as the one being used for printing?

    Try debugging the code by adding some println statements to show what is added, how many elements are in the arraylist after each add.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with ArrayList

    Norm, I try to print the SpyRing in lines 54-61 of the Driver.

    In answer to your second question, the SpyRing is an ArrayList of Spies, which contains an ArrayList of aliases. So yes, there are elements to be printed.

    As for the last question, yes--I believe that the program is printing the ArrayList that I'm putting elements into

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with ArrayList

    Did you try debugging the code by adding calls to the println method as I suggested?
    Add a println in the constructors so you can see when they are called.

    How many items are in the array list when you print it at different places in the code?
    Last edited by Norm; 01-17-2013 at 02:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Help with ArrayList

    When I attempt to print the SpyRing, it compiles w/ no errors, but prints nothing.
    When I compile, I get some warnings. I suggest that you compile with warnings turned on and try to understand the warnings. Fixing your code to make all warnings go away often fixes bugs that make it past the compiler.

  6. #6
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with ArrayList

    Quote Originally Posted by Norm View Post
    Did you try debugging the code by adding calls to the println method as I suggested?
    Add a println in the constructors so you can see when they are called.

    How many items are in the array list when you print it at different places in the code?
    While doing as you suggested, I ran into an issue. When adding a second alias, I get an indexOutOfBoundsException while running the program. Not sure what's causing it

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with ArrayList

    I get an indexOutOfBoundsException
    Where does it happen, what is the value of the index and how long is the item that is being indexed?
    The cause is the index's value is too big for the item being indexed.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help with ArrayList

    It happens when adding a second element to the Aliases ArrayList.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with ArrayList

    Please post the full text of the error message.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. Replies: 1
    Last Post: 05-15-2011, 07:07 PM
  3. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 07:07 AM
  4. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •