import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class CheckIn{

	Date today;

	void getArrivalDate() {

		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

		try {

			System.out.print("Enter Date in dd/mm/yyyy format: ");
			String d = Keyboard.readString();
			today = df.parse(d);
			df.format(today);

		} catch (ParseException e) {

		e.printStackTrace();
		}
	}

	void getDepartureDate(){


		Calendar ca1 = Calendar.getInstance();
		ca1.setTime(today);

    	System.out.print("How many nights do you want to stay? ");
		int nights = Keyboard.readInt();

    	ca1.add(Calendar.DATE, nights);

		System.out.println("Departure Date: " + ca1.get(Calendar.DATE)
                        + "/"
                        + (ca1.get(Calendar.MONTH) + 1)
                        + "/"
                        + ca1.get(Calendar.YEAR)
                        + "\n");
	}

}

I've made these two methods and they are working well. But I'm confused if the method type is good(void).

Recommended Answers

All 7 Replies

Yes void is OK for a method return type. Use it when the method does not return anything.
Do you get any error messages when you compile the code?

The names of your methods are misleading. A method beginning with get should return a value and not be void.

Before you write the code and give the method a name you should decide what the method is supposed to do. For example: Return a value or print a message.

What are your methods supposed to do? When you answer that then you can write the code.

No, I don't get any errors. The first method is supposed to get the Arrival Date and then store it. The second one is supposed to get how many nights you want to stay and then add them to date and display the new date. Thanks

Methods named get... usually return a value. Yours don't return a value so their names are misleading.
Having a method change a class level variable can lead to hidden bugs. Better to return the value and have the caller change the variable with the returned value.

What do you do with the value returned by the format method?

I use the value returned by the format method to add to it in the other method and to display it in a receipt.

df.format(today);

Here is the call to the format method on line 21.
The return value is NOT received and saved.

Can't understand your point :(

You call a method but do NOT use the value it returns. Why bother calling the method if you ignore what it returns?
Why do you call format in this statement?
df.format(today);

Print the value of today before you call it and again after you call it to see if today is changed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.