Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post

    Why will this block not complie?

    Code:
        public void memberRegister(String newName, String newId, int newPinNumber)
        {
            String name = newName;
            String id = newId;
            int pinNumber = newPinNumber;
            
            Member.welcomeMessage(String name, String id, String storeName);
        }

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,930
    Thanks
    46
    Thanked 203 Times in 202 Posts
    String 'storeName' is not declared; presumably Member is outside this block... need more code and error message to give an accurate answer though

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    Ok sorry didnt provide enough info.

    storeName is a field in the class, so thats valid.

    Error on this line:
    Member.welcomeMessage(String name, String id, String storeName);

    Says this:
    ')' Expected

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,930
    Thanks
    46
    Thanked 203 Times in 202 Posts
    I'm not seeing it... can you post entire class?

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can't call a method like that. It expects those argument datatypes, you cannot respecify them in the method call itself. Give it name, id and storename, not String name, ... instead.

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,930
    Thanks
    46
    Thanked 203 Times in 202 Posts
    I can't believe I didn't catch that :| it looked so correct for a constructor the fact the method call was bad eluded me *smack

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    Can you fix it for me.
    Here is the store class:

    Code:
    public class Store
    {
        // instance variables
        private String storeName;
        private int total;
    
        /**
         * Constructor for objects of class Store
         */
        public Store(String newStoreName)
        {
            storeName = newStoreName;
            total = 0;
        }
        
        /**
         * Register a member
         */
        public void memberRegister(String newName, String newId, int newPinNumber)
        {
            String name = newName;
            String id = newId;
            int pinNumber = newPinNumber;
            
            Member.welcomeMessage(String name, String id, String storeName);
        }
    }
    And here is the welcomeMessage method which is in the member class:
    Code:
        /**
         * Welcome Message
         */
        public void welcomeMessage(String newName, String newId, String newStoreName)
        {
            System.out.println(newStoreName + ": Welcome " + newName + " (id: " + newId + ")");
        }

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeah, use this: Member.welcomeMessage(name, id, storeName);.

  • #9
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    Yeah, use this: Member.welcomeMessage(name, id, storeName);.
    Using that line i now get this error:

    non-static method welcomMessage(java.lang.String,java.lang.String,java.lang.String) cannot be referenced from a static context

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You need to provide it with an object then. WelcomeMessage is a non-static method, so it must be called on an instance of an object.

  • #11
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    How do I do that?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The only direct way to do it with the code you have here is to instantiate a new member within the method memberRegister. I would assume that memberRegister should be doing something with Member anyway, so that may be a fine place to construct a new member.

  • #13
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Instantiating a Member object would depend on the constructors available.

    Here's an example of instantiating a Member if it has a default constructor (no arguments)

    Code:
    public void memberRegister(String newName, String newId, int newPinNumber)
        {
            
            String name = newName;
            String id = newId;
            int pinNumber = newPinNumber;
            Member member = new Member();
            member.welcomeMessage(name, id, storeName);
        }
    Here's the Java trail on constructors for your reference:
    http://docs.oracle.com/javase/tutori...structors.html

  • #14
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thanks for all your help. I have come across another problem if you dont mind helping me out.
    Heres the link:
    http://www.codingforums.com/showthread.php?t=282258


  •  

    Posting Permissions

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