简体   繁体   中英

How do i call on a local variable from another method?

I want to shorten my method, but when doing so some of my variables don't work. Should I do something with the parameters? My user at the bottom auction.makeBid(user, value); is the one that doesn't work.

This is my new method to shorten out the other at the bottom

    private User checkUser() {
    System.out.println("Enter the name of the user>");
    String nameUser = inputToString();

    User user = null;
    for (User userName : users) {
        if (userName.getName().equalsIgnoreCase(nameUser)) {
            user = userName;
            return user;
        }
    }
        System.out.println("Error: no such user");
        return null;

This is where I want to implement the code. At the bottom im supposed to have the variable user.

    private void makeBid() {
    User u = checkUser();

    System.out.println("Enter the name of the dog");
    String nameDog = inputToString();
    Dog dogsname = null;
    for (Dog dogsName : registerDog) {
        if (dogsName.getName().equalsIgnoreCase(nameDog)) {
            dogsname = dogsName;
            break;
        }
    }
    if (dogsname == null) {
        System.out.println("Error: this dog is not up for auction");
        return;
    }

    Auction auction = null;
    for (Auction auctionsMany : auctions) {
        if (auctionsMany.getDog().equals(dogsname)) {
            auction = auctionsMany;
            break;
        }
    }

    if (auction == null) {
        System.out.println("Error no auction");
        return;
    }

    Bid b = auction.getHighestBid();
    int min;
    if (b == null) {
        min = 1;
    } else {

        min = b.getAmountBid() + 1;
    }

    int value = 0;
    do {
        System.out.println("Enter a bid");
        value = input.nextInt();
        input.nextLine();
        if (value < min) {
            System.out.println("Error to low bid");
        }
    } while (value < min);

    auction.makeBid(user, value);
    System.out.println("Bid made");

}

In Java there are things called "scopes" between the parenthesis. A variable that is created in one scope cannot be accessed by another scope. An example is the variable that you are using here. What you can do is you can either call the method from another method and get a return value for "user", or you can pass the variable in one scope as a parameter for another method. I will demonstrate with ar mini example below:

public static void scopeOne() {
    String myName = "name";
}

public static void scopeTwo() {
    System.out.println(myName);
}

This will obviously not work because you cannot access myName from scopeOne in scopeTwo this way. You will get a compile-time error. You could solve this in various ways, but here is one example:

public static void scopeOne() {
    String myName = "name";
    scopeTwo(myName);
}

public static void scopeTwo(String myName) {
    System.out.println(myName);
}

You could pass the variable to the method as an argument and make the variable a parameter. That way it can be within the local scope of the method you are attempting to call. Another thing you can do is make the User object variable a class or instance variable and then update it within your methods.

To cannot use local variable declared in one local method and use it in another. If you want to do something to reduce your code try to declare variable at class scope not at method scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM