1

I am trying to get the user to enter a four digit pin but I don't want it to be printed out on the screen as they type it. I have tried using System.console().readPassword(); like so:

import java.util.Scanner;
import java.io.*;
import java.util.InputMismatchException;
public class VirtualATM{
    private static String Details;  
    public static void main(String [] args){
        try{
            //Create variables & scanner to be used throughout the program.
            Scanner sc = new Scanner(System.in);
            boolean RegisterOrExist = false;
            int cardNo = 0;
            String DirToWriteFile = System.getProperty("user.dir") + "/VirtualATM.txt"; //Get path to write text file to.
            DirToWriteFile.trim();
            File file = new File(DirToWriteFile);
            // if file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            //Create writer to write to files.
             FileWriter fw = new FileWriter(file.getAbsoluteFile());
             BufferedWriter bw = new BufferedWriter(fw);
            System.out.println("Enter card number, or type register to register a new card.");
            String register = sc.nextLine();
            if(register.equalsIgnoreCase("register")){
                RegisterOrExist = false;
                RegisterNewCard();
            } else {
            RegisterOrExist = true;
            cardNo = Integer.valueOf(register);
            }
            bw.write(Details);
            //Close the writer.
            bw.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    /*** Method for registering a new card **/
    public static void RegisterNewCard(){
        Scanner sc = new Scanner(System.in);
        System.out.print("Name: ");
        String name = sc.nextLine();
        int MaxVal = Integer.MAX_VALUE;
        int CardNo =  new java.util.Random().nextInt(MaxVal) + 1;
        int balance = new java.util.Random().nextInt(10000) + 1;
        boolean OverDraft = false;
        int OverDraftLimit = 0;
        if(OverDraft = true){
            OverDraftLimit = 250;
        }
        char [] PIN = {};
        System.out.println("Create a FOUR digit pin: ");
        try{
            PIN = System.console().readPassword();
        }catch(InputMismatchException e){
            e.printStackTrace();
        }
        int P1 = (int) PIN[0];
        int P2 = (int) PIN[1];
        int P3 = (int) PIN[2];
        int P4 = (int) PIN[3];
        int [] PinNo = {P1, P2, P3, P4};
        Details = "Name:   " + name + "   CardNo:   " + CardNo + "   Current Balance:   " + balance + "   overdraft?   " + OverDraft + "   OverDraftLimit:   " + OverDraftLimit + "   pin:   " + PinNo;
    }
}

I then try to write the pin int [] pinNo = {P1, P2, P3, P4} to a text file using the BufferedWriter. I get the following this text in the text file when I input the pin as 2566: [I@42a57993 Is there another way to read a password without it printing on the screen as the user types?

OO7
  • 2,785
  • 1
  • 21
  • 33
James
  • 338
  • 1
  • 2
  • 16

3 Answers3

2

problem:

pin:   " + PinNo;

You are actually printing the memory location of the array object not the array elements itself

solution:

you can get each index of the PinNo array and append each of them to the string

 "pin:   " + PinNo[0] + PinNo[1] + PinNo[2] + PinNo[3]
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
2

well this line

bw.write(Details);

is writing the details in the file where

Details = "..otherdetails..."+"pin:   " + PinNo;

shows that you are writing the hash of the array. So simply replace "pin: " + PinNo with the actual elements of the array

You can simply follow what Rod said or try this

Details = "..otherdetails..."+"pin:   " + java.util.Arrays.toString(PinNo)

Tip - using this makes you password more secure try like this

char[] PIN = System.console.readPassword();  
Arrays.fill(password, ' ');
SparkOn
  • 8,806
  • 4
  • 29
  • 34
1

If you are trying to run your code on Eclipse IDE console then it'll not work. It is bug System.console() (Java 6.0) returns null when running inside Eclipse.

You need to run from command prompt or teminal, etc.

For more on this see these post :-

  1. Hide input on command line - stackoverflow
  2. Masking password input from the console - stackoverflow
Community
  • 1
  • 1
OO7
  • 2,785
  • 1
  • 21
  • 33