2 Replies - 8423 Views - Last Post: 09 November 2008 - 06:20 PM Rate Topic: ***** 1 Votes

#1 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Password login

Post icon  Posted 09 November 2008 - 12:05 PM

Im just began learning java and from what i've learned i created a java login that works, but not like i would want it to.

What i want it to do, is that once the user has typed in the new password, and they are asked to login with thew password they created; if the password they enter dosent match the password they had just created, the login should erase what they entered and ask them again to enter there password till it is correct or repeat asking for the password for a number of times, before the application decides to teminate instead.

Heres the code I created.

package project3;

import javax.swing.JOptionPane;

public class Main {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {

		
		String  pass =" ";
		String logon = " ";
		String first;
		String second;

		int number1;
		int number2;
		int sum;
	   
pass = JOptionPane.showInputDialog("Enter new password"); // user  input password they like
		
logon = JOptionPane.showInputDialog("Enter your Password"); //user inputs there password

  if (logon.equals(pass)){
			JOptionPane.showMessageDialog(null, "You entered the correct password", "OK", JOptionPane.PLAIN_MESSAGE); // display corerect password message, if answers is correct, then procced to calculator
	   
		//calculator
		
		first = JOptionPane.showInputDialog("Enter first Number");
		second = JOptionPane.showInputDialog("Enter second number");

		number1 = Integer.parseInt(first);
		number2 = Integer.parseInt(second);

		sum = number1 + number2; // calculate sum of both numbers

		JOptionPane.showMessageDialog(null, " These two numbers equal " + sum, " Amount", JOptionPane.PLAIN_MESSAGE); // display amount of both numbers
  }	 
  
	else 
		JOptionPane.showMessageDialog(null, "You May Not Enter", "NOT GOOD", JOptionPane.PLAIN_MESSAGE); //display incorrect password message


		 System.exit(0); // terminate application if password is incorrect

}
	}



*Welcome at DIC
*Edited to add the [ code] tags. In the future please :code"

This post has been edited by pbl: 09 November 2008 - 02:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Password login

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Password login

Posted 09 November 2008 - 02:23 PM

May be a simple loop

import javax.swing.JOptionPane;

public class Main2 {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {


		String  pass =" ";
		String logon = " ";
		String first;
		String second;

		int number1;
		int number2;
		int sum;

		pass = JOptionPane.showInputDialog("Enter new password"); // user  input password they like

		int nbTrial = 3;
		for(int i = 0; i < nbTrial; i++)
		{
			logon = JOptionPane.showInputDialog("Enter your Password"); //user inputs there password
			if (logon.equals(pass)){
				JOptionPane.showMessageDialog(null, "You entered the correct password", "OK", JOptionPane.PLAIN_MESSAGE); // display corerect password message, if answers is correct, then procced to calculator

				//calculator

				first = JOptionPane.showInputDialog("Enter first Number");
				second = JOptionPane.showInputDialog("Enter second number");

				number1 = Integer.parseInt(first);
				number2 = Integer.parseInt(second);

				sum = number1 + number2; // calculate sum of both numbers

				JOptionPane.showMessageDialog(null, " These two numbers equal " + sum, " Amount", JOptionPane.PLAIN_MESSAGE); // display amount of both numbers
				break;
			}	 

			else {

				if(i < nbTrial - 1)
					JOptionPane.showMessageDialog(null, "Invalid password try again", "NOT GOOD", JOptionPane.PLAIN_MESSAGE); //display incorrect password message
				else
					JOptionPane.showMessageDialog(null, "You May Not Enter", "NOT GOOD", JOptionPane.PLAIN_MESSAGE); //display incorrect password message

			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Re: Password login

Posted 09 November 2008 - 06:20 PM

Thank you so much. I tried using the for and while statments myself but they didnt work properly. but after seeing where you placed the staments, i know where i went wrong. Thx alot again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1