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
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.
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
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.
dateLoanDueBack.Date..........The dateLoanDueBack value should be 3 weeks from when the object is created(21 days)
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.
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...
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.
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.........
Glad you got that one solved.
Java is case sensitive. Spelling it as written in the API doc will work better.I ve capitalised DATE to diffrenciate it from the date method
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.
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)
Please post the full text of the error message that shows where the error happens and the contents of the source statement.gives me error message
If you don't understand my answer, don't ignore it, ask a question.
/** * * */ 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......
What type is loanDate?
If you don't understand my answer, don't ignore it, ask a question.
What type is loanDate?
its gregorian calendar
Did you read the API doc for the format() method? What args does it take?
That is NOT a data type or classname. You should have learned by now that spelling is very important in java programming.gregorian calendar
If you don't understand my answer, don't ignore it, ask a question.
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"
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.
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 ---
public void returnDateLoanIsDueBack(int bookId) { Loan.loanReturnDate(loan); }
now it cannot find symbol.............
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.
clarky2006 (December 12th, 2012)
Hia this is how i ve coded that method but its not compiling..it says that dateReturned; is not a statement...
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.
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); }
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.
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..
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.