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 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 96

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

  1. #51
    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.

    Or can you please help me add 21 days to the loanDate as im struggling to do it..been trying for nearly 2 wks now. At least i would know how to do it next time..Have till Friday to submit this..Its a fail if I do not add the loan class..cheers

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Read the API doc and answer this question:
    What value should the first arg to the add() method be?

    See posts #17 and 29 for examples.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #53
    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.

    dateLoanDueBack.Date..........The dateLoanDueBack value should be 3 weeks from when the object is created(21 days)

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Did you read my last post? You can't code anything that comes into your head. You must follow the rules as defined in the API.

    The variable: Date does NOT exist in the GregorianCalendar class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #55
    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.

    trust me if i understood how to do it, then i would have by now...the API is so hard to understand it causes me more confusion than it solves. i could look at it all day and still not understand it...

  6. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Look at the add() method. What is the first arg to that method?

    You will need to learn to read the API if you want to write any programs.
    Copy the contents of the API doc you don't understand and paste it here with your questions about what it says.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #57
    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.

    Did you read my last post? You can't code anything that comes into your head. You must follow the rules as defined in the API.

    The variable: Date does NOT exist in the GregorianCalendar class.

    Yeeeeeeeeeeeeeeeeeeeeees done it. Its adding 21 days to the loanDate now..Here is it is..I ve capitalised DATE to diffrenciate it from the date method

    /**
         * 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);
     
        }


    --- Update ---

    now i ll try and add time to it.........

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Glad you got that one solved.

    I ve capitalised DATE to diffrenciate it from the date method
    Java is case sensitive. Spelling it as written in the API doc will work better.

    One confusing way you've coded the field arg is using a variable name instead of the name of the class.
    The expected way of coding would be: Calendar.DATE
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    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
    Glad you got that one solved.


    Java is case sensitive. Spelling it as written in the API doc will work better.

    One confusing way you've coded the field arg is using a variable name instead of the name of the class.
    The expected way of coding would be: Calendar.DATE
    You are right, Wish I knew that yesterday..wud have saved me so much time...to do my documentation..

    --- Update ---

    more issues tho...when I print, it prints the book ID and member ID ok but not the loanDate or dateloanDueBack....gives me error message. "java.lang.illegalArgumentException:" cannot format given object as a Date(in java.text.DateFormat)

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    gives me error message
    Please post the full text of the error message that shows where the error happens and the contents of the source statement.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    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.

        /**
         * 
         * 
         */
        public void printLoanDetails()
        {
           System.out.println("Member ID:"  +  memberId);
           System.out.println("Book ID:"  +  bookId);
           DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
           System.out.println("Loan Date: "+dateFormat.format(loanDate));

    This is the print method......Blue j wont let me to highlight and paste the error message so i ve written it as it is word for word..

    --- Update ---

    java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(DateFormat.java:301)
    at java.text.Format.format(Format.java:157)
    at Loan.printLoanDetails(Loan.java:92)
    java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(DateFormat.java:301)
    at java.text.Format.format(Format.java:157)
    at Loan.printLoanDetails(Loan.java:92)
    java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(DateFormat.java:301)
    at java.text.Format.format(Format.java:157)
    at Loan.printLoanDetails(Loan.java:92)

    This is the message from the output window......

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    What type is loanDate?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    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.

    What type is loanDate?

    its gregorian calendar

  14. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Did you read the API doc for the format() method? What args does it take?

    gregorian calendar
    That is NOT a data type or classname. You should have learned by now that spelling is very important in java programming.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #65
    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.

    public String dateBookdueback(int bookID)
        {
          Loan.getLoanDate();
        }

    My library is nearly done now....thanks to you......Got some issues here the above code, trying to call the getLoanDate from the library class but Im getting this error message "non-static method getLoanDate()cannot be referenced from a static context"

  16. #66
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    You can not call a non-static method without using an instance of the class.
    Loan.getLoanDate();

    You need an instance of the Loan class to call the getLoanDate() method. It should be the instance that has the date in it.

    Read the Class Methods section on this page: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  17. #67
    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
    You can not call a non-static method without using an instance of the class.
    Loan.getLoanDate();

    You need an instance of the Loan class to call the getLoanDate() method. It should be the instance that has the date in it.

    Read the Class Methods section on this page: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Just read the API documentation and still not sure how to call this method..Im running out of time to finish this work..I do know that non-static means that its not there. does not exist yet.

    --- Update ---

    Quote Originally Posted by clarky2006 View Post
    Just read the API documentation and still not sure how to call this method..Im running out of time to finish this work..I do know that non-static means that its not there. does not exist yet.
        public void returnDateLoanIsDueBack(int bookId)
        {
     
          Loan.loanReturnDate(loan);   
        }

    now it cannot find symbol.............

  18. #68
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    You need an instance of the Loan class to call the getLoanDate() method. It should be the instance that has the date in it. Something like this:
    Loan refToLoanClass = new Loan();  // get reference to an instance
     
    ....
     
     
     
    SomeDateClass aDateVar = refToLoanClass.getLoanDate();
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    clarky2006 (December 12th, 2012)

  20. #69
    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.

        public String DateLoanIsDueBack(int bookId)
        {
          for(Loan loan : loans)
        {
              String dateReturned = "";  
              if(loan.getBookId() == bookId)
              {
               dateReturned = loan.getLoanReturnDate();
     
              }
        }
         dateReturned; 
        }

    Hia this is how i ve coded that method but its not compiling..it says that dateReturned; is not a statement...

  21. #70
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    The compiler is correct. A statement consisting only of a variable name is not valid.
    What did you want to do with the value in the variable?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #71
    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
    The compiler is correct. A statement consisting only of a variable name is not valid.
    What did you want to do with the value in the variable?
    public String loanDate(int bookID)
        {
          String dateborrowed = "";
          for(Loan loan : loans)
          {
              if(loan.getBookId() == bookId)
              {
                dateborrowed = loan.getLoanDate();
            }
        }
        return dateborrowed;
        }

    managed to re-write it and it works...here it is..

    --- Update ---

    But i do have problems with the return date, it seems to be fixed on 1/1/2013...i borrowed a book yesterday and the return date was 1/1/2013. i borrowed another today and was given the same return date 1/1/2013...its suppose to add 21 days each time but it doesnt.....here is the code.....

    public class Loan
    {
     
        // Declared fields for the Loan Class.
        private int bookId;// The book's unique ID.
        private int memberId;// The member's unique Identification number.
        public String month="";
        public String day="";
        public String year="";
        //private GregorianCalendar loanDate;// The date when loan was taken out.
        //private GregorianCalendar dateLoanDueBack;// The date when loan is due back.
        private String loanDate;
        private String loanReturnDate;
     
     
        /**
         * Default Constructor for objects of class Loan
         */
        public Loan()
        {
         int currentDay;
         int currentMonth;
         int currentYear;
         GregorianCalendar calendar = new GregorianCalendar();
         month=String.valueOf(calendar.get(GregorianCalendar.MONTH));            
         day=String.valueOf(calendar.get(GregorianCalendar.DAY_OF_MONTH));
         year=String.valueOf(calendar.get(GregorianCalendar.YEAR));
         loanDate = (day + " / " + month + " / " + year);
         //System.out.println (date);
         int d = Integer.parseInt(day);
         int m = Integer.parseInt(month);
         int y = Integer.parseInt(year);
         m = m + 1; 
         loanDate = (d + " / " + m + " / " + y);
         System.out.println ("Loan Date: - " + loanDate);
         d = d + 21;
         if (d > 30)
         {
             d= 1;
             m = m + 1;
            }
         if (m > 12)
         {
            m = 1; 
            y = y + 1;
            }
         currentMonth = m;
         currentYear = y;
         currentDay = d;
         loanReturnDate = (currentDay + "/" + currentMonth + "/" + currentYear);
         System.out.println ("Loan Return date - " + loanReturnDate);
        }

  23. #72
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    The posted code does not compile. One problem is missing import statements.

    Another problem is there is no code to test the Loan class. There needs to be a small class with a main() method that calls the Loan class and shows the problem. It needs to compile, execute and show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #73
    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
    The posted code does not compile. One problem is missing import statements.

    Another problem is there is no code to test the Loan class. There needs to be a small class with a main() method that calls the Loan class and shows the problem. It needs to compile, execute and show the problem.
    it does compile in blue J..u dont need main method in blue J..i ve just compiled it again and it says no syntax errors..

    --- Update ---

    import java.util.*;
    import java.text.*;
     
    /**
     * This Class would hold all loan details.
     * 
     * @author () 
     * @version 1.0
     */
    public class Loan
    {
     
        // Declared fields for the Loan Class.
        private int bookId;// The book's unique ID.
        private int memberId;// The member's unique Identification number.
        public String month="";
        public String day="";
        public String year="";
        //private GregorianCalendar loanDate;// The date when loan was taken out.
        //private GregorianCalendar dateLoanDueBack;// The date when loan is due back.
        private String loanDate;
        private String loanReturnDate;
     
     
        /**
         * Default Constructor for objects of class Loan
         */
        public Loan()
        {
         int currentDay;
         int currentMonth;
         int currentYear;
         GregorianCalendar calendar = new GregorianCalendar();
         month=String.valueOf(calendar.get(GregorianCalendar.MONTH));            
         day=String.valueOf(calendar.get(GregorianCalendar.DAY_OF_MONTH));
         year=String.valueOf(calendar.get(GregorianCalendar.YEAR));
         loanDate = (day + " / " + month + " / " + year);
         //System.out.println (date);
         int d = Integer.parseInt(day);
         int m = Integer.parseInt(month);
         int y = Integer.parseInt(year);
         m = m + 1; 
         loanDate = (d + " / " + m + " / " + y);
         System.out.println ("Loan Date: - " + loanDate);
         d = d + 21;
         if (d > 30)
         {
             d= 1;
             m = m + 1;
            }
         if (m > 12)
         {
            m = 1; 
            y = y + 1;
            }
         currentMonth = m;
         currentYear = y;
         currentDay = d;
         loanReturnDate = (currentDay + "/" + currentMonth + "/" + currentYear);
         System.out.println ("Loan Return date - " + loanReturnDate);
        }

    It does have import statememnt...

    --- Update ---

    I ve tried to create a test class but I ve not been able to do so..im not that clever yet with java..

  25. #74
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    There is no main() method in the class so there is no way to execute it for testing.
    There needs to be code that executes and shows the problem for testing.



    Where does the code call the add() method with 21 days?
    If you don't understand my answer, don't ignore it, ask a question.

  26. #75
    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
    There is no main() method in the class so there is no way to execute it for testing.
    There needs to be code that executes and shows the problem for testing.



    Where does the code call the add() method with 21 days?
    d = d + 21;
         if (d > 30)
         {
             d= 1;
             m = m + 1;
         }
         if (m > 12)
         {
            m = 1; 
            y = y + 1;
         }
    this is suppose to add 21 days

Page 3 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