MaxK
MaxK

Reputation: 437

Passing parameter to method gotten from another method?

I am new to Java but I know this stuff I'm just having a seerious brain fart. Could you help me. I've been up for 24+ hours writing code and I'm all confused now. Here is the code in question:

    public long getTheLong() {
    Scanner scano = new Scanner(System.in);
    System.out.println ("enter account number");
    acctNum = scano.nextLong();
    return acctNum;
}

public void searchById(long acctNum) {
// do something with acctNum
{

the first method returns acctNum that gotten by user input. I want to take whatever acctNum is now after getTheLong() method was invoked and pass that to the the following method searchById(long acctNum). How can I do something like this? Thank you so much, I know it's stupid but I'm confused at this point.

Upvotes: 0

Views: 66

Answers (5)

Samantha Connelly
Samantha Connelly

Reputation: 71

try this in your java file

import java.util.Scanner;

public class TestProgram 
{

    public static void main(String[] args) 
    {
        long acctNum = getTheLong();
        searchById(acctNum);
    }

    public static long getTheLong() 
    {
        long acctNum = 0;
        Scanner scano = new Scanner(System.in);
        System.out.println ("enter account number");
        acctNum = scano.nextLong();
        return acctNum;
    }

    public static void searchById(long acctNum) 
    {
        // do something with acctNum
    }
}

Upvotes: 2

NPKR
NPKR

Reputation: 5496

try this

public void searchById() {
    long acctNum = getTheLong();
    // do something with acctNum
    }

Upvotes: 1

Ayaz Ali Khatri
Ayaz Ali Khatri

Reputation: 483

hi place it before return statement
searchById(scano.nextLong()); Thanks

Upvotes: 0

Karthik T
Karthik T

Reputation: 31952

You can pass the return value of the one function as an arguement to the second as below.

searchById(getTheLong());

Upvotes: 0

Renjith
Renjith

Reputation: 3274

long acctNum = getTheLong();
searchById(acctNum );

Upvotes: 0

Related Questions