PDA

View Full Version : Help with creating an array to store dates



aophyz
Mar 15th, 2007, 04:45 AM
Hey everyone, I am new here!

Anyways, I am a struggling computer-science college student and I come to you all for some help.

What I am trying to do is to create an array that could hold "dates". (1/3/2004)

I have a file that has 6 sets of 3 dates. So 18 individual dates.
The program reads the file and takes in the dates.

What I have is a while loop. Inside the while loop it reads each set one at a time and passes it to a method which will sort the dates in the chronological order.

I.E.
Reads:
1/3/1992
2/3/1991
1/5/1998

Prints out to the file:
2/3/1991
1/3/1992
1/5/1998

Get what I am saying?

Well what I need help with, is how can I create an array in the main method which the read dates can be stored into and passed into this "orderedDate" method, which is the method I want to sort the dates.

I looked around and I found this setup (algorithm) to follow but am having trouble turning it into actual code:

. It uses three local Date variables Earliest, Middle, and Latest. The purpose of the algorithm below is to make Earliest reference the first of the three dates, Middle reference the next date, and Latest reference the last date. Of course your code will have to replace Earliest with array[0], middle with array[1] and latest with array[2].

You of course would have to replace Earliest in this algorithm with your datesArray[0], Middle with datesArray[1], and Latest with datesArray[2].

(um i dont know why the above line got crossed out when I pasted this)


Your main program will simply print the contents of the array.


IF ALL THE DATES WERE LEGAL THEN
COMPARE DATE1 AND DATE2
STORE EARLIER OF THE TWO IN EARLIEST
STORE OTHER ONE IN LATEST
// now we are looking at Earliest, Latest, and Date3
IF DATE3 IS EARLIER THAN EARLIEST THEN
STORE EARLIEST IN MIDDLE
STORE DATE3 IN EARLIEST // we are done
OTHERWISE // Date3 not earlier than EARLIEST)
IF DATE3 IS EARLIER THAN LATEST THEN
STORE DATE3 IN MIDDLE // Date3 is in middle
OTHERWISE // now you know Date3 is last
STORE LATEST IN MIDDLE
STORE DATE3 IN LATEST


What we know of the order



EARLIEST LATEST DATE3??



DATE3 EARLIEST LATEST

DATE3?? Or DATE3??
EARLIEST | LATEST |


So I am a little lost and would appreciate any help at all.
If I was unclear about anything please let me know.

edit:
I forgot my code: here


public class Datetest3b
{

// If there are static (class) variables or constants, declare here
//************** STATIC VARIABLES **************************************
static final String IN_FILE_NAME = "Dates3b.txt";
static final String OUT_FILE_NAME = "out.txt";

//************* HELPER METHODS *****************************************
// Typically, private methods called from within methods of the class
//**********************************************************************


private static void orderedDate (Date date1, Date date2, Date date3, Date[]theDates)
{

} // end testComparisonMethods

/************* MAIN METHOD *******************************************
* Preconditions: There is an input file (specified by the constant
* IN_FILE_NAME) available on disk. The data file
* contains pairs of dates in the format (mm/dd/yyyy)
* required by the Date class.
* Postconditions: For any valid pairs of dates in the input file, the
* program prints to the output file (specified in the
* constant OUT_FILE_NAME) the dates and the relationship
* (earlier, later, equal) of the dates in each pair of
* dates.
* If either date in a pair is invalid, an error message
* is printed explaining what the error was and no
* comparison is attempted.
* Throws: IOException if the input file is not found or the output file
* cannot be created.
*/
public static void main(String[] args) throws IOException
{
BufferedReader inFileStream =
new BufferedReader(new FileReader(IN_FILE_NAME));
PrintWriter outFileStream =
new PrintWriter(new FileOutputStream(OUT_FILE_NAME));

boolean endOfFile = false,
errors = false;
int dateCount;
Date myDate = new Date(),
yourDate = new Date(),
thirdDate = new Date();

String theDates[] = new String[3];

outFileStream.println("CSC20 - Lab3b output Ash Gholami");
outFileStream.println(" USING DATA FILE: " + IN_FILE_NAME);
outFileStream.println("***************************************");
outFileStream.println();

while (!endOfFile)
{
dateCount = 0;
errors = false;
// Read in three dates. If an error occurs an exception will
// be thrown and caught in the catch block that follows.
try
{
try
{
myDate = Date.readDate(inFileStream);
dateCount++;
}
catch (DateException dateError)
{
// There is an error in the first date
outFileStream.println(dateError);
errors = true;
}
try
{
yourDate = Date.readDate(inFileStream);
dateCount++;
}
catch (DateException dateError)
{
// There is an error in the second date
outFileStream.println(dateError);
errors = true;
}
try
{
thirdDate = Date.readDate(inFileStream);
dateCount++;
}
catch (DateException dateError)
{
// There is an error in the first date
outFileStream.println(dateError);
errors = true;
}
}
catch (EOFException eof)
{
endOfFile = true;
}
catch (IOException e)
{
outFileStream.println(e);
}
if (dateCount == 3)
{
// We have three good dates
orderedDate(myDate, yourDate, thirdDate, []theDates);
}
else if (dateCount == 1 && !errors)
{
outFileStream.println("\nThere are an odd number of dates "
+ "in the file " + IN_FILE_NAME + ".");
outFileStream.println("The last date, "
+ myDate + " was not compared.");
}
} // end while not EOF
System.out.println("You will find your output in the file: "
+ OUT_FILE_NAME);
System.out.println("Thank-you for using this program.\nGood-bye");

inFileStream.close();
outFileStream.close();
} // end main
} // end class Datetest3a
Thanks :)

vwphillips
Mar 15th, 2007, 07:24 AM
Welcome


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
var Ary=['1/3/1992','2/3/1991','1/5/1998'];

function SortDates(zxcary){
zxcary=zxcary.sort(DateSort);
alert(zxcary);
}

function DateSort(zxca,zxcb){
var zxcaa=new Date(zxca.split('/')[2],zxca.split('/')[2],zxca.split('/')[2]);
var zxcbb=new Date(zxcb.split('/')[2],zxcb.split('/')[2],zxcb.split('/')[2]);
if (zxcaa>zxcbb) return 1;
if (zxcaa<zxcbb) return -1;
return 0;
}
//-->
</script></head>

<body onload="SortDates(Ary)">

</body>

</html>

glenngv
Mar 15th, 2007, 06:54 PM
His code is in Java not Javascript. So he posted in the wrong forum.

aophyz
Mar 15th, 2007, 09:11 PM
your right, this is in the wrong forum. i guess i will re-post in the correct forum.

glenngv
Mar 15th, 2007, 09:21 PM
You can ask the mods to move this thread to Java Forum.