1

I guess my title isn't very clear and needs a code example so here you go:

public class ATM {


public static void main(String[] args) {

     Keypad K=new Keypad();
     K.mypin(pin);
}
}

That is the main method, now here is a method in another class:

public class Keypad{
    public void mypin(int pin) {
        System.out.print("Please enter your pin");
        pin=scan.nextInt(); 
        System.out.print(pin);  
    }
}

How to include pin=scan.nextInt(); in my main method and make this work normally? You might ask me why I want it this way and it is just because that is what I was asked to do.

2
  • This is not clear to me. Were you asked to read pin (using Scanner) in the main method and pass that value to mypin? Or to read the value inside the mypin? Sep 10, 2019 at 8:40
  • 1
    To read pin in the main method.
    – IamTrying
    Sep 10, 2019 at 8:44

2 Answers 2

4

If I understood you correctly, you want something along those lines :

public class ATM {
    public static void main(String[] args) {
        System.out.print("Please enter your pin");
        Scanner sc = new Scanner(System.in);
        Keypad K=new Keypad();
        K.mypin(sc.nextInt());
    }
}

public class Keypad{
    public void mypin(int pin) {
        System.out.print(pin);  
    }
}
7
  • This makes more sense to me Sep 10, 2019 at 8:42
  • 1
    @lealceldeiro I don't know, it follows the title and description better, but it's incoherent with the class names. Usually you'd want the Keypad to be what provides the input
    – Aaron
    Sep 10, 2019 at 8:44
  • I was just about to upload this as answer. As the original poster says this is an assignment and thus we should take it literally. This solves the assignment literally.
    – Snagtz
    Sep 10, 2019 at 8:47
  • @Aaron yes, why would a Keypad print your pin instead of requesting it? Sep 10, 2019 at 8:47
  • I guess that is the way to go Aaron, I will try this and get back to you.
    – IamTrying
    Sep 10, 2019 at 8:50
0

You can use Scanner class with input stream System.in.

It is ATM class:

package user.input;

public class ATM {

  public static void main(String[] args) {
    Keypad keypad = new Keypad();

    try {
      int userPin = keypad.enterPin();
      System.out.printf("User Pin: %s", userPin);
    } catch (Exception e) {
      System.err.println("Occured error while entering user's PIN.");
      e.printStackTrace();
    }

  }
}

It is Keypad class:

package user.input;

import java.util.Scanner;

    public class Keypad {

      public int enterPin() throws Exception {
        Scanner scanner = null;
        try {
          scanner = new Scanner(System.in);
          System.out.print("Please enter your pin: ");
          int inputNum = scanner.nextInt(); // Read user input
          return inputNum;
        } catch (Exception e) {
          throw e;
        } finally {
          if (scanner != null) {
            scanner.close();
          }
        }
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.