Home > Enterprise >  i'm trying to figure out how to access a returned value/variable from one method while writing
i'm trying to figure out how to access a returned value/variable from one method while writing

Time:02-03

i understand what they do and what their purpose is and i've used them successfully many times. but there seems to be some things i don't yet understand that keep getting in the way. for instance, im assigned to create a program to prompt the user to input their birthday then calculate the number of days until their birthday. The main should not have any print statements, just methods. here is a method i wrote:

public static int inputUser () {
    Scanner console = new Scanner(System.in);
    
    System.out.println("Please enter your birthday: ");
    System.out.print("What is the month (1-12)? ");
    int month1 = console.nextInt();
    if (month1 == 4 || month1 == 6 || month1 == 9 || month1 == 11) {
        System.out.print("What is the day   (1-30)? ");
    } else if (month1 == 1 || month1 == 3 || month1 == 5 || month1 == 7 || month1 == 8 || month1 == 10 || month1 == 12) {
        System.out.print("What is the day   (1-31)? ");
    } else if (month1 == 2) {
        System.out.print("What is the day   (1-28)? ");
    }
    int day1 = console.nextInt();
    int absBday = daysInMonth(month1, day1);
    System.out.println(month1   "/"   day1   " is day #"   absBday   " of 365.");
    return absBday;

i want to use the returned value in another method i'm writing but i can't figure out how to access this returned value for the new method. the other method will basically calculate how many days from today until the user's inputted birthday, which i have another method already successfully written for determining how many days into the year any given day is, i just need this returned "day #" to do one last tiny little method. anybody got any pointers?

i basically just tried things my intuition already knew wouldnt work, like trying to call absDays in the new method i'm writing, trying to define the userInput method with a declared type and variable in the parameters, with AND without matching it to the absDay variable name...just basically a bunch of things i already pretty much knew were not going to work, but sometimes when i'm experimenting i'll have an AHA! moment even if i'm trying something that obviously won't work, but the AHA! moment never came :(

CodePudding user response:

I am not sure if I understand your question fully. But I think the following might work.

public static int inputUser(){
 // Your code in question
}

public static int theOtherMethod(int n){
 // already working method
}

You just have to call

int nDays = theOtherMethod(inputUser());

CodePudding user response:

i just wanted to post what i ended up doing, for anyone browsing for help.

all i needed was to declare a variable in main and initialize it to the method i illustrated above that holds my desired return value, which would then hold that returned value as well, allowing me to pass that new declared variable as an argument to another method, which i actually needed to do this twice, two new variables in main/2 return values from 2 different methods, to achieve my desired results.

i actually solved my own issue before checking my answers here. i dont know if yall have heard of the rubber duck method? if youre stuck, ask a detailed version of your question out loud to a rubber duck, or anything really, or a friend whether they know anything about what youre asking or not, because the process of illustrating the question thoroughly activates additional useful resources in your analysis. basically, after i emailed my professor then posted this question here, i returned back to my program and suddenly had a much more clear and almost zen like perspective, and started connecting dots more efficiently which led me to discovering the answer to my own question. thanks for your time guys! this website has helped tremendously

  • Related