user3752579
user3752579

Reputation: 11

Passing scanner to method

Can anybody tell me why the date prints 0/0/0?
What part of the code is missing so the values input using Scanner are passed into the method DisplayDate and printed on the screen?

import java.util.Scanner;

public class DateClass {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter day:");
    int day = scanner.nextInt();
    System.out.println("Enter month:");
    int month = scanner.nextInt();
    System.out.println("Enter year:");
    int year = scanner.nextInt();

    Date d = new Date(day,month,year);
    d.DisplayDate();

}

}

class Date {

private int day;
private int month;
private int year;


public Date(int day, int month, int year) {
}

public int getDay() {
    return day;
}

public void setDay(int day) {
    this.day = day;
}

public int getMonth() {
    return month;
}

public void setMonth(int month) {
    this.month = month;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}


public void DisplayDate() {
    System.out.println(getDay() + "/" + getMonth() + "/" + getYear());
}

}  

Upvotes: 0

Views: 190

Answers (6)

Jon Skeet
Jon Skeet

Reputation: 1504121

This is the problem:

public Date(int day, int month, int year) {
}

Your constructor completely ignores its parameters, leaving your fields with their default values. For int fields, that's 0.

Instead, it should save the values, e.g.

public Date(int day, int month, int year) {
    this.day = day;
    this.month = month;
    this.year = year;
}

or perhaps call your properties:

public Date(int day, int month, int year) {
    setDay(day);
    setMonth(month);
    setYear(year);
}

I'd also strongly recommend that:

  1. You make the type immutable (remove the setters, make the class final, make the fields final)
  2. You try to think "year month day" rather than "day month year" (so reorder the parameters)
  3. You validate the parameters
  4. You follow Java naming conventions (change DisplayDate to displayDate, or separate the concern of formatting from writing to the console, by just overriding toString instead)
  5. You avoid creating your own date type entirely, and instead use the types in java.time (if you're on Java 8) or Joda Time otherwise.

Upvotes: 6

cbg
cbg

Reputation: 68

The constructor of your Date class is empty, so the values are not set when you create a new Date object.

Upvotes: 0

sternze
sternze

Reputation: 1644

Because your Constructor of the Date class is empty!

Change it to the following and it will work:

public Date(int day, int month, int year) {
    this.day = day;
    this.month = month;
    this.year = year;
}

Upvotes: 0

mhafellner
mhafellner

Reputation: 468

Edit your contructor to:

public Date(int day, int month, int year) {
    this.day=day;
    this.month=month;
    this.year=year;
}

The problem is that you never set the variables you are requesting later!

Upvotes: 1

Hunter McMillen
Hunter McMillen

Reputation: 61550

Your constructor does nothing:

public Date(int day, int month, int year) {
}

it accepts the appropriate values, but doesn't do anything with them. Notice how DisplayDate calls your getter methods: getDay(), getMonth(), and getYear()? The values returned by those getters are the instance variables defined at the top of your class: day, month, and year respectively. Since these values were never initialized with the proper values, they default to 0.

A more sensible constructor would be:

public Date(int day, int month, int year) {
   this.day   = day; 
   this.month = month;
   this.year  = year;
}

Upvotes: 2

BackSlash
BackSlash

Reputation: 22243

You are not saving your day, month and year. You should do it in the constructor:

public Date(int day, int month, int year) {
    this.day = day;
    this.month = month;
    this.year = year;
}

Upvotes: 6

Related Questions