Welcome to Dream.In.Code
Become a Java Expert!

Join 150,422 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,098 people online right now. Registration is fast and FREE... Join Now!




methods & arguments

 
Reply to this topicStart new topic

methods & arguments

raeNet
16 Mar, 2008 - 12:45 PM
Post #1

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
When I compile my code, I get an error: cannot find symbol and it points to "retail" in code line: System.out.println("Retail is: " + retail);

My goal is to call on the calcRetail method to display the retail price based on the user's wholesale(ws) and markup price(mup) inputs.

1) Any suggestions on how I can fix this?
and
2) Is this the way I need to approach this?

Many thanks for your help!!!!!!!!!



CODE

import java.io.*;

public class RetailPrice
{
    public static void main(String [] args) throws IOException
    {
        String input;
        double ws;
        int mup;
        double retail;
        retail = ws * mup;
        
        //create objects for keyboard entry.
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader keyboard = new BufferedReader(reader);
        
        System.out.println("This program displays an item's retail price based on wholesale and markup input entered by user");
        
        //get wholesale cost.
        System.out.print("What is wholesale cost? ");
        input = keyboard.readLine();
        ws = Double.parseDouble(input);
        
        
        //get markup %.
        System.out.print("What is markup? ");
        input = keyboard.readLine();
        mup = Integer.parseInt(input);
        
        if (ws > 0 && mup > 0)
            calcRetail();
        else
            invalidEntry();
        
        System.out.println("Tootles.");
        
    
            }
            public static void calcRetail()
            {
            System.out.println("Retail is: " + retail);
            }
            
            public static void invalidEntry()
            {
            System.out.println("your entry is invalid.");
            }
    }

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Methods & Arguments
16 Mar, 2008 - 12:51 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Remember that you defined retail in your main method. In order for calcRetail to see your variable, you have to do one of two things...

1) Pass the retail variable to calcRetail (recommended)
2) Make retail a class level variable (not recommended for this situation).

This is a classic scoping issue. Functions can only see variables passed to it or defined in a more "broader or global" scope.

So it is up to you as to which way you want to do. Since you are not apparently altering the value of the retail, just printing it or going to use it in a calculation, I would definitely just pass the variable to the calcRetail function.

Hope that answers your question. smile.gif

This post has been edited by Martyr2: 16 Mar, 2008 - 12:52 PM
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Methods & Arguments
16 Mar, 2008 - 01:15 PM
Post #3

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
[quote name='Martyr2' date='16 Mar, 2008 - 01:51 PM' post='326832']
Remember that you defined retail in your main method. In order for calcRetail to see your variable, you have to do one of two things...

1) Pass the retail variable to calcRetail (recommended)
2) Make retail a class level variable (not recommended for this situation).

This is a classic scoping issue. Functions can only see variables passed to it or defined in a more "broader or global" scope.

So it is up to you as to which way you want to do. Since you are not apparently altering the value of the retail, just printing it or going to use it in a calculation, I would definitely just pass the variable to the calcRetail function.

Hope that answers your question. smile.gif
[/quote]

[newbie quote]
I am so new to this - How do I pass the variable to calcRetail?
I tried this:
CODE

public static void calcRetail(retail)

But, now I get an error message: an identifier expected? This sounds way to easy, but I tend to make them difficult...
[/quote]
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Methods & Arguments
16 Mar, 2008 - 01:23 PM
Post #4

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions

CODE
The declaration inside a class for the method would be :

public static void calcRetail(double retail){

     //code for this method comes here

}
//The method call inside your main would be

calcRetail(retail); //if something is returned, you can store it inside a variable of data-type same as the return type.
//if the return type is void, i.e. you are not returning anything. You can simply call it like the above statement.


Hope that helps :)
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Methods & Arguments
16 Mar, 2008 - 01:35 PM
Post #5

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
QUOTE(letthecolorsrumble @ 16 Mar, 2008 - 02:23 PM) *

CODE
The declaration inside a class for the method would be :

public static void calcRetail(double retail){

     //code for this method comes here

}
//The method call inside your main would be

calcRetail(retail); //if something is returned, you can store it inside a variable of data-type same as the return type.
//if the return type is void, i.e. you are not returning anything. You can simply call it like the above statement.


Hope that helps smile.gif



[newbie quote]
I thought I did what you advised, but with the following main/method coding, I get an error: Illegal start of expression. What am I doing wrong?????????

CODE

System.out.println("Hi.");
if (ws > 0 && mup > 0)
calcRetail(retail);
else
invalidEntry();

public static void calcRetail(double retail)
{
System.out.println("Retail is: " + retail);
}

public static void invalidEntry()
{
System.out.println("your entry is invalid.");
}
}

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Methods & Arguments
16 Mar, 2008 - 02:02 PM
Post #6

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
You are defining a method inside the main method, as I see it. You need to define the method inside a class, but outside of other methods, only the method call is allowed inside another method.
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Methods & Arguments
16 Mar, 2008 - 02:12 PM
Post #7

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
QUOTE(letthecolorsrumble @ 16 Mar, 2008 - 03:02 PM) *

You are defining a method inside the main method, as I see it. You need to define the method inside a class, but outside of other methods, only the method call is allowed inside another method.



[newbie quote]
I modified my code (as follows) and now I'm getting the following errors:
1) variable ws might nothave been initialized
2) variable ws might nothave been initialized

Can you help me?
[/newbie quote]

CODE

import java.io.*;

public class RetailPrice
{
public static void main(String [] args) throws IOException
{
String input;
double ws;
int mup;
double retail;
retail = ws * mup;

//create objects for keyboard entry.
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(reader);

System.out.println("This program displays an item's retail price based on wholesale and markup input entered by user");

//get wholesale cost.
System.out.print("What is wholesale cost? ");
input = keyboard.readLine();
ws = Double.parseDouble(input);


//get markup %.
System.out.print("What is markup? ");
input = keyboard.readLine();
mup = Integer.parseInt(input);
retail = ws * mup;

System.out.println("For wholesale and markup% entered,");
calcRetail(retail);

}
public static void calcRetail(double retail)
{
System.out.println("Retail is: " + retail);
}
}

User is offlineProfile CardPM
+Quote Post

raeNet
RE: Methods & Arguments
16 Mar, 2008 - 02:33 PM
Post #8

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
QUOTE(raeNet @ 16 Mar, 2008 - 03:12 PM) *

QUOTE(letthecolorsrumble @ 16 Mar, 2008 - 03:02 PM) *

You are defining a method inside the main method, as I see it. You need to define the method inside a class, but outside of other methods, only the method call is allowed inside another method.



[newbie quote]
I modified my code (as follows) and now I'm getting the following errors:
1) variable ws might nothave been initialized
2) variable ws might nothave been initialized

Can you help me?
[/newbie quote]

CODE

import java.io.*;

public class RetailPrice
{
public static void main(String [] args) throws IOException
{
String input;
double ws;
int mup;
double retail;
retail = ws * mup;

//create objects for keyboard entry.
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(reader);

System.out.println("This program displays an item's retail price based on wholesale and markup input entered by user");

//get wholesale cost.
System.out.print("What is wholesale cost? ");
input = keyboard.readLine();
ws = Double.parseDouble(input);


//get markup %.
System.out.print("What is markup? ");
input = keyboard.readLine();
mup = Integer.parseInt(input);
retail = ws * mup;

System.out.println("For wholesale and markup% entered,");
calcRetail(retail);

}
public static void calcRetail(double retail)
{
System.out.println("Retail is: " + retail);
}
}



[newbie quote]
I initialized my variables, but my calculation is not working.

Any suggestions?
[/newbie quote]
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Methods & Arguments
16 Mar, 2008 - 02:55 PM
Post #9

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
String input;
double ws;
int mup;
double retail;
retail = ws * mup; //this line needs to go.


Also next time, please the task you want to accomplish.

This post has been edited by letthecolorsrumble: 16 Mar, 2008 - 02:56 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month