Results 1 to 3 of 3
  1. #1
    zneith is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default my password program

    Hi everyone. I am relatively new to java and have a few questions on my most recent program. I haven't had much help in my AP class on it. It is mostly my own side project to further my education in Java. It most likely has many easier ways to write this but this is the way I went with it without prior practice in programming yet. My question is in my program, it asked for a user name and password. I also have a new user button that asks what you would like the user name and password to be. With the user's input, it writes that to a text file. Now if you close the program, i have no code to make the program look back in the text file for the new user name and password; So it just uses the default one that is made. I am not sure how to write this any help would be appreciated. Don't be to critical though for this is my first "major" program I created from scratch. Thanks all!
    Zakk N.
    /*Zakk Neith
    Password Program
    */
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;


    public class PasswordProgram implements ActionListener{

    static JFrame frame1 = new JFrame("Please Choose an Option");
    static JFrame frame2 = new JFrame("Please Enter Your UserName and Password");
    static JFrame frame3 = new JFrame("Game Passwords");
    static JFrame frame4 = new JFrame("Personal Passwords");
    static JFrame frame5 = new JFrame("Internet Passwords");
    static JFrame frame6 = new JFrame("Change Password");

    String username = "admin";

    String pass = "admin";

    JPanel textPanel, panelForTextFields, completionPanel, textPanel2, textPanel3, textPanel4, textPanel5;
    //--------------------------------------------------------------------------------------------------------
    JLabel titleLabel, usernameLabel, passwordLabel, userLabel, passLabel, loggedInLabel,titleLabel2, ccPasswordLabel;
    JLabel titleLabel3, titleLabel4, titleLabel5, cPasswordLabel, nPasswordLabel;
    //-------------------------------------------------------------------------------------------------------------------
    JTextField usernameField, gamePassTextField, personalPassTextField, internetPassTextField;
    JTextField gameTextField, personalTextField, internetTextField, cPasswordTextField, nPasswordTextField, ccPasswordTextField;
    //------------------------------------------------------------------------------------------------------------------------------
    JButton loginButton, gamePassButton, personalPassButton, internetButton, textPanel2Button, quitButton, newUserButton;
    JButton gameCloseButton, personalCloseButton, internetCloseButton, changePasswordButton, confirmPassChange;
    //------------------------------------------------------------------------------------------------------------
    JPasswordField loginField;
    //------------------------------------------------------------------------------------------------------------

    Scanner reader = new Scanner(System.in);

    public JPanel createContentPane() {

    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    titleLabel = new JLabel("Login Screen");
    titleLabel.setLocation(0, 0);
    titleLabel.setSize(290, 30);
    titleLabel.setHorizontalAlignment(0);
    totalGUI.add(titleLabel);

    textPanel = new JPanel();
    textPanel.setLayout(null);
    textPanel.setLocation(10,35);
    textPanel.setSize(70,80);
    totalGUI.add(textPanel);

    usernameLabel = new JLabel("Username");
    usernameLabel.setLocation(0,0);
    usernameLabel.setSize(70,40);
    usernameLabel.setHorizontalAlignment(4);
    textPanel.add(usernameLabel);

    passwordLabel = new JLabel("Password");
    passwordLabel.setLocation(0,40);
    passwordLabel.setSize(70,40);
    passwordLabel.setHorizontalAlignment(4);
    textPanel.add(passwordLabel);

    panelForTextFields = new JPanel();
    panelForTextFields.setLayout(null);
    panelForTextFields.setLocation(110,40);
    panelForTextFields.setSize(110,70);
    totalGUI.add(panelForTextFields);

    usernameField = new JTextField(8);
    usernameField.setLocation(0,0);
    usernameField.setSize(100,30);
    panelForTextFields.add(usernameField);

    loginField = new JPasswordField(8);
    loginField.setLocation(0,40);
    loginField.setSize(100,30);
    panelForTextFields.add(loginField);

    completionPanel = new JPanel();
    completionPanel.setLayout(null);
    completionPanel.setLocation(240,35);
    completionPanel.setSize(70,80);
    totalGUI.add(completionPanel);

    userLabel = new JLabel("Wrong");
    userLabel.setForeground(Color.red);
    userLabel.setLocation(0,0);
    userLabel.setSize(50,40);
    completionPanel.add(userLabel);

    passLabel = new JLabel("Wrong");
    passLabel.setForeground(Color.red);
    passLabel.setLocation(0,40);
    passLabel.setSize(50,40);
    completionPanel.add(passLabel);

    loginButton = new JButton("Login");
    loginButton.setLocation(200,120);
    loginButton.setSize(80,30);
    loginButton.addActionListener(this);
    totalGUI.add(loginButton);

    changePasswordButton = new JButton("Change Password");
    changePasswordButton.setLocation(135, 175);
    changePasswordButton.setSize(150,30);
    changePasswordButton.addActionListener(this);
    totalGUI.add(changePasswordButton);

    newUserButton = new JButton("New User");
    newUserButton.setLocation(10,175);
    newUserButton.setSize(125,30);
    newUserButton.addActionListener(this);
    totalGUI.add(newUserButton);

    quitButton = new JButton("Quit");
    quitButton.setLocation(25,120);
    quitButton.setSize(80,30);
    quitButton.addActionListener(this);
    totalGUI.add(quitButton);

    JOptionPane.showMessageDialog(null,"Hi, Welcome to the password manager. The Default user name is admin along with the password");



    totalGUI.setOpaque(true);
    return totalGUI;
    }

    public JPanel createContentPane2(){

    JPanel totalGUI1 = new JPanel();
    totalGUI1.setLayout(null);

    textPanel2 = new JPanel();
    textPanel2.setLayout(null);
    textPanel2.setLocation(10,35);
    textPanel2.setSize(70,80);
    totalGUI1.add(textPanel2);

    titleLabel2 = new JLabel("Password Screen");
    titleLabel2.setLocation(0,0);
    titleLabel2.setSize(290,30);
    titleLabel2.setHorizontalAlignment(0);
    totalGUI1.add(titleLabel2);

    gamePassButton = new JButton("Game Pass");
    gamePassButton.setLocation(10,20);
    gamePassButton.setSize(100,30);
    gamePassButton.addActionListener(this);
    totalGUI1.add(gamePassButton);

    personalPassButton = new JButton("Personal Pass");
    personalPassButton.setLocation(10,60);
    personalPassButton.setSize(100,30);


    personalPassButton.addActionListener(this);
    totalGUI1.add(personalPassButton);

    internetButton = new JButton("Internet Pass");
    internetButton.setLocation(10,100);
    internetButton.setSize(100,30);
    internetButton.addActionListener(this);
    totalGUI1.add(internetButton);

    gamePassTextField = new JTextField("Game Passwords");
    gamePassTextField.setLocation(115,20);
    gamePassTextField.setSize(250,30);
    gamePassTextField.setEditable(false);
    totalGUI1.add(gamePassTextField);

    personalPassTextField = new JTextField("Personal Passwords");
    personalPassTextField.setLocation(115,60);
    personalPassTextField.setSize(120, 30);
    personalPassTextField.setEditable(false);
    totalGUI1.add(personalPassTextField);

    internetPassTextField = new JTextField("Internet Passwords");
    internetPassTextField.setLocation(115,100);
    internetPassTextField.setSize(120,30);
    internetPassTextField.addActionListener(this);
    internetPassTextField.setEditable(false);
    totalGUI1.add(internetPassTextField);

    textPanel2Button = new JButton("Log Out");
    textPanel2Button.setLocation(115,180);
    textPanel2Button.setSize(120,30);
    textPanel2Button.addActionListener(this);
    totalGUI1.add(textPanel2Button);


    totalGUI1.setOpaque(true);
    return totalGUI1;
    }


    public JPanel createContentPane3(){

    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    textPanel3 = new JPanel();
    textPanel3.setLayout(null);
    textPanel3.setSize(70,80);
    totalGUI.add(textPanel3);

    titleLabel3 = new JLabel ("Game Passwords");
    titleLabel3.setLocation(0,0);
    titleLabel3.setSize(290,30);
    titleLabel3.setHorizontalAlignment(0);
    totalGUI.add(titleLabel3);

    gameCloseButton = new JButton("Close");
    gameCloseButton.setLocation(115,180);
    gameCloseButton.setSize(120,30);
    gameCloseButton.addActionListener(this);
    totalGUI.add(gameCloseButton);

    gameTextField = new JTextField(150);
    gameTextField.setLocation(0,0);
    gameTextField.setSize(250,150);
    gameTextField.setEditable(true);
    totalGUI.add(gameTextField);

    totalGUI.setOpaque(true);
    return totalGUI;
    }

    public JPanel createContentPane4(){

    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    textPanel4 = new JPanel();
    textPanel4.setLayout(null);
    textPanel4.setSize(70,80);
    totalGUI.add(textPanel4);

    titleLabel4 = new JLabel("Internet Password");
    titleLabel4.setLocation(0,0);
    titleLabel4.setSize(290,30);
    titleLabel4.setHorizontalAlignment(0);
    totalGUI.add(titleLabel4);

    personalTextField = new JTextField(150);
    personalTextField.setLocation(0,0);
    personalTextField.setSize(290,30);
    personalTextField.setEditable(true);
    totalGUI.add(personalTextField);

    personalCloseButton = new JButton("Close");
    personalCloseButton.setLocation(115,180);
    personalCloseButton.setSize(120,30);
    personalCloseButton.addActionListener(this);
    totalGUI.add(personalCloseButton);

    totalGUI.setOpaque(true);
    return totalGUI;
    }

    public JPanel createContentPane5(){

    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    textPanel5 = new JPanel();
    textPanel5.setLayout(null);
    textPanel5.setSize(70,80);
    totalGUI.add(textPanel5);

    titleLabel5 = new JLabel("Personal Passwords");
    titleLabel5.setLocation(0,0);
    titleLabel5.setSize(290,30);
    titleLabel5.setHorizontalAlignment(0);
    totalGUI.add(titleLabel5);

    internetCloseButton = new JButton("Close");
    internetCloseButton.setLocation(115,180);
    internetCloseButton.setSize(120,30);
    internetCloseButton.addActionListener(this);
    totalGUI.add(internetCloseButton);

    totalGUI.setOpaque(true);
    return totalGUI;
    }

    public JPanel createContentPane6(){

    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    cPasswordLabel = new JLabel("Current Password");
    cPasswordLabel.setLocation(0,0);
    cPasswordLabel.setSize(310,30);
    cPasswordLabel.setHorizontalAlignment(0);
    totalGUI.add(cPasswordLabel);

    nPasswordLabel = new JLabel("New Password");
    nPasswordLabel.setLocation(5,69);
    nPasswordLabel.setSize(290,30);
    nPasswordLabel.setHorizontalAlignment(0);
    totalGUI.add(nPasswordLabel);

    confirmPassChange = new JButton("Confirm");
    confirmPassChange.setLocation(115,180);
    confirmPassChange.setSize(120,30);
    confirmPassChange.addActionListener(this);
    totalGUI.add(confirmPassChange);

    cPasswordTextField = new JTextField(8);
    cPasswordTextField.setLocation(0,0);
    cPasswordTextField.setSize(100,30);
    totalGUI.add(cPasswordTextField);

    nPasswordTextField = new JTextField(8);
    nPasswordTextField.setLocation(0,67);
    nPasswordTextField.setSize(100,30);
    totalGUI.add(nPasswordTextField);

    ccPasswordTextField = new JTextField(8);
    ccPasswordTextField.setLocation(0,100);
    ccPasswordTextField.setSize(100,30);
    totalGUI.add(ccPasswordTextField);

    ccPasswordLabel = new JLabel("Confirm New Password");
    ccPasswordLabel.setLocation(28,100);
    ccPasswordLabel.setSize(290,30);
    ccPasswordLabel.setHorizontalAlignment(0);
    totalGUI.add(ccPasswordLabel);

    return totalGUI;

    }

    public void actionPerformed(ActionEvent e){

    if(e.getSource() == newUserButton){
    if(username == "admin" && pass == "admin"){
    System.out.println("What is your username?");
    username = reader.nextLine();
    System.out.println("What is your password?");
    pass = reader.nextLine();

    }
    else if(username != "admin" && pass != "admin"){
    JOptionPane.showMessageDialog(null,"Welcome Back");
    }
    }

    if(e.getSource() == loginButton){
    if(usernameField.getText().compareTo(username) == 0){
    userLabel.setForeground(Color.green);
    userLabel.setText("Correct!");
    }
    else{
    userLabel.setForeground(Color.red);
    userLabel.setText("Wrong UserName");
    }
    if(loginField.getText().compareTo(pass) == 0){
    passLabel.setForeground(Color.green);
    passLabel.setText("Correct");
    }
    else{
    passLabel.setForeground(Color.red);
    passLabel.setText("Wrong Pass");
    }

    if((userLabel.getForeground() == Color.green)
    &&(passLabel.getForeground() == Color.green)){
    titleLabel.setText("Loggin in: One Moment Please");
    loginButton.setEnabled(false);

    JOptionPane.showMessageDialog(null,"The username and password is correct!");

    createAndShowGUI2();

    }
    else{
    JOptionPane.showMessageDialog(null,"Bad username and/or pass","Try Again",JOptionPane.PLAIN_MESSAGE);
    }
    }
    else if(e.getSource() == gamePassButton){
    createAndShowGUI3();


    }
    else if(e.getSource() == internetButton){
    createAndShowGUI4();

    }
    else if(e.getSource() == personalPassButton){
    createAndShowGUI5();

    }
    else if(e.getSource() == gameCloseButton){
    frame3.dispose();
    }
    else if(e.getSource() == personalCloseButton){
    frame4.dispose();

    }
    else if(e.getSource() == internetCloseButton){
    frame5.dispose();
    }

    else if(e.getSource() == textPanel2Button){
    frame2.dispose();
    createAndShowGUI();
    }

    else if(e.getSource() == changePasswordButton){
    createAndShowGUI6();

    }

    else if(e.getSource() == quitButton){
    frame1.dispose();
    }

    if(e.getSource() == confirmPassChange){
    frame6.dispose();
    }

    try
    {
    FileWriter fw = new FileWriter( "Password Program.txt", false );
    BufferedWriter bw = new BufferedWriter( fw );

    bw.write( "Your username is " + username );
    bw.newLine();
    bw.write("Your password is " + pass );
    bw.newLine();
    bw.close();
    System.out.println("File written successfully");
    }
    catch( IOException ioe)
    {
    ioe.printStackTrace();
    }
    }


    private static void createAndShowGUI(){
    JFrame.setDefaultLookAndFeelDecorated(true);

    PasswordProgram demo = new PasswordProgram();
    frame1.setContentPane(demo.createContentPane());

    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame1.setSize(300,250);
    frame1.setVisible(true);
    }



    private static void createAndShowGUI2(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    PasswordProgram demo = new PasswordProgram();
    frame2.setContentPane(demo.createContentPane2());

    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame2.setSize(250,250);
    frame2.setVisible(true);
    }


    private static void createAndShowGUI3(){
    JFrame.setDefaultLookAndFeelDecorated(true);

    PasswordProgram demo = new PasswordProgram();
    frame3.setContentPane(demo.createContentPane3());


    frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame3.setSize(250,250);
    frame3.setVisible(true);
    }



    private static void createAndShowGUI4(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    PasswordProgram demo = new PasswordProgram();
    frame4.setContentPane(demo.createContentPane4());


    frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame4.setSize(250,250);
    frame4.setVisible(true);
    }

    private static void createAndShowGUI5(){
    JFrame.setDefaultLookAndFeelDecorated(true);

    PasswordProgram demo = new PasswordProgram();
    frame5.setContentPane(demo.createContentPane5());


    frame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame5.setSize(250,250);
    frame5.setVisible(true);
    }


    private static void createAndShowGUI6(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    PasswordProgram demo = new PasswordProgram();
    frame6.setContentPane(demo.createContentPane6());
    frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame6.setSize(250,250);
    frame6.setVisible(true);
    }


    public static void main(String [] args){


    SwingUtilities.invokeLater(
    new Runnable(){
    public void run(){
    createAndShowGUI();


    }

    }
    );
    }
    }
    Last edited by zneith; 01-11-2009 at 07:39 AM. Reason: wrong intro

  2. #2
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    hi zneith,

    just for future ref, just copy and paste the part of your code that applies to your question rather than the whole program. People will come on here and try and help you if they can but not usually if it means them giving up a large amount of time just trying to find the problem in your code.
    But anywho, I aint a whinger, see if this helps you at all. I can't seem to see anywhere in your code where you're using a buffered reader to pull the details back out of your code. I think you'll need to start with something along these lines:

    Also, you need to get rid of the "Your username is: " and "Your password is: " when you're writing the text file. Just save the username on the first line and then the password on the next line. It is important that that password is on the next line because we're going to use that to verify if the password corresponds to the username. Also this will allow you to save multiple usernames and passwords, so long as they're one after the other on each new line

    username
    password
    username
    password... etc

    try {
    //Use the fileReader to read the lines that you have save in your text file
    FileReader fr = new FileReader("c:\\myjava\\primes\\00000002.txt"); //replace this with your text files directory
    BufferedReader br = new BufferedReader(fr);

    record = new String();
    while ((record = br.readLine()) != null) // this will read through each line that you have saved until there are no more
    {
    if (record.equals( //username -you will need to put the users input here input here))
    { record = br.readLine(); //check the next line of the text file
    if (record.equals( //password -you will need to put the users input here input here))
    {//grant access - password is correct or whatever
    }
    else {
    //password is incorrect
    }
    }
    }//end of while statement. There is no username with the same name as the users input if nothing has come up yet


    } catch (IOException e) {
    // catch possible io errors from readLine()
    System.out.println("Uh oh, got an IOException error!");
    e.printStackTrace();
    }
    Last edited by 2potatocakes; 01-11-2009 at 06:11 PM.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java Code:
    try
    {
    //
    first_message_digest= java.security.MessageDigest.getInstance("SHA-1");
    if(debug)
    {
    System.out.println("Using: "+first_message_digest.toString());//
    }
    this.isValid=Boolean.valueOf(true);
    return;
    }
    catch(java.security.NoSuchAlgorithmException nsae)
    {
    this.isValid=Boolean.valueOf(false);
    System.out.println(nsae.toString());
    return;
    }
    never store passwords or usernames......
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. 'Password' help
    By iWonder in forum New To Java
    Replies: 20
    Last Post: 12-17-2008, 11:05 PM
  2. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 08:53 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 02:22 PM
  4. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 02:11 PM
  5. How to get password in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:04 PM

Posting Permissions

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