Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Simple Password checker into JOptionPane help!

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Password checker into JOptionPane help!

    So i have this source code...

    package Homework1;
    import java.util.Scanner; 
    //I had to use scanner in this program because I had to create objects that were in the Scanner class, such as in row 16.
    // The password is Tradd.
    /**
    * @author Tradd Edwin Specter*
    * @version 1.69, 1 September 2014 *
    * This program confirms a password typed into a *
    * console window *
    */
     
    public class Homework1
    { 
    	public static void main(String[] args) {
    		//Needs to add a scanner to the program to continue on
    		Scanner keyboard = new Scanner(System.in);
     
    		//The values are now set up. 
    		int tries = 1;
    		String password = "Tradd";
     
    		//It tells the name of the program to the user.
    		System.out.println("Password Checker");
    		//Asks the user to insert their username which can be anything 
    		System.out.println("Please enter a Username:");
     
    		//takes the username from the keyboard
    		String username = keyboard.next(); 
    		//Once a username is put in, the program asks for a password
    		System.out.println("Please enter your password:"); 
    		String temp1 = keyboard.next();
    		// if the password is correct, the user will be approved and the program will be terminated.
    		if(temp1.equals(password)) 
    		{
    		System.out.println("You are approved by access control!"); 
    		System.exit(0);
    		}
    		//If the password is incorrect then the program will allow the user another chance at getting the password correct.
    		else
    		{
    			System.out.println("Try Again:");		
    			}
     
    		String temp2 = keyboard.next();
    		//same as the first "if" in the program
    		if (temp2.equals(password))
    		{
    			System.out.println("You are approved by access control!");
    		}
    		// If the user inputs the wrong password again, the program will tell them sorry then terminate the program.
    		else
    		{
    			System.out.println("Sorry");
    		}
    		System.exit(0);
    	}
    }
    Now I need to create a second version of this program that uses JOptionPane to get the inputs from the user and show the output! help!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Password checker into JOptionPane help!

    Have you reviewed writing dialogs with JOptionPane? Once you've done that, replace the console I/O with JOptionPane dialogs. Pretty simple, really.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Password checker into JOptionPane help!

    alright i did that and i think it is right! could you run it for me yourself and see if it is right?
    package Homework1;
    import java.awt.Component;
    import java.util.Scanner; 
     
    import javax.swing.JOptionPane;
    //I had to use scanner in this program because I had to create objects that were in the Scanner class, such as in row 16.
    // The password is Tradd.
    /**
    * @author Tradd Edwin Specter*
    * @version 1.69, 1 September 2014 *
    * This program confirms a password typed into a *
    * console window *
    */
     
    public class JoptionPaneHW1
    { 
    	public static void main(String[] args) {
    		//Needs to add a scanner to the program to continue on
    		Scanner keyboard = new Scanner(System.in);
     
    		//The values are now set up. 
    		int tries = 1;
    		String password = "Tradd";
    		Component frame = null;
    		//It tells the name of the program to the user.
    		JOptionPane.showMessageDialog(frame, "Password Checker");
    		//Asks the user to insert their username which can be anything 
    		JOptionPane.showMessageDialog(frame, "Please enter a Username:");
     
    		//takes the username from the keyboard
    		String username = keyboard.next(); 
    		//Once a username is put in, the program asks for a password
    		JOptionPane.showMessageDialog(frame, "Please enter your password:"); 
    		String temp1 = keyboard.next();
    		// if the password is correct, the user will be approved and the program will be terminated.
    		if(temp1.equals(password)) 
    		{
    			JOptionPane.showMessageDialog(frame, "You are approved by access control!"); 
    		System.exit(0);
    		}
    		//If the password is incorrect then the program will allow the user another chance at getting the password correct.
    		else
    		{
    			JOptionPane.showMessageDialog(frame, "Try Again:");		
    			}
     
    		String temp2 = keyboard.next();
    		//same as the first "if" in the program
    		if (temp2.equals(password))
    		{
    			JOptionPane.showMessageDialog(frame, "You are approved by access control!");
    		}
    		// If the user inputs the wrong password again, the program will tell them sorry then terminate the program.
    		else
    		{
    			JOptionPane.showMessageDialog(frame, "Sorry");
    		}
    		System.exit(0);
    	}
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Password checker into JOptionPane help!

    Did you run it? Was input entered in a JOptionPane dialog? Is the Scanner object still needed?

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Password checker into JOptionPane help!

    I ran it and it came up as a dialog box but i also had to input my password and username into the console instead of the dialog boxes. How would i do without Scanner?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Password checker into JOptionPane help!

    The JOptionPane dialogs can also be used to collect input. Keep reading that section.

  7. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Password checker into JOptionPane help!

    alright this is what i have now....

    package Homework1;
    import java.awt.Component;
    import java.util.Scanner; 
     
    import javax.swing.JOptionPane;
    //I had to use scanner in this program because I had to create objects that were in the Scanner class, such as in row 16.
    // The password is Tradd.
    // had to import java.swing.JOptionPane to create the dialog boxes
    /**
    * @author Tradd Edwin Specter*
    * @version 1.69, 1 September 2014 *
    * This program confirms a password typed into a *
    * console window *
    */
     
    public class JoptionPaneHW1
    { 
    	public static void main(String[] args) {
    		//Needs to add a scanner to the program to continue on
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//The values are now set up. 
    		int tries = 1;
    		String password = "Tradd";
    		Component frame = null;
    		//It tells the name of the program to the user.
    		//created a component frame to be able to use a variable to make the dialog boxes
     
    		String username1 = (String)JOptionPane.showInputDialog(
                    frame,
                    "Password Checker:\n"
                    + "please enter your username:",
                    "Customized Dialog",
                    JOptionPane.PLAIN_MESSAGE);
     
     
    		//Once a username is put in, the program asks for a password
    		String password1 = (String)JOptionPane.showInputDialog(
                    frame, "Please enter your password:",
                    "Customized Dialog",
                    JOptionPane.PLAIN_MESSAGE);
     
    	     String showinputdialog = keyboard.next();
    		// if the password is correct, the user will be approved and the program will be terminated.
    		if(showinputdialog.equals(password)) 
    		{
    			JOptionPane.showMessageDialog(frame, "You are approved by access control!"); 
    		System.exit(0);
    		}
    		//If the password is incorrect then the program will allow the user another chance at getting the password correct.
    		else
    		{
    			JOptionPane.showMessageDialog(frame, "Try Again:");		
    			}
     
    		String temp2 = keyboard.next();
    		//same as the first "if" in the program
    		if (temp2.equals(password))
    		{
    			JOptionPane.showMessageDialog(frame, "You are approved by access control!");
    		}
    		// If the user inputs the wrong password again, the program will tell them sorry then terminate the program.
    		else
    		{
    			JOptionPane.showMessageDialog(frame, "Sorry");
    		}
    		System.exit(0);
    	}
    }

    The problem is that i can get to the dialog box that says please enter your password but then the program terminates for no reason even though i have if-else statements

  8. #8
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Simple Password checker into JOptionPane help!

    Your Scanner objects are leaving an input when the password
    is typed in. As mentioned by Greg, you do not need the Scanner class
    in this sort of program. All input is controlled by the JOptionPane library.

    Also check what value you are comparing to in your 'if' statement

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  9. #9
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Password checker into JOptionPane help!

    okay this is what i have at the moment....
    package Homework1;
    import java.awt.Component;
     
    import javax.swing.JOptionPane;
    //I had to use scanner in this program because I had to create objects that were in the Scanner class, such as in row 16.
    // The password is Tradd.
    // had to import java.swing.JOptionPane to create the dialog boxes
    /**
    * @author Tradd Edwin Specter*
    * @version 1.69, 1 September 2014 *
    * This program confirms a password typed into a *
    * console window *
    */
     
    public class JoptionPaneHW1
    {
     
    	public static void main(String[] args) {
    		//Needs to add a scanner to the program to continue on
     
     
    		//The values are now set up. 
    		int tries = 1;
    		String password = "Tradd";
    		Component frame = null;
    		//It tells the name of the program to the user.
    		//created a component frame to be able to use a variable to make the dialog boxes
     
    		String username1 = JOptionPane.showInputDialog(
                    frame,
                    "please enter your username:",
                    "Password Checker",
                    JOptionPane.PLAIN_MESSAGE);
     
    		String password1 = JOptionPane.showInputDialog(
                    frame, "Please enter your password:",
                    "Password Checker",
                    JOptionPane.PLAIN_MESSAGE);
     
     
    		//Once a username is put in, the program asks for a password
     
     
    		// if the password is correct, the user will be approved and the program will be terminated.
    		if(password1.equals(password)) 
    		{
    			JOptionPane.showMessageDialog(frame, "You are approved by access control!"); 
    		System.exit(0);
    		}
    		//If the password is incorrect then the program will allow the user another chance at getting the password correct.
    		else
    		{
    			JOptionPane.showMessageDialog(frame, "Access Denied");		
    		}
     
    		String temp2 = JOptionPane.showInputDialog("Try Again:");
    		//same as the first "if" in the program
    		if (temp2.equals(password))
    		{
    			JOptionPane.showMessageDialog(frame, "You are approved by access control!");
    		}
    		// If the user inputs the wrong password again, the program will tell them sorry then terminate the program.
    		else
    		{
    			JOptionPane.showMessageDialog(frame, "Sorry");
    		}
    		System.exit(0);
    	}
     
    }
    it works great but the only thing that is wrong is that it says access denied and i want it to go straight to Try Again: i have tried to put String temp2 inside the else statement but it wont let me use the string outside of the else statement.
    Last edited by HappyCamper; September 2nd, 2014 at 08:16 AM.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Password checker into JOptionPane help!

    Your current approach of asking the same question until you get a correct answer is not sustainable. How many times will you write the same code? Rather, if you want to repeat a section of code, it should be in a loop. This is the thought Ada was trying to develop in your previous thread. I believe you responded that the looping concept was beyond you at that point, but the general idea is:
    boolean responseIncorrect = true;
    while ( responseIncorrect )
    {
        // ask the question, get the response here
     
        // if the response is correct, set responseIncorrect = false;
    }

  11. #11
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Simple Password checker into JOptionPane help!

    Quote Originally Posted by HappyCamper View Post
    //If the password is incorrect then the program will allow the user another chance at getting the password correct.
    else
    {
    JOptionPane.showMessageDialog(frame, "Access Denied");
    }
    [/highlight]
    it works great but the only thing that is wrong is that it says access denied and i want it to go straight to Try Again: i have tried to put String temp2 inside the else statement but it wont let me use the string outside of the else statement.
    The reason for that is because when the 'else' statement executes, there is nothing more the application can
    do. It will not go to your next 'if' statement because an else always follows the nearest 'if' statement
    above it. I suggest implementing a loop system, as Greg mentioned.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. Need help with programming a simple password checker.
    By HappyCamper in forum What's Wrong With My Code?
    Replies: 22
    Last Post: September 1st, 2014, 12:32 PM
  2. How to make a very simple Password Strength indicator?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 18th, 2014, 07:40 PM
  3. JAVA NOOB: Password Strength Checker NEED HELP
    By blobby404 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 18th, 2014, 05:55 PM
  4. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 03:59 PM
  5. Password Strength Checker
    By uthuth in forum Object Oriented Programming
    Replies: 1
    Last Post: February 6th, 2010, 04:09 PM