Can I use local variables from one method in another method?

Click For Summary
Local variables in one method cannot be accessed directly in another method without being passed as parameters. To use these variables in methods like `printReport()` and `verifyUser()`, they should be included as parameters in those methods. The original poster confirmed that the local variables do have assigned values in their actual code, which is essential for passing them correctly. This approach adheres to the instructor's guideline of not adding new fields. Utilizing method parameters is a standard practice for sharing data between methods in Java.
wyosteve
Messages
23
Reaction score
0
Without adding new Fields, how do I use local variables from another method in the same class? I know I could simply add these variables to the list of Fields and get to them that way but our instructor specifically told us NOT to do this. I am stumped. Heres the realavent code, in bold are the local variables I want to use again.

public static void main(String[] args)
{
String userName; // the name of the person using this program
String password; // user's password
String input; // temporary String for holding returned values from input dialog boxes
boolean done = false; // false until the employee is done entering products sold

String itemName; // the name of the item sold
String itemID; // product ID # for the item sold
double price; // price for the item
int quantity; // quantity sold

ArrayList<Item> items = new ArrayList<Item>();// holds all items sold by this employee today

code

/**
* Prints out the sales report in an organized format.
*/
public void printReport()
{
int totalPrice = 0;
for(Item item: items)
{
totalPrice = item.getPrice();
}
}

/**
* Verify a user account and password.
*/
public void verifyUser()
{
for(String name: EMPLOYEE)
{
for(String pswd: EMPLOYEE_PSWD)
{
if((userName.equals(name)) && (password.equals(pswd)))
{
do this }
}
}
}
}
Any clue would be appriciated! Oh and first post!
 
Physics news on Phys.org
Welcome to PF!

When you include code, wrap it with [ code] and [ /code] tags (without extra spaces) to preserve your formatting. I did that below.
wyosteve said:
Without adding new Fields, how do I use local variables from another method in the same class? I know I could simply add these variables to the list of Fields and get to them that way but our instructor specifically told us NOT to do this. I am stumped. Heres the realavent code, in bold are the local variables I want to use again.
Code:
public static void main(String[] args)
    {
        String [B]userName[/B];        // the name of the person using this program
        String [B]password[/B];        // user's password
        String input;           // temporary String for holding returned values from input dialog boxes
        boolean done = false;   // false until the employee is done entering products sold
        
        String itemName;    // the name of the item sold
        String itemID;      // product ID # for the item sold
        double price;       // price for the item
        int quantity;       // quantity sold
        
        ArrayList<Item>[B] items [/B]= new ArrayList<Item>();// holds all items sold by this employee today

        code

/**
* Prints out the sales report in an organized format.
*/
    public void printReport()
      {
          int totalPrice = 0;
          for(Item item: [B]items[/B])
          {
              totalPrice = item.getPrice();
          }
       }
      
/**
* Verify a user account and password.
*/
    public void verifyUser()
    {
        for(String name: EMPLOYEE)
        {
            for(String pswd: EMPLOYEE_PSWD)
            {
                if(([B]userName[/B].equals(name)) && ([B]password[/B].equals(pswd)))
                {
                   do this                }
                }
            }
        }
    }
Any clue would be appriciated! Oh and first post!

Your two functions should have parameters. That's how you can pass the value of a local variable in main to your functions. However, the variables you pass should already have values. In your code, none of the variables are shown as having values assigned to them.
 
Outstanding! Thank you Mark! I omitted the code in which I assigned values to those variables, but in my actuall code they do have assigned values. I will remember to wrap my code in the future. Thanks again.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
5K