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.

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 96

Thread: Creating Random unused ID number for every library member.

  1. #26
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    ok looking at simpleDateFormat

  2. #27
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by clarky2006 View Post
    ok looking at simpleDateFormat
    Still not able to print loan details..

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    not able to print loan details..
    Please show what you are having problems with.


    See http://docs.oracle.com/javase/tutori...dateintro.html
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Please show what you are having problems with.


    See Dates and Times (The Java™ Tutorials > Internationalization > Formatting)
    Hia...Still having problems with my codes, nearly done now but the loan class is giving me headache..

    /**
     * This Class would hold all loan details.
     * 
     * @author () 
     * @version 1.0
     */
    public class Loan
    {
        // fields for the Loan Class.
        private int bookId;
        private int memberId;
        private GregorianCalendar loanDate;
        private GregorianCalendar dateDueBack;
        private String month = "";
        private String day = "";
        private String year = "";
        private String currentDate = "";
     
     
        /**
         * Constructor for objects of class Loan
         */
        public Loan(int bookId, int memberId)
        {
            // initialised fields.
            this.bookId = bookId;
            this.memberId = memberId;
            this.loanDate = new GregorianCalendar();
     
            GregorianCalendar gregorianCalendar=new GregorianCalendar();            
            month=String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH));            
            day=String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));
            year=String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR));
            currentDate = (day + " / " + month + " / " + year);
     
     
     
        }

    Here im trying to define date when loan is taken out with gregorian Calendar..when a loan object is created a date stamp should be created to be the loan date at the same time, so i can call the getLoandate an get that time. I want to add 21 days to the loan date and be be able to call it via get dateDueBack. Im not sure how to add 21 days to the loandate to accomplish this. please help....thanks

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    Im not sure how to add 21 days
    Use the Calendar class's add() method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    I tried it earlier in the construtor but it did not work..ok i ll try it again..have to submit the project on friday...thanx

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    but it did not work
    When you have code that 'does not work', you should post it and explain what it does and ask about how to have it do what you want it to do.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    When you have code that 'does not work', you should post it and explain what it does and ask about how to have it do what you want it to do.
    ok I ll take note..

    --- Update ---

    Quote Originally Posted by clarky2006 View Post
    Hia...Still having problems with my codes, nearly done now but the loan class is giving me headache..

    /**
     * This Class would hold all loan details.
     * 
     * @author () 
     * @version 1.0
     */
    public class Loan
    {
        // fields for the Loan Class.
        private int bookId;
        private int memberId;
        private GregorianCalendar loanDate;
        private GregorianCalendar dateDueBack;
        private String month = "";
        private String day = "";
        private String year = "";
        private String currentDate = "";
     
     
        /**
         * Constructor for objects of class Loan
         */
        public Loan(int bookId, int memberId)
        {
            // initialised fields.
            this.bookId = bookId;
            this.memberId = memberId;
            this.loanDate = new GregorianCalendar();
     
            GregorianCalendar gregorianCalendar=new GregorianCalendar();            
            month=String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH));            
            day=String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));
            year=String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR));
            currentDate = (day + " / " + month + " / " + year);
     
     
     
        }

    Here im trying to define date when loan is taken out with gregorian Calendar..when a loan object is created a date stamp should be created to be the loan date at the same time, so i can call the getLoandate an get that time. I want to add 21 days to the loan date and be be able to call it via get dateDueBack. Im not sure how to add 21 days to the loandate to accomplish this. please help....thanks

    public void add(int loanDate, int amount)
        {
            Calendar cal = new GregorianCalendar();
            cal.add(loanDate, + 21);
     
     
        }

    Here is the add method, it says "cannot find symbol class calendar"

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    "cannot find symbol class calendar"
    All java class names start with Uppercase letters. Try using Calendar.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    All java class names start with Uppercase letters. Try using Calendar.
    Calendar is in Capital...

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    You fooled me with this:
    "cannot find symbol class calendar"
    Do you have the import statement for the package the Calendar class is in?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    no but i do have Gregorian calendar imported

    --- Update ---

    Quote Originally Posted by clarky2006 View Post
    no but i do have Gregorian calendar imported
    wow..just imported calendar and its compiled...i see if it works..you are a star...cheers

    --- Update ---

    at Loan.getDateDueBack(Loan.java:85)
    at Loan.toString(Loan.java:95)
    java.lang.NullPointerException
    at Loan.setDateDueBack(Loan.java:75)

    this is what i get with the add method

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    You left off the first lines of the error message. You need to post the whole message.
    In the middle of the message is a NullPointerException at line 75.
    What variable on line 75 is null? Look at line 75, find the null variable and then backtrack in the code to find out why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    ok i will

    --- Update ---

         /**
         * Get the date of the loan.
         * @return
         */
        public GregorianCalendar getLoandDate()
        {
            return (GregorianCalendar) loanDate.clone();
        }

    Here is the code on line 75

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    What variable had the null value? What was the value of loanDate? Print it out with a println if you don't know what its value was.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    The Loan date isjava.util.GregorianCalendar[time=1355021169274,areFieldsSet=true,areAllFieldsS et=true,lenient=true,zone=sun.util.calendar.ZoneIn fo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=tr ue,transitions=242,lastRule=java.util.SimpleTimeZo ne[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=tru e,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode =2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,Y EAR=2012,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=1, DAY_OF_MONTH=9,DAY_OF_YEAR=344,DAY_OF_WEEK=1,DAY_O F_WEEK_IN_MONTH=2,AM_PM=0,HOUR=2,HOUR_OF_DAY=2,MIN UTE=46,SECOND=9,MILLISECOND=274,ZONE_OFFSET=0,DST_ OFFSET=0]

    This is what i get when i print the loan Date...I think I need to assign the current date to the loanDate, but i dont know how to do it..i ve declared current date in the field.

    this.loanDate = new GregorianCalendar(); Im trying to assign the current date to the loanDate here but I dont think the code is right....

    --- Update ---

    the value for the loandate date should be when the loan object is created.......

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    If loanDate did not have a null value, then there is something wrong with what you have been posting.
    The text of the truncated/mangled error message you posted said there was a variable with a null value on line 75.
    The variable on that line was loanDate. Did you add a println between lines 74 and 75 to print out the value of the loanDate variable used on line 75?

    Can you Post the full text of the error message that shows everything?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    /**
     * This Class would hold all loan details.
     * 
     * @author () 
     * @version 1.0
     */
    public class Loan
    {
        // fields for the Loan Class.
        private int bookId;
        private int memberId;
        private GregorianCalendar loanDate;
        private GregorianCalendar dateDueBack;
        private String month = "";
        private String day = "";
        private String year = "";
        private String currentDate = "";
     
     
        /**
         * Constructor for objects of class Loan
         */
        public Loan(int bookId, int memberId)
        {
            // initialised fields.
            this.bookId = bookId;
            this.memberId = memberId;
            this.loanDate = new GregorianCalendar();
     
            GregorianCalendar gregorianCalendar=new GregorianCalendar();          
            month=String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH));            
            day=String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));
            year=String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR));
            currentDate = (day + " / " + month + " / " + year);
     
     
     
     
     
     
        }
     
     
        /**
         * Set date when the loan was taken out.
         * 
         * @param date the day of the month 1 to 28, 30 or 31 depending on month
         * @param month 1 (January) to 12 December
         * @param year the year including century e.g. 2010
         */
        public void setLoanDtae(int date, int month, int year)
        {
            loanDate.set(date, month - 1, year);
        }
     
        public void add(int loanDate, int amount)
        {
            Calendar cal = new GregorianCalendar();
            cal.add(loanDate, + 21);
     
     
        } 
     
     
     
     
         /**
         * Get the date of the loan.
         * @return
         */
        public GregorianCalendar getLoandDate()
        {
            System.out.println(loanDate);
            return (GregorianCalendar) loanDate.clone();
     
        } 
     
         /**
         * Get date when loan is due back.
         * @return
         */
        public GregorianCalendar getDateDueBack()
        {
     
            return (GregorianCalendar) dateDueBack.clone();
        }
     
        /**
         * Return date when loan is due back as a String.
         */
        public String toString()
        {
            String s = new String();
            s = memberId + " " + bookId + " Loan ";
            s += getDateDueBack();
            return s;
        } 
     
        /**
         * 
         * 
         */
        public void printLoanDetails()
        {
           System.out.println("Member ID:"  +  memberId);
           System.out.println("Book ID:"  +  bookId);
           System.out.println("The Loan date is" + loanDate);
           GregorianCalendar calendar = new GregorianCalendar();
           int day = calendar.get(Calendar.DATE);
           int month = calendar.get(Calendar.MONTH);
           int year = calendar.get(Calendar.YEAR);
            //
           System.out.println("The Loan date is "
            + calendar.get(Calendar.DATE) + " "
            + (calendar.get(Calendar.MONTH) + 1) + " "
            + calendar.get(Calendar.YEAR));


    Here is the whole code in the Loan Class..That was the message I was getting......

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    Sorry, that was not the full text of an error message. You need to post the full text.


    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    ok thanks for your help....me too im off to bed...I go to sleep dreaming and thinking of codes...its driving me crazy....

  21. #46
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Morning...its a new day and more coding to be done....Today i need to be able to initialise loanDate and give its value to the field..instead of creating a parameter to pass it through..

  22. #47
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    import java.util.GregorianCalendar;
    import java.util.Calendar;
    import java.util.*;
    import java.util.*;
    /**
     * This Class would hold all loan details.
     * 
     * @author () 
     * @version 1.0
     */
    public class Loan
    {
        // fields for the Loan Class.
        private int bookId;// The borrower's unique ID.
        private int memberId;// The book's unique Identification number.
        private GregorianCalendar loanDate;// The date when loan was taken out.
        private GregorianCalendar dateLoanDueBack;// The date when loan is due back.
     
        /**
         * Constructor for objects of class Loan
         */
        public Loan(int bookId, int memberId)
        {
            // initialised fields.
            this.bookId = bookId;
            this.memberId = memberId;
            loanDate = new GregorianCalendar();
            dateLoanDueBack = new GregorianCalendar();
            dateLoanDueBack.add(dateLoanDueBack.Date, 21);
     
        }
     
        public Loan()
        {
     
     
     
        }   
     
        /**
         * Set date when the loan was taken out.
         * 
         * @param date the day of the month 1 to 28, 30 or 31 depending on month
         * @param month 1 (January) to 12 December
         * @param year the year including century e.g. 2010
         */
        public void setLoanDtae(int date, int month, int year)
        {
            loanDate.set(date, month - 1, year);
        }
     
         public String getDateLoanDueBack()
         {
            SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy");   
               return df.format(dateLoanDueBack.getTime()); 
         }
     
         /**
         * Get the date of the loan.
         * @return
         */
        public GregorianCalendar getLoanDate()
        {
           SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy");
            return df.format(loanDate.getTime());
     
        }
     
             /**
         * Get date when loan is due back.
         * @return
         */
        public GregorianCalendar getDateDueBack()
        {
     
            return (GregorianCalendar) dateDueBack.clone();
        }
     
        /**
         * Return date when loan is due back as a String.
         */
        public String toString()
        {
            String s = new String();
            s = memberId + " " + bookId + " Loan ";
            s += getDateDueBack();
            return s;
        } 
     
        /**
         * 
         * 
         */
        public void printLoanDetails()
        {
           System.out.println("Member ID:"  +  memberId);
           System.out.println("Book ID:"  +  bookId);
           System.out.println("The Loan date is" + loanDate);
           System.out.println("The due back date for this loan is:" + dateLoanDueBack);
        } 
    }

    Hi mate I have re-written the loan class but im getting an error message "cannot find Symbol - variable Date" help please.........

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    "cannot find Symbol - variable Date"
    If that message is from the compiler, (it looks different from normal compiler error messages)
    then the compiler can not find a definition for the variable: Date.
    BTW conventions say variable names should start with lowercase names. Using an uppercase first letter is for class names.

    What line in the code is getting the error message? That is shown on the normal compiler error message.
    Where is the variable: Date defined?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #49
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    If that message is from the compiler, (it looks different from normal compiler error messages)
    then the compiler can not find a definition for the variable: Date.
    BTW conventions say variable names should start with lowercase names. Using an uppercase first letter is for class names.

    What line in the code is getting the error message? That is shown on the normal compiler error message.
    Where is the variable: Date defined?

    /**
    * Constructor for objects of class Loan
    */
    public Loan(int bookId, int memberId)
    {
    // initialised fields.
    this.bookId = bookId;
    this.memberId = memberId;
    loanDate = new GregorianCalendar();
    dateLoanDueBack = new GregorianCalendar();
    dateLoanDueBack.add(dateLoanDueBack.Date, 21);

    }

    dateLoanDueBack.add(dateLoanDueBack.Date, 21); Its complaining about the date here(Date) Im trying to use the add method to add 21 days here for the due back date during initialisation.....

  25. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating Random unused ID number for every library member.

    dateLoanDueBack.Date
    What is that value supposed to be?
    Does the class for the object referred to by: dateLoanDueBack (GregorianCalendar) define a variable: Date? If not, then the code makes no sense.

    What value should the first arg to the add() method be?
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. creating a shared library
    By navinbecse in forum Threads
    Replies: 1
    Last Post: April 9th, 2010, 07:54 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM