Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can someone help me to bring just a variable from one method to another?

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone help me to bring just a variable from one method to another?

    I need to get the "math" variable into another method. any help would be appreciated.

    public static void Amount()
    {
    Scanner input = new Scanner(System.in);

    double initialAmount;
    double paycheckAmount;

    // user inputs the initial amount that is in their bank account

    System.out.println("Please enter your initial amount in you bank account.");
    initialAmount = input.nextDouble();
    input.nextLine();

    // dont let user input a negative value

    while(initialAmount < 0)
    {
    System.out.println("That is an invalid Value, please enter a valid value.");
    System.out.println("Please enter your initial amount in you bank account.");
    initialAmount = input.nextDouble();
    input.nextLine();
    }
    // user inputs the amount of their paycheck, that amount gets added to the initial amount

    System.out.println("Please enter your paycheck amount.");
    paycheckAmount = input.nextDouble();
    input.nextLine();

    // dont let the user input a negative value

    while(paycheckAmount < 0)
    {
    System.out.println("That is an invalid Value, please enter a valid value.");
    System.out.println("Please enter your paycheck amount.");
    paycheckAmount = input.nextDouble();
    input.nextLine();
    }

    double math = initialAmount + paycheckAmount;
    System.out.println("You have " + math + " dollars in your bank account");


    } // end of amount method

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Can someone help me to bring just a variable from one method to another?

    You can either return the variable from the Amount() function, or make it a class-level variable:

    public static double Amount() {
     ...
     double math = initialAmount + paycheckAmount;
     return math;
    }

    Or:
    private static double math;
    public static void Amount() {
     ...
     math = initialAmount + paycheckAmount;
    }

Similar Threads

  1. Changing Variable Within Method
    By Deployment in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2017, 10:23 PM
  2. How do I call a variable from another method?
    By brandon236 in forum Java Theory & Questions
    Replies: 8
    Last Post: October 3rd, 2017, 02:04 AM
  3. Can't declare Variable in new method
    By JohnEliot in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2013, 06:14 AM
  4. Help with returning variable from method
    By m7abraham in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 17th, 2012, 03:01 PM
  5. how to pass a variable from a method to another
    By amr in forum Java Theory & Questions
    Replies: 1
    Last Post: November 20th, 2010, 10:32 PM