-2

how can I send an int value made by a scanner from one of my methods to another method. Also, how can I also send it to my main and then send it to another method

betso
  • 9
  • 2
    [The Java Tutorials - Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) - [The Java Tutorials - Returning a Value from a Method](https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html) – OH GOD SPIDERS Apr 27 '22 at 21:19
  • 1
    Parameters and return values. See other questions like https://stackoverflow.com/questions/31625043/how-to-use-parameters-and-arguments-in-java – Progman Apr 27 '22 at 21:20

2 Answers2

0

To pass a value, you use parameters:

public static void main()
{
    Scanner scan = new Scanner(System.in);// creates keyboard scanner
    int thisInt = scan.nextInt(); // user types 5
    example(thisInt);             // it is passed 5
}

If the method "example" was passed the number with thisInt

public void example(int value)
{
    System.out.print(value);         // prints that 5
}

It would print the value, thisInt, or 5.

EDIT FOR MORE INFO

The void on that method is the reason nothing is returned. With this VERY VERY BASIC code:

public int example(Scanner scan)
{
    return scan.nextInt()
}

You can return a read number to main like so:

int newNumber = example(new Scanner(System.in));
Kemper Lee
  • 276
  • 4
  • 10
-2

you can declare it as static parameter value and then you can get it from anywhere
example:
static int value=null; to get
ClassWhereValueIsDeclared.value

airwin67
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '22 at 04:31