Results 1 to 8 of 8

Thread: method question

  1. #1
    xplayerr is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default method question

    Hi,

    I'll try to include as much info without making this too long.

    I'm trying to write a method which uses another method in the same class.

    String userWithName(String s){
    String a = null;
    for(User b: users){
    if(s==b.userName)
    a=b.userName;
    }
    return a;

    is my first method, in the second one, this is the question : A registerUser(User x) method that adds a given User to the music center’s list of users,
    provided that there are no other users with the same username. If there are other users with the
    same username, then this method does nothing. Use the userWithName() method above.



    I tried different code, but I just can't get one to work.

    void registerUser(User x) {
    if (userWithName(String x) = null)
    {
    users.add(User);

    thats what I had at one point, but It doesnt look right, and btw, all the arraylists are created and compile perfectly.

    Could anyone help ?

    thanks a lot

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

    Default

    Java Code:
    if(s==b.userName)
    How does one compare Strings? Ha? :rolleyes:


    Java Code:
    if(s.equals(b.userName))
    And
    Java Code:
    if (userWithName(String x) = null)
    is missing one = if you would like to compare.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    xplayerr is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    thanks for you reply.

    if (userWithName(String x) == null)

    I added the = but java still tells me it's an invalid statement and illegal start of expression AND ) + ; expected :mad: all for the same line lol

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

    Default

    Post your code, without seeing it there is no way helping you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    xplayerr is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    import java.util.ArrayList;

    class MusicExchangeCenter{
    ArrayList<User> users;


    public MusicExchangeCenter() {
    users = new ArrayList<User>();
    }

    public String toString() {
    int numSongs = 0;
    for (User a: users) {
    numSongs += a.songList.size();
    }

    return "Music Exchange Center" + "(" + users.size() +"clients online" +" , " + numSongs + " songs available)";
    }

    String userWithName(String s){
    String a = null;
    for(User b: users){
    if(s.equals(b.userName))
    a=b.userName;
    }
    return a;
    }
    void registerUser(User x) {
    if (userWithName(String x) == null)
    add.users(User);
    }
    }
    }

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

    Default

    One closing brace at the end too much and
    Java Code:
    void registerUser(User x) {
      if (userWithName(x.userName) == null) // <-- look here
        add.users(User);
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    xplayerr is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    thanks man, that'll be it for now :D

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

Similar Threads

  1. Learning - 2D array and method question.
    By Russo in forum New To Java
    Replies: 24
    Last Post: 09-15-2009, 09:16 AM
  2. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 12:13 AM
  3. Question about CAS (compare and swap) method
    By illidian in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2008, 09:49 PM
  4. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 03:39 AM
  5. Return question in a method.
    By MetalGear in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 05:45 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
  •