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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 5K views
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!
 
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.