Results 1 to 12 of 12
Thread: assignment problem with List<T>
- 01-21-2010, 03:36 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
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); } }
- 01-21-2010, 03:41 AM #2
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
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 ?
- 01-21-2010, 05:10 AM #3
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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.
- 01-21-2010, 05:33 AM #4
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
if you want the list to contain strings...List<String> instead of List<T>
- 01-21-2010, 06:00 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
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.
-
- 01-21-2010, 06:11 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
ahh, thanks fubar
- 01-21-2010, 06:16 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
or not....
OUTPUT: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); } }
Java Code:Female bunny: true Name:
- 01-21-2010, 06:37 AM #9
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
did i interpret it correct, if not i guess you can ignore it.I haven't used List<T> before so I'm not quite sure how to assign it to a string.
- 01-21-2010, 09:33 AM #10gcampton Guest
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.
- 01-21-2010, 11:15 AM #11
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
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));
- 01-21-2010, 12:12 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
The Assignment Problem
By bumblyb33 in forum New To JavaReplies: 5Last Post: 03-04-2009, 04:21 AM -
Problem with an assignment: Backgammon game
By Poddy in forum New To JavaReplies: 6Last Post: 02-05-2009, 05:32 AM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
assignment problem help needed
By tiggz1980 in forum New To JavaReplies: 2Last Post: 02-06-2008, 11:14 PM -
Cannot solve the coding problem of my assignment
By elimmom in forum New To JavaReplies: 3Last Post: 08-13-2007, 11:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks