Question in using GregorianCalendar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AliceG
    New Member
    • Oct 2006
    • 2

    #1

    Question in using GregorianCalendar

    Hi all,

    By using JOptionPane, I'm trying to do this little program with Notepad for generating a password from the user's first two letters of the first name, the age calculated as a difference of the current year (by use of GregorianCalend ar) and the birthday year and the last two letters of the last name, all concatenated in lower case. The GregorianCalend ar thingy keeps me from making this program work, and i don't really know how to use GregorianCalend ar and insert it effectively in my lines of code so that all would fit.
    I would greatly appreciate any input, and i'll send my code so far:

    import javax.swing.*;

    public class Password{

    public static void main (String[] args) {

    String firstname, lastname, bday, password;

    firstname=JOpti onPane.showInpu tDialog("Please Insert Your First Name:");

    lastname=JOptio nPane.showInput Dialog("Please Insert Your Last Name:");

    bday=JOptionPan e.showInputDial og("Please Insert Your Birthday in the format dd/mm/yyyy: ");

    char a=firstname.cha rAt(0);

    char b=firstname.cha rAt(1);

    char c=bday.substrin g(6); //to get the birthday year

    char d= //i believe GregorianCalend ar goes here and that "d" is the character result of the subtraction between the birthday year and current year; also i believe i need to use int Integer.parseIn t(String s) to convert a string in an integer (because in this case, the bday string is a sequence of digits), (for example Integer.parseIn t("1984") has as a result 1984), though i am not sure where again :S

    char e=lastname.char At(lastname.len gth()-2);

    char f=lastname.char At(lastname.len gth()-1);

    char[] characters = {a,b,c,d,e,f};

    password = new String(characte rs);

    password=passwo rd.toLowerCase( );

    System.out.prin tln("Hello, "+firstname +" "+lastname+ "! Your password is "+password) ;
    }
    }


    Thanks,
    Alice
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Code:
    import javax.swing.*;
    import java.util.*;
    public class Password {
    	public static void main (String[] args) {
    		String firstname = JOptionPane.showInputDialog("Please Insert Your First Name:");
    		String lastname = JOptionPane.showInputDialog("Please Insert Your Last Name:");
            String bday = JOptionPane.showInputDialog("Please Insert Your Birthday in the format dd/mm/yyyy: ");
    
    		String fName = firstname.substring(0, 2);
    		String sName = lastname.substring(lastname.length() - 2, lastname.length());
    		int year = Integer.parseInt(bday.substring(6, 10));
    		GregorianCalendar a = new GregorianCalendar();
    		a.setTime(new Date());
    		int age = a.get(GregorianCalendar.YEAR) - year;
    		String password = fName + age + sName;
    		password = password.toLowerCase();
    		System.out.println("Hello, "+firstname+" "+lastname+"! Your password is "+password);
    	}
    }

    Comment

    Working...