469,950 Members | 2,237 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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 GregorianCalendar) and the birthday year and the last two letters of the last name, all concatenated in lower case. The GregorianCalendar thingy keeps me from making this program work, and i don't really know how to use GregorianCalendar 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=JOptionPane.showInputDialog("Please Insert Your First Name:");

lastname=JOptionPane.showInputDialog("Please Insert Your Last Name:");

bday=JOptionPane.showInputDialog("Please Insert Your Birthday in the format dd/mm/yyyy: ");

char a=firstname.charAt(0);

char b=firstname.charAt(1);

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

char d= //i believe GregorianCalendar 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.parseInt(String s) to convert a string in an integer (because in this case, the bday string is a sequence of digits), (for example Integer.parseInt("1984") has as a result 1984), though i am not sure where again :S

char e=lastname.charAt(lastname.length()-2);

char f=lastname.charAt(lastname.length()-1);

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

password = new String(characters);


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

Oct 16 '06 #1
1 4232
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.util.*;
  3. public class Password {
  4.     public static void main (String[] args) {
  5.         String firstname = JOptionPane.showInputDialog("Please Insert Your First Name:");
  6.         String lastname = JOptionPane.showInputDialog("Please Insert Your Last Name:");
  7.         String bday = JOptionPane.showInputDialog("Please Insert Your Birthday in the format dd/mm/yyyy: ");
  9.         String fName = firstname.substring(0, 2);
  10.         String sName = lastname.substring(lastname.length() - 2, lastname.length());
  11.         int year = Integer.parseInt(bday.substring(6, 10));
  12.         GregorianCalendar a = new GregorianCalendar();
  13.         a.setTime(new Date());
  14.         int age = a.get(GregorianCalendar.YEAR) - year;
  15.         String password = fName + age + sName;
  16.         password = password.toLowerCase();
  17.         System.out.println("Hello, "+firstname+" "+lastname+"! Your password is "+password);
  18.     }
  19. }
Oct 17 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by cg_news | last post: by
4 posts views Thread by chennakeshava_ramesh | last post: by
3 posts views Thread by | last post: by
56 posts views Thread by spibou | last post: by
1 post views Thread by cnixuser | last post: by
3 posts views Thread by Z.K | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.