Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Java programing any ideas????Please help (http://www.programmingforums.org/showthread.php?t=12756)

bargain4uuu Mar 9th, 2007 3:45 PM

Java programing any ideas????Please help
 
Complete the condition in the if statement below so that it will be true when the date in the GregorianCalendar instance myGC1 is not later than the date of the GregorianCalendar instance myGC2:

if ( ) {
...
}



:rolleyes: :rolleyes: :rolleyes: :confused: :confused: :confused:

DaWei Mar 9th, 2007 4:49 PM

:beard: :eek: :beard: :eek: :beard: :rolleyes:

bargain4uuu Mar 9th, 2007 5:03 PM

??? if this is so easy for you could you help me????

bargain4uuu Mar 9th, 2007 5:13 PM

I'm doing this like that

myGC1.compare(myGC2)<=0



Test 1 succeeded (0 marks):

The expected output was produced correctly.


Test 2 succeeded (0 marks):

The expected output was produced correctly.


Test 3 failed (exit code = 1):

Main.java:35: cannot find symbol
symbol : method compare(java.util.Calendar)
location: class java.util.Calendar
return (myGC1.compare(myGC2)<=0 );
^
1 error


Test 4 failed (exit code = 1):

Exception in thread "main" java.lang.NoClassDefFoundError: Main

Arevos Mar 9th, 2007 5:45 PM

Look at the error that you get. It says that it "cannot find symbol" and that this symbol is "method compare". In other words, it can't find a method called "compare" on your Calendar object.

Next look up the Calendar class (from which GregorianCalendar derives), on Sun's online Java documentation. Usually you can just type the full class name (java.util.Calendar) into Google and it'll find the right page for you without having to figure out where Sun's search engine is.

Look through the Calendar documentation. It doesn't have a "compare" method, which explains why your Java compiler couldn't find it. There is, however, a "compareTo" method, and a "before" and "after" method.

See? Simple when you know how ;)

bargain4uuu Mar 9th, 2007 6:40 PM

I still don't know how to do this :confused:

Calendar now = Calendar.getInstance(); :confused:

Arevos Mar 9th, 2007 7:02 PM

You don't explain what you want to do. It's always helpful to explain what you want to do, rather than posting an incorrect line of code and expecting people to know what it is you want to do.

However, I'd guess that you want to create a new Calendar instance. You can't create a new Calendar object, because Calendar is an abstract class. There are several different calendar systems around, and you need to tell Java which one you want. The standard calendar system in use today is the Gregorian Calendar, and that's the one you're told to use. So:
:

Calendar now = new GregorianCalendar();
Note that the type of "now" is a Calendar, but it holds a GregorianCalendar instance. This is possible because Calendar is the parent class of GregorianCalendar. I could also have done:
:

GregorianCalendar now = new GregorianCalendar();
But "Calendar" is shorter to type, and if I use a generic class like Calendar, then potentially my function can work with any number of calendar systems. This is useful behaviour.


All times are GMT -5. The time now is 2:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC