Results 1 to 4 of 4
  1. #1
    Collinryans is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default How to replace typed characters?

    Hello, my objective is simple:

    I just want the display to output "*" for every character the user types in.

    For example if he wants types "password" in the program the program displays "********".

    I should mention im doing this in JCreator and not in a password field box, rather just the general output box it comes with.



    Just incase it maybe relevent the code im trying to implement this idea is:

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.Vector;
    
    public class MedicalOrganizer {
    	
    	
    	public static void main(String[] args) {
    		
    		Scanner get = new Scanner (System.in);
    		Vector strUser, strPassword;
    		String strLoginU, strLoginP;
    		boolean blnLogged = false;
    		
    		strUser	 = new Vector();
    		strPassword = new Vector();
    		
    		System.out.println ("Login...");
    		
    		while (blnLogged == false) {
    			
    			System.out.print ("\nUsername: ");
    			strLoginU = get.next();
    			System.out.print ("Password: ");
    			strLoginP = get.next();
    			
    			int i = 0;
    			
    			try {
    				DataInputStream uin = new DataInputStream(new FileInputStream("Usernames.ini"));
    				DataInputStream pin = new DataInputStream(new FileInputStream("Passwords.ini"));
    				while ((uin.available() != 0) && (pin.available() != 0)) {
    				
    					strUser.addElement(uin.readLine());
    					strPassword.addElement(pin.readLine());
    					
    					if ((strLoginU.compareTo(String.valueOf(strUser.elementAt(i))) == 0) && (strLoginP.compareTo(String.valueOf(strPassword.elementAt(i))) == 0)) {
    						blnLogged = true;
    					}
    					
    					i++;
    				}
    			} catch (Exception e) {
    				System.err.println (e);
    			}
    			
    			if (blnLogged == false) {
    				
    				System.out.println ("\nIvalid username/password, please try again.");
    			}
    		}
    		
    		System.out.println ("\nAccess granted.");
    	}
    }
    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by Collinryans View Post
    Hello, my objective is simple:

    I just want the display to output "*" for every character the user types in.

    For example if he wants types "password" in the program the program displays "********".

    I should mention im doing this in JCreator and not in a password field box, rather just the general output box it comes with.
    Read the API documentation for the Console class; it can read passwords for you while nothing is echoed on the console. The documentation also contains a simple example.

    kind regards,

    Jos

  3. #3
    Collinryans is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the reply. I did look into the API just now, however I keep getting "Console not found" error.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    10

    Default

    I'm not sure that the Console class can be used in all environments. If you're allowed to create a Swing GUI, then the answer would be trivial: use a JPasswordField.

Similar Threads

  1. is there anyway to replace java.IO
    By nobody58 in forum Advanced Java
    Replies: 1
    Last Post: 03-19-2010, 02:19 PM
  2. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 01:22 PM
  3. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:58 PM
  4. First Typed via video instruction
    By ZAXTHEGREAT in forum New To Java
    Replies: 15
    Last Post: 07-23-2007, 05:27 PM
  5. Char type for first character typed?
    By Sageinquisitor in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •