Luqman Rumaiz
Luqman Rumaiz

Reputation: 33

How do I return an Input from one method to the other without using a global variable and without the input being asked again in java

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter A to Add Customer\nEnter V to View Customers");
    System.out.print("Enter Your Option : ");
    String option = input.next();
    getInput(option);
    Application.launch(args);
}
public void start(Stage stage) {
    stage.setTitle("Train Booking System");
    Scene window = makeButtons();
    stage.setScene(window);
    stage.show();
}
public static String getInput(String Input){
    return Input;
}
public static Scene makeButtons() {
    GridPane layout = new GridPane();
    Scene window = new Scene(layout, 1366, 768);
    for (int column = 1; column <= 10; column++) {
        Button btn = new Button();
        layout.add(btn, column, column);
        btn.setId("btn");
        Button finalBtn = btn;
        if (getInput().equals("A"))
            btn.setOnAction(e -> {
                finalBtn.setStyle("-fx-background-color: #FF3EA5FF");
            });
        }
    return window;
}

I am making a software like to add a booking and I should store it (which I have to do still) , also I have the option to view bookings so in this case I dont want to be able to click the button.

How do I set the buttons' action in the if condition without getting the input again as the input will be asked again

Upvotes: 1

Views: 164

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

How do I return an Input from one method to the other without using a global variable and without the input being asked again in java

Do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.print("Enter an integer: ");
        int n = getInput();
        showInput(n);
    }

    public static int getInput() {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        return num;
    }

    public static void showInput(int n) {
        System.out.println("The input is " + n);
    }
}

A sample run:

Enter an integer: 10
The input is 10

Upvotes: 2

dan1st
dan1st

Reputation: 16373

Just pass it as parameter:

public static void main(String[] args) {
    int in=getInput();
    showInput(i);
}
public static int getInput(){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    return num;
}
public static void showInput(int i){
    System.out.println(i);
}

This saves the input into a local variable that is only valid inside the main method.

Also, the showInput method gets an int value as parameter so it does not need to load it by itself.

Or, you can pass the return value directly:

public static void main(String[] args) {
    showInput(getInput());
}

This skips saving the input in a variable and passes it directly.

Note that both ways are essentially the same.

Also note that you should close() the Scanner when you don't need it anymore. This will be important if you handle streams other than System.in.

Closing means saying you won't need a resource any more and giving it away to the operating system.

But if you close() a Scanner, the resource it is built on will also be closed.

That means, you won't be able to use System.in after this again. You may want to have one Scanner that is instantiated and closed in the main method and passed to the getInput() method.

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int in=getInput(sc);
    showInput(i);
    sc.close();
}
public static int getInput(Scanner sc){
    int num = sc.nextInt();
    return num;
}
public static void showInput(int i){
    System.out.println(i);
}

Or(even better but you that is more advanced) with a try-with-resources:

public static void main(String[] args) {
    try(Scanner sc=new Scanner(System.in)){
        int in=getInput(sc);
        showInput(i);
    }
}
public static int getInput(Scanner sc){
    int num = sc.nextInt();
    return num;
}
public static void showInput(int i){
    System.out.println(i);
}

If you don't want the Scanner to be instantiated in the main method, you can create a new method for this:

public static void main(String[] args) {
    try(Scanner sc=getScanner()){
        int in=getInput(sc);
        showInput(i);
    }
}
public static Scanner getScanner(){
    return new Scanner(System.in);
}
public static int getInput(Scanner sc){
    int num = sc.nextInt();
    return num;
}
public static void showInput(int i){
    System.out.println(i);
}

Upvotes: 1

Abhinav Chauhan
Abhinav Chauhan

Reputation: 1384

 **Sure do this**
public class Test {
public static int getInput(){
    System.out.println("enter a number");
    return new Scanner(System.in).nextInt();
}
public static void showInput(int number){
    System.out.println(number);
}
public static void main(String[] args) {

    showInput(getInput());
}
}

Upvotes: 1

Willem
Willem

Reputation: 1060

Is this what you are looking for?

public static void main(String[] args) {
    showInput(getInput());
}

public static int getInput(){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    return num;
}
public static void showInput(int input){
    System.out.println(input);
}

Upvotes: 2

Related Questions