Results 1 to 12 of 12
  1. #1
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default assignment problem with List<T>

    Hi, I'm new to java i'm having a problem assigning a string from a string list.
    the code, gets a random number and chooses a sex, m/f if female tries to assign a name based on a list of girlnames otherwise a list of boynames.

    error: method girlNames(int) is undefined...

    I haven't used List<T> before so I'm not quite sure how to assign it to a string. Also once I have assigned it I want to delete the element from the list so that there is no Bunnies with duplicate names. So should I then use clear(); and this will remove the element?

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class BunnyCreation
    {
    	private static final String BOY_NAMES = "names/boynames.txt";
    	private static final String GIRL_NAMES = "names/girlnames.txt";
    	private List<String> boyNames = new ArrayList<String>(1000);
    	private List<String> girlNames = new ArrayList<String>(1000);
    	
    	public void generateBunny()
    	{
    		boolean female;
    		int i = (int)getRandom(20);
    		if (i>10)
    			female=true;
    		else
    			female=false;
    		
    		if (female)
    		{
    			int girlIndex = (int)getRandom(1000);
    			String name = girlNames(girlIndex);  // ERROR  HERE : method girlNames(int) is undefined
    		}
    	}
    	
    	/**
    	 * Adds all names from both boys/girls files to appropiate Lists.
    	 */
    	public void initializeLists()
    	{
    		try
    		{
    			String inStr;
    			Scanner inFile = new Scanner(new FileInputStream(BOY_NAMES));
    			while (inFile.hasNextLine())
    			{
    				inStr = inFile.nextLine();
    				boyNames.add(inStr);
    			}
    			inFile.close();
    			
    			inFile = new Scanner(new FileInputStream(GIRL_NAMES));
    			while (inFile.hasNextLine())
    			{
    				inStr = inFile.nextLine();
    				girlNames.add(inStr);
    			}
    			inFile.close();
    			
    		}
    		catch(FileNotFoundException fnfe)
    		{
    			fnfe.printStackTrace();
    			System.out.println("File not found: " + BOY_NAMES);
    			System.out.println("File not found: " + GIRL_NAMES);
    		}
    	}
    	
    	/**
    	 * Prints out Girl names / Boy names.
    	 */
    	public void printList()
    	{
    		for (String gname : girlNames)
    			System.out.println((gname));
    		for (String bname : boyNames)
    			System.out.println((bname));
    		/*
    		for (Iterator it = girlNames.iterator(); it.hasNext();) 
    		   System.out.println(((String)it.next()));
    		for (Iterator it = boyNames.iterator(); it.hasNext();) 
    		{
    		   String name = (String)it.next();
    		   System.out.println(name);
    		}
    		//*/
    	}
    	
    	public double getRandom(int numOutOf)
    	{
    		double temp = Math.random();
    		return (temp *= numOutOf);
    	}
    }

  2. #2
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    woops nevermind, learning so much I forgot the basics.... I guess I had to make the post to see it clearly :P

    String.equals(string);


    However I'm still not positive on the List.clear(); will the remove the element so there is only 999 elements, or will it just make the element I clear() a null ?

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    It clears the entire list. By the way, why don't you just take a look at the API, it will tell you what every method does.

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    if you want the list to contain strings...List<String> instead of List<T>

  5. #5
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Quote Originally Posted by collin389 View Post
    It clears the entire list. By the way, why don't you just take a look at the API, it will tell you what every method does.
    woops, yea i was thinking of remove(int);

    My brain does not seem to want to work today.

    This is not working for me... the API says it returns the element in the list, does that mean it returns the index or object?

    I'm trying to do this: String name.equals(girlNames.remove(girlIndex));

    because String name.equals(girlNames(girlIndex)); was giving me the same error as before. Method girlNames(int) undefined.
    Last edited by Newbie666; 01-21-2010 at 06:10 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by [RaIdEn] View Post
    if you want the list to contain strings...List<String> instead of List<T>
    Nothing to do with this. List<T> is how generic lists are defined. The OP's problem (which he has already discovered) is in not using the get method.

  7. #7
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    ahh, thanks fubar

  8. #8
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    or not....

    Java Code:
    public void generateBunny()
    	{
    		boolean female;
    		String name="";
    		int i = (int)getRandom(20);
    		if (i>10)
    			female=true;
    		else
    			female=false;
    		
    		if (female)
    		{
    			int girlIndex = (int)getRandom(1000);
    			name.equals(girlNames.get(girlIndex));
    			
    			System.out.println("Female bunny: " + female + " Name: " + name);
    		}
    	}
    OUTPUT:
    Java Code:
    Female bunny: true Name:

  9. #9
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    I haven't used List<T> before so I'm not quite sure how to assign it to a string.
    did i interpret it correct, if not i guess you can ignore it.

  10. #10
    gcampton Guest

    Default

    I thought <T> simply meant generic Type.

    edit: what I thought is irrelevant however, the OP IS using String, you can see lines 10-11, List<String>
    Last edited by gcampton; 01-21-2010 at 09:39 AM.

  11. #11
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    Sorry, I didn't know what List<T> or List<E> meant.

    the assignment is still not happening, I'm getting nothing output for variable name.

    But my method printList() is printing the Lists so there's definately 1000 boyNames and 1000 girlNames. I just don't seem to have the assignment down in that last block of code I posted.

    name.equals(girlNames.get(girlIndex));

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    equals() is for comparisons eg:
    Java Code:
    if (name.equals("Bob")) {
        // Do something.
    }
    You, presumably, want to assign the name at girlIndex to "name", using "=".

Similar Threads

  1. The Assignment Problem
    By bumblyb33 in forum New To Java
    Replies: 5
    Last Post: 03-04-2009, 04:21 AM
  2. Problem with an assignment: Backgammon game
    By Poddy in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:32 AM
  3. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  4. assignment problem help needed
    By tiggz1980 in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 11:14 PM
  5. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 11:33 AM

Posting Permissions

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