That kind of code is hard to write. I'd use the Calendar class's add() method because it will have been tested and will work correctly.
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.
That kind of code is hard to write. I'd use the Calendar class's add() method because it will have been tested and will work correctly.
If you don't understand my answer, don't ignore it, ask a question.
This is a second constructor, it wont compile so i had to comment out sum of the code..
/** * Constructor for objects of class Loan */ public Loan(int bookId, int memberId) { 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 (loanDate); 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 ("new date - " + date); 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); this.bookId = bookId;// The book ID is initialised here. this.memberId = memberId;// The member's unique ID is initialised here. //loanDate = new GregorianCalendar();// When the loan item was taken out. //loanDate = new GregorianCalendar();// End of loan date. //loanReturnDate.add(Calendar.DATE, 21);// This adds three weeks(21 days) to the loanDate. //System.out.println (dateLoanDueBack); }
Sorry, I don't understand your last post.
You should remove your code that adds 21 and use the add() method.
If you don't understand my answer, don't ignore it, ask a question.
Look at how you use the get() method and the set() method.know how to use the add method..
I can't see the highlighting. Can you compile the code with javac and get the error message?highlighting new GregorianCalendar as an incompatible type..
If you don't understand my answer, don't ignore it, ask a question.
Glad you got it working.
If you don't understand my answer, don't ignore it, ask a question.
Morning Norm..Not sure wot time it is across the pond..Hey got more issues with java methods
Have a look at this code please. I created this for a car dealership. here im trying to remove a car by
registration which is a "string". im not sure about using the "==" comparison. should i use ".contains" instead..?
kind regards
public void removeCarByRegistration(String carRegistration) { Iterator<Car> it = cars.iterator(); while(it.hasNext()) { Car car = it.next(); if(carRegistration == (car.getRegNumber())) { it.remove(); } } }
If you want to test for equality use the equals() method.
If you don't understand my answer, don't ignore it, ask a question.
Use the equals() method when comparing the contents of objects for equality.not sure about using the "==" comparison
If you don't understand my answer, don't ignore it, ask a question.
Here is the alternative method im thinkinking of using but not sure...
public void removeCarByRegistration(String searchString) { Iterator<Car> it = cars.iterator(); while(it.hasNext()) { Car car = it.next(); if(car.getRegNumber().contains (searchString)) { it.remove(); } } }
Depends on the programs requirements.
The String "ASDF" contains the String "AS"
If you don't understand my answer, don't ignore it, ask a question.
They want to be able to remove a car that is sold from the Stock Class by just entering the registration number...
Does the logic work?
"AS" is contained in "ASDF"
"AS" does not equal "ASDF"
If you don't understand my answer, don't ignore it, ask a question.
I cant really understand that last reply..What is "AS" and "ASDF"? as you know I'm new to java...
--- Update ---
import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * Write a description of class Stock here. * * @author (your name) * @version (a version number or a date) */ public class Stock { // An Arraylist for storing car details. private ArrayList<Car> cars; private int carId; /** * Constructor for objects of class Stock */ public Stock() { cars = new ArrayList<Car>(); carId = 100; } /** * Add a new car to the Stock. * @param The Car to be added. */ public void addCar(Car car) { cars.add(car); } /** * */ public void addNewCar(String make, String model, String regNumber, double pricePaid) { carId++;// The car Id gets incremented by one in sequence when a new object is created. cars.add(new Car(make, model, regNumber, carId, pricePaid)); } public void printCarByRegistration(String searchstring) { for(Car car : cars) { if(car.getRegNumber().contains (searchstring)) { car.printDetails(); } } } public void printCarBymake(String carMake) { for(Car car : cars) { if(car.getMake() == carMake) { car.printDetails(); } } } public void printCarByModel(String carModel) { for(Car car : cars) { if(car.getModel() == carModel) { car.printDetails(); } } } public void removeCarByRegistration(String searchString) { Iterator<Car> it = cars.iterator(); while(it.hasNext()) { Car car = it.next(); if(car.getRegNumber().contains (searchString)) { it.remove(); } } } }
Here is the whole code for the Stock Class...Not added the main yet...
"AS" and "ASDF" are two Strings I used to show the difference between contains and equals.
That comparison has nothing to do with java. It was an attempt to discuss the logic of what it means for one String to contain another and for one String to be equal to another.
The String "ASDF" contains these Strings: "AS" and "SD" and "DF" and "A" and "ASD" etc
If the String returned by getRegNumber() was "ASDF"
and searchString was "AS" or "SD" etc then contains would be true.
If you don't understand my answer, don't ignore it, ask a question.
ok..I will use the equals() method then so they have to enter the full registration for the right car to be removed instead of searchstring as searchstring would throw up the wrong cars...Thank you for the lecture this morning..You ve just taught me some proper logic...I love been educated by you..Thanks for sharing your knowledge...I would never get those if logics wrong again..Thank you once again..cheers.
Cheers
If you don't understand my answer, don't ignore it, ask a question.
Hi Norm..Just seen my tutor and hes saying that my comparison is wrong on the print carByRegistration method.
He says I should use the compareto method instead. Never used compare to before.
can you help please.
regards
/** * Print a car's details by its registration * @param The registration of the car to be printed. */ public void printCarByRegistration(String registration) { for(Car car : cars) { if(car.getRegNumber()== (registration)) { car.printDetails(); } } }
Have you read the API doc for the compareTo method to see what it does?
Also I suggest that you write a small testing program that uses the compareTo() method with some short Strings and prints out what is returned by the method so you can see what it does.
and change the letters for different tests.System.out.println("D".compareTo("C"));
If you don't understand my answer, don't ignore it, ask a question.