Results 1 to 8 of 8
Thread: Java: simple ArrayList question
- 01-21-2013, 01:57 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 7
- Rep Power
- 0
Java: simple ArrayList question
Just started java few weeks ago. I am learning about ArrayList. soo i encounter this problem
keyword class is a tool for me to use so never mind about that. The problem is with line 17, i cannot seem to add the player into the arraylist. Any idea? Sorry, i am very new to Java. I think it should be an easy one. I did some research but i still do not understand why.Java Code:public class test { public ArrayList<Player> players = new ArrayList<Player>(); public Deck deck = new Deck(); public int numPlayer =0; private void registerPlayer(){ while(numPlayer < 2|| numPlayer > 5){ numPlayer = keyword.readInt("Enter numner of players(2-5)> "); } for(int i=0;i<numPlayer;i++){ String name = keyword.readString("Enter Name of Player"+(char)('A'+i)+">"); while(name.length()<3||name.length()>8){ System.out.println("****NAME TOO SHORT/LONG*****"); name = keyword.readString("Enter Name of Player"+(char)('A'+i)+">"); } players.add(new Player('A'+i)+ name); }
- 01-21-2013, 02:02 PM #2
Re: Java: simple ArrayList question
Hi,
I bet you got an Exception. Always post the exact error message and stack trace when asking questions.
You try to add new Player('A'+i)+ name into the ArrayList. I bet you wanted to append the name into the constructor parameter of Player. So you probably want: new Player('A'+ i + name).Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-21-2013, 02:22 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 7
- Rep Power
- 0
Re: Java: simple ArrayList question
Hi,
yes i want to append the user input into the Arraylist which is String name. I replace my codes with yours but it returns me (The constructor Player(String) is undefined). So, basically, it should function like this( i did it in a normal array but now i need it in arraylist)
Java Code:players = new Player[numPlayer]; for(int i=0;i<numPlayer;i++){ String name = Helper.readString("Enter Name of Player"+(char)('A'+i)+">"); while(name.length()<3||name.length()>8){ System.out.println("NAME TOO SHORT/LONG"); name = Helper.readString("Enter Name of Player"+(char)('A'+i)+">"); } players[i] = new Player((char)('A'+i),name);
The code should act like that, but using an arraylist. Thanks for the help!
- 01-21-2013, 02:25 PM #4
Re: Java: simple ArrayList question
Ok, so where is the error message and stack trace when you try using ArrayList?
I know what's wrong, but I want you to learn how to read error messages.Last edited by PhHein; 01-21-2013 at 02:29 PM.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-23-2013, 12:16 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 7
- Rep Power
- 0
Re: Java: simple ArrayList question
hm.. the error message appears on line 17. The error that i got when i run it is (java.lang.NoSuchMethodError: main). i think i really should start learning how to read error messages, been depending on quick fix most of the time.
- 01-23-2013, 01:00 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,451
- Rep Power
- 16
Re: Java: simple ArrayList question
That says you have no main() method when trying to launch your application.
Please do not ask for code as refusal often offends.
- 01-23-2013, 01:08 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 7
- Rep Power
- 0
Re: Java: simple ArrayList question
oh, sorry i accidently deleted the main() method. haha. the error when i get when i tried to run the code is:
The method add(Player) in the type ArrayList<Player> is not applicable for the arguments (String)
The constructor Player(int) is undefined
i was wondering if i need to change the constructor in my player.class because it is undefined.
- 01-23-2013, 01:47 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,451
- Rep Power
- 16
Re: Java: simple ArrayList question
Those are compilation errors.
You can't add a String to an ArrayList you have defined as holding something else (Players in your case).
And yes, you need to write a Player(int) constructor if you plan on using one.Please do not ask for code as refusal often offends.
Similar Threads
-
Simple Java question
By devinmont in forum New To JavaReplies: 1Last Post: 09-11-2012, 10:15 PM -
Simple java applet zoom question
By Lemmy Winks in forum New To JavaReplies: 4Last Post: 07-02-2012, 03:17 AM -
Simple question on Embedding Java applets
By gasper91 in forum New To JavaReplies: 6Last Post: 06-14-2011, 03:24 AM -
Question about a simple Java programing assignment.
By ron_j_m in forum New To JavaReplies: 12Last Post: 04-27-2011, 12:03 AM -
Java Native Access (JNA) really simple begginer question
By carek in forum Advanced JavaReplies: 2Last Post: 03-18-2010, 12:39 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks