Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By PhHein

Thread: Java: simple ArrayList question

  1. #1
    bronzefish is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Red face Java: simple ArrayList question

    Just started java few weeks ago. I am learning about ArrayList. soo i encounter this problem

    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);
    		}
    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.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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

  3. #3
    bronzefish is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default 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!

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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 03:29 PM.
    DarrylBurke likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    bronzefish is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default 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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default 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.

    ** This space for rent **

  7. #7
    bronzefish is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default 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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Simple Java question
    By devinmont in forum New To Java
    Replies: 1
    Last Post: 09-11-2012, 11:15 PM
  2. Simple java applet zoom question
    By Lemmy Winks in forum New To Java
    Replies: 4
    Last Post: 07-02-2012, 04:17 AM
  3. Simple question on Embedding Java applets
    By gasper91 in forum New To Java
    Replies: 6
    Last Post: 06-14-2011, 04:24 AM
  4. Question about a simple Java programing assignment.
    By ron_j_m in forum New To Java
    Replies: 12
    Last Post: 04-27-2011, 01:03 AM
  5. Replies: 2
    Last Post: 03-18-2010, 01:39 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
  •