Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-14-2012, 04:10 PM   PM User | #1
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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);
    }
abell12 is offline   Reply With Quote
Old 11-14-2012, 04:37 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Old 11-14-2012, 04:40 PM   PM User | #3
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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
abell12 is offline   Reply With Quote
Old 11-14-2012, 05:51 PM   PM User | #4
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Old 11-14-2012, 06:24 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,644
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-14-2012, 06:36 PM   PM User | #6
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Old 11-14-2012, 06:46 PM   PM User | #7
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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 + ")");
    }
abell12 is offline   Reply With Quote
Old 11-14-2012, 07:03 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,644
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, use this: Member.welcomeMessage(name, id, storeName);.
Fou-Lu is offline   Reply With Quote
Old 11-14-2012, 07:28 PM   PM User | #9
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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
abell12 is offline   Reply With Quote
Old 11-14-2012, 07:51 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,644
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-14-2012, 08:27 PM   PM User | #11
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
How do I do that?
abell12 is offline   Reply With Quote
Old 11-14-2012, 10:07 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,644
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-15-2012, 04:10 AM   PM User | #13
vision_slr
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
vision_slr is an unknown quantity at this point
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
vision_slr is offline   Reply With Quote
Old 11-15-2012, 03:24 PM   PM User | #14
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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
abell12 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:47 PM.


Advertisement
Log in to turn off these ads.