File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes DateRec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DateRec" Watch "DateRec" New topic


Liz Lara

Joined: Feb 16, 2008
Posts: 2
Hi I am new to this site I have a problem in my DateTest program and it is with the output I know it has to do with the constructors any help would be appreciated I am very new at this please be specfic

import java.util.*;

public class DateRec
private int month,day,year;
boolean good;
private static final int[] daysnMonth ={31,28,31,30,31,30,31,31,30,31,30,31};

DateRec ()
month = 1;
day = 1;
year = 2008;

DateRec (int m,int d,int y)
month = m;
day = d;
year = y;
good = validate();

public boolean validate ()
if (month < 0 && month < 13 && day > 0 && day <= daysnMonth[month] && year >1900 && year < 2009)
return good;
return false;

public String myToString()
String m = Integer.toString(month); //there must be a
String d = Integer.toString(day); //simpler format
String y = Integer.toString(year);

String s = m + "/" + d + "/" + y;
return s;


import java.util. *;

public class DateTest
public static void main(String[] args)
DateRec Datetest = new DateRec();
String s,m,d,y;
int month = 0 ,day = 0,year = 0 ;

Scanner keyboard = new Scanner(;

char ch;
String ans = "y";

while(ans.charAt(0) == 'y' || ans.charAt(0) == 'Y')
System.out.print("Please enter the month: ");
m = keyboard.nextLine();

System.out.print("Please enter the day: ");
d = keyboard.nextLine();

System.out.print("Please enter the year: ");
y = keyboard.nextLine();

s = m + d + y;
for(int i = 0; i < s.length(); i++)
ch = s.charAt(i);
} // end for

System.out.println("Do you want to continue: Enter <Y>es or <N>o: ");
ans = keyboard.nextLine();

DateRec today = new DateRec();
s = today.myToString();

DateRec anyDay = new DateRec(month,day,year);
s = anyDay.myToString();

DateRec noDay = new DateRec(month,day,year);
s = noDay.myToString();
System.out.println("You entered an invalid date:" + s);
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42952
Welcome to JavaRanch.

What is the output you were expecting, and what is actually being shown?
Liz Lara

Joined: Feb 16, 2008
Posts: 2
These are my instructions for my DateRec program
design a program with a main method to test the DateRec class
a) Create a local variable named today, and assign it to a new object of the type DateRec created using the current date for month, day, and year.
b) Create a local variable named anyDay, and assign it to a new object of the type DateRec created with no arguments.
c) Create a local variable named noDay, and assign it to a new object of the type DateRec created with invalid date values.
d) Call toString()method for each DateRec object and display the values returned
I am basically getting all zeros no values returned except for today returns default constructor from DateRec
I agree. Here's the link:
subject: DateRec