| | |
having some trouble with my project
![]() |
•
•
Join Date: Oct 2007
Posts: 1
Reputation:
Solved Threads: 0
ok the goal of the project is to define a class called DateCS212 and use an array of DateCS212 objects to store a sequence of dates. The main program, Project2.java, will read the dates from a file and will instantiate a DateCS212 object for each date read from a line of the file and store that object into an array of DateCS212 objects. A slight modification is needed for sorting done in a separate class and sort the array of DateCS212 objects in ascending order by date. The program will take a single command-line argument which will be the name of a data file containing lines that are supposed to be in either MM/DD/YYYY format or MM-DD-YYYY format. The program should display its output using two text areas (JTextArea) on a JFrame.
Here is the code for Project2
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.text.*;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* CS212 Project2
* This program will read the dates from a file
* It will then instantiate a DateCS212 object for each date and store that object into an array
* A change will be made to methods for sorting done in a separate class
* The array of DateCS212 objects in ascending order by date
* The program will take a single command-line argument which will be the name of a data file
* This date file will be containing lines that are supposed to be in either MM/DD/YYYY format or MM-DD-YYYY format.
*
* @
*/
public class Project2 {
/**
* @param args
*/
//static String filename="";
static TextFileInput inFile;
static JFrame myFrame;
static JTextArea textAreaOriginal;
static JTextArea textAreaSorted;
static JTextField messageField;
public Project2()
{
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
myFrame=new JFrame();
myFrame.setSize(450, 300);
myFrame.setLocation(100, 100);
myFrame.setTitle("Read file of Dates");
Container contentPane = myFrame.getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
contentPane.add(panel, BorderLayout.CENTER);
textAreaOriginal = new JTextArea();
textAreaOriginal.setEditable(false);
panel.add(new JScrollPane(textAreaOriginal));
textAreaSorted = new JTextArea();
textAreaSorted.setEditable(false);
panel.add(new JScrollPane(textAreaSorted));
messageField = new JTextField();
messageField.setEditable(false);
contentPane.add(messageField, BorderLayout.SOUTH);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
int arrcount = 0;
String filename = args[0];
DateCS212[] dateList = new DateCS212[1000] ;
arrcount = readFile(filename, dateList);
displayResults(textAreaOriginal,
dateList,
arrcount);
}
public static void displayResults(JTextArea area,
DateCS212[] dates,
int length)
{
for (int i=0;i<length;i++) {
area.append(dates[i].toString()+"\n");
}
}
private static int readFile(String filename, DateCS212[] dates)
{
try
{
String lineDate = "";
inFile = new TextFileInput(filename);
int linecount = 0;
//int month1 = 0;
//int day1 = 0;
//int year1 = 0;
//for (int i=0;i<100;i++) {
//dates[i] = new DateCS212(1,1,07);
//}
lineDate = inFile.readLine();
while (lineDate != null) {
if (dates[linecount].isValidDate(lineDate))
{
dates[linecount]= lineDate;
//dates[linecount].setMonth(Integer.parseInt(lineDate.substring(0,2))) ;
//dates[linecount].setDay(Integer.parseInt(lineDate.substring(3,5)));
//dates[linecount].setYear(Integer.parseInt(lineDate.substring(6)));
linecount++;
}
lineDate = inFile.readLine();
} //while
return linecount;
}
catch(Exception anyException)
{
return 0;
}
finally
{
inFile.close();
}
}
}
Here is the code for DateCS212
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateCS212
{
static int month;
static int day;
static int year;
public DateCS212(int month1,
int day1,
int year1)
{
month = month1;
day = day1;
year = year1;
}
public void setDate(String date1)
{
month = Integer.parseInt(date1.substring(0,2));
day = Integer.parseInt(date1.substring(3,5));
year = Integer.parseInt(date1.substring(6));
}
public void setMonth(int month1)
{
month = month1;
}
public void setDay(int day1)
{
day = day1;
}
public void setYear(int year1)
{
year = year1;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
public int compareTo(DateCS212 other)
{
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
int month2 = other.month;
int day2 = other.day;
int year2 = other.year;
// I invoke an instance of Calendar in order to get a Date object
// (Remark : the first month of the year = 0)
Calendar cal2 = Calendar.getInstance();
cal2.set(year2, month2 - 1, day2);
if (cal.before(cal2))
return -1;
else if (cal.equals(cal2))
return 0;
else
return 1;
}
public boolean equals(Object other)
{
return ( other != null
&& getClass() == other.getClass()
);
} // method equals
//&& month == ((int) other).month
//&& day == ((int) other).day
//&& year == ((int) other).year
public String toString()
{
String datestr = String.valueOf(month) + "\\" + String.valueOf(day) + "\\" + String.valueOf(year);
return datestr;
}
public static boolean isValidDate(String text)
{
int test = 0;
String format = "MM/dd/yyyy";
SimpleDateFormat df = new SimpleDateFormat(format);
String format2 = "MM-dd-yyyy";
SimpleDateFormat df2 = new SimpleDateFormat(format2);
Date result = null;
df.setLenient(false);
df2.setLenient(false);
try {
result = df.parse(text);
test = 1;
} catch (Throwable t) {
test = 0;
}
try {
if (test == 0)
{
result = df2.parse(text);
test = 1;
}
} catch (Throwable t) {
test = 0;
}
if (test == 1)
return true;
return false;
}
private static boolean isValidDate(int month, int day, int year)
{
if (month>12 || month<1) {
return false;
}
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day > 31 || day < 1)) {
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30 || day < 1)) {
return false;
}
if (month == 2) {
if (day < 1) {
return false;
}
if (isLeapYear(year) == true) {
if (day > 29) {
return false;
}
}
else {
if (day > 28) {
return false;
}
}
}
return true;
}
private static boolean isLeapYear(int year)
{
if (year % 100 == 0) {
if (year % 400 == 0) { return true; }
}
else {
if ((year % 4) == 0) { return true; }
}
return false;
}
}
im not sure what to put in the code for class DateSorting, but anyway when i run this a blank jframe screen pops up, im not sure where to put the two text files containing the dates one with valid dates and one with invalid dates either, please let me know what i can do to make this program run properly
Here is the code for Project2
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.text.*;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* CS212 Project2
* This program will read the dates from a file
* It will then instantiate a DateCS212 object for each date and store that object into an array
* A change will be made to methods for sorting done in a separate class
* The array of DateCS212 objects in ascending order by date
* The program will take a single command-line argument which will be the name of a data file
* This date file will be containing lines that are supposed to be in either MM/DD/YYYY format or MM-DD-YYYY format.
*
* @
*/
public class Project2 {
/**
* @param args
*/
//static String filename="";
static TextFileInput inFile;
static JFrame myFrame;
static JTextArea textAreaOriginal;
static JTextArea textAreaSorted;
static JTextField messageField;
public Project2()
{
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
myFrame=new JFrame();
myFrame.setSize(450, 300);
myFrame.setLocation(100, 100);
myFrame.setTitle("Read file of Dates");
Container contentPane = myFrame.getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
contentPane.add(panel, BorderLayout.CENTER);
textAreaOriginal = new JTextArea();
textAreaOriginal.setEditable(false);
panel.add(new JScrollPane(textAreaOriginal));
textAreaSorted = new JTextArea();
textAreaSorted.setEditable(false);
panel.add(new JScrollPane(textAreaSorted));
messageField = new JTextField();
messageField.setEditable(false);
contentPane.add(messageField, BorderLayout.SOUTH);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
int arrcount = 0;
String filename = args[0];
DateCS212[] dateList = new DateCS212[1000] ;
arrcount = readFile(filename, dateList);
displayResults(textAreaOriginal,
dateList,
arrcount);
}
public static void displayResults(JTextArea area,
DateCS212[] dates,
int length)
{
for (int i=0;i<length;i++) {
area.append(dates[i].toString()+"\n");
}
}
private static int readFile(String filename, DateCS212[] dates)
{
try
{
String lineDate = "";
inFile = new TextFileInput(filename);
int linecount = 0;
//int month1 = 0;
//int day1 = 0;
//int year1 = 0;
//for (int i=0;i<100;i++) {
//dates[i] = new DateCS212(1,1,07);
//}
lineDate = inFile.readLine();
while (lineDate != null) {
if (dates[linecount].isValidDate(lineDate))
{
dates[linecount]= lineDate;
//dates[linecount].setMonth(Integer.parseInt(lineDate.substring(0,2))) ;
//dates[linecount].setDay(Integer.parseInt(lineDate.substring(3,5)));
//dates[linecount].setYear(Integer.parseInt(lineDate.substring(6)));
linecount++;
}
lineDate = inFile.readLine();
} //while
return linecount;
}
catch(Exception anyException)
{
return 0;
}
finally
{
inFile.close();
}
}
}
Here is the code for DateCS212
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateCS212
{
static int month;
static int day;
static int year;
public DateCS212(int month1,
int day1,
int year1)
{
month = month1;
day = day1;
year = year1;
}
public void setDate(String date1)
{
month = Integer.parseInt(date1.substring(0,2));
day = Integer.parseInt(date1.substring(3,5));
year = Integer.parseInt(date1.substring(6));
}
public void setMonth(int month1)
{
month = month1;
}
public void setDay(int day1)
{
day = day1;
}
public void setYear(int year1)
{
year = year1;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
public int compareTo(DateCS212 other)
{
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
int month2 = other.month;
int day2 = other.day;
int year2 = other.year;
// I invoke an instance of Calendar in order to get a Date object
// (Remark : the first month of the year = 0)
Calendar cal2 = Calendar.getInstance();
cal2.set(year2, month2 - 1, day2);
if (cal.before(cal2))
return -1;
else if (cal.equals(cal2))
return 0;
else
return 1;
}
public boolean equals(Object other)
{
return ( other != null
&& getClass() == other.getClass()
);
} // method equals
//&& month == ((int) other).month
//&& day == ((int) other).day
//&& year == ((int) other).year
public String toString()
{
String datestr = String.valueOf(month) + "\\" + String.valueOf(day) + "\\" + String.valueOf(year);
return datestr;
}
public static boolean isValidDate(String text)
{
int test = 0;
String format = "MM/dd/yyyy";
SimpleDateFormat df = new SimpleDateFormat(format);
String format2 = "MM-dd-yyyy";
SimpleDateFormat df2 = new SimpleDateFormat(format2);
Date result = null;
df.setLenient(false);
df2.setLenient(false);
try {
result = df.parse(text);
test = 1;
} catch (Throwable t) {
test = 0;
}
try {
if (test == 0)
{
result = df2.parse(text);
test = 1;
}
} catch (Throwable t) {
test = 0;
}
if (test == 1)
return true;
return false;
}
private static boolean isValidDate(int month, int day, int year)
{
if (month>12 || month<1) {
return false;
}
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day > 31 || day < 1)) {
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30 || day < 1)) {
return false;
}
if (month == 2) {
if (day < 1) {
return false;
}
if (isLeapYear(year) == true) {
if (day > 29) {
return false;
}
}
else {
if (day > 28) {
return false;
}
}
}
return true;
}
private static boolean isLeapYear(int year)
{
if (year % 100 == 0) {
if (year % 400 == 0) { return true; }
}
else {
if ((year % 4) == 0) { return true; }
}
return false;
}
}
im not sure what to put in the code for class DateSorting, but anyway when i run this a blank jframe screen pops up, im not sure where to put the two text files containing the dates one with valid dates and one with invalid dates either, please let me know what i can do to make this program run properly
Last edited by Demonic5; Oct 24th, 2007 at 4:57 pm.
Tackle the problem in stages.
If needs be create a separate program for just the dates. Another separate program for the text boxes. Then when you know both are doing their job combine them together.
@Claudio use a beautifer honey
If needs be create a separate program for just the dates. Another separate program for the text boxes. Then when you know both are doing their job combine them together.
@Claudio use a beautifer honey
Java Syntax (Toggle Plain Text)
import java.awt.Container; import java.awt.GridLayout; import java.awt.TextArea; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.*; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.text.*; import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class Project2 { static TextFileInput inFile; static JFrame myFrame; static JTextArea textAreaOriginal; static JTextArea textAreaSorted; static JTextField messageField; public Project2() { } public static void main ( String[] args ) { myFrame = new JFrame(); myFrame.setSize ( 450, 300 ); myFrame.setLocation ( 100, 100 ); myFrame.setTitle ( "Read file of Dates" ); Container contentPane = myFrame.getContentPane(); JPanel panel = new JPanel(); panel.setLayout ( new GridLayout ( 1, 2 ) ); contentPane.add ( panel, BorderLayout.CENTER ); textAreaOriginal = new JTextArea(); textAreaOriginal.setEditable ( false ); panel.add ( new JScrollPane ( textAreaOriginal ) ); textAreaSorted = new JTextArea(); textAreaSorted.setEditable ( false ); panel.add ( new JScrollPane ( textAreaSorted ) ); messageField = new JTextField(); messageField.setEditable ( false ); contentPane.add ( messageField, BorderLayout.SOUTH ); myFrame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); myFrame.setVisible ( true ); int arrcount = 0; String filename = args[0]; DateCS212[] dateList = new DateCS212[1000] ; arrcount = readFile ( filename, dateList ); displayResults ( textAreaOriginal, dateList, arrcount ); } public static void displayResults ( JTextArea area,DateCS212[] dates,int length ) { for ( int i = 0; i < length; i++ ) { area.append ( dates[i].toString() + "\n" ); } } private static int readFile ( String filename, DateCS212[] dates ) { try { String lineDate = ""; inFile = new TextFileInput ( filename ); int linecount = 0; lineDate = inFile.readLine(); while ( lineDate != null ) { if ( dates[linecount].isValidDate ( lineDate ) ) { dates[linecount] = lineDate; linecount++; } lineDate = inFile.readLine(); } return linecount; } catch ( Exception anyException ) { return 0; } finally { inFile.close(); } } }
Java Syntax (Toggle Plain Text)
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateCS212 { static int month; static int day; static int year; public DateCS212 ( int month1, int day1, int year1 ) { month = month1; day = day1; year = year1; } public void setDate ( String date1 ) { month = Integer.parseInt ( date1.substring ( 0, 2 ) ); day = Integer.parseInt ( date1.substring ( 3, 5 ) ); year = Integer.parseInt ( date1.substring ( 6 ) ); } public void setMonth ( int month1 ) { month = month1; } public void setDay ( int day1 ) { day = day1; } public void setYear ( int year1 ) { year = year1; } public int getMonth() { return month; } public int getDay() { return day; } public int getYear() { return year; } public int compareTo ( DateCS212 other ) { Calendar cal = Calendar.getInstance(); cal.set ( year, month - 1, day ); int month2 = other.month; int day2 = other.day; int year2 = other.year; Calendar cal2 = Calendar.getInstance(); cal2.set ( year2, month2 - 1, day2 ); if ( cal.before ( cal2 ) ) return -1; else if ( cal.equals ( cal2 ) ) return 0; else return 1; } public boolean equals ( Object other ) { return ( other != null && getClass() == other.getClass() ); } public String toString() { String datestr = String.valueOf ( month ) + "\\" + String.valueOf ( day ) + "\\" + String.valueOf ( year ); return datestr; } public static boolean isValidDate ( String text ) { int test = 0; String format = "MM/dd/yyyy"; SimpleDateFormat df = new SimpleDateFormat ( format ); String format2 = "MM-dd-yyyy"; SimpleDateFormat df2 = new SimpleDateFormat ( format2 ); Date result = null; df.setLenient ( false ); df2.setLenient ( false ); try { result = df.parse ( text ); test = 1; } catch ( Throwable t ) { test = 0; } try { if ( test == 0 ) { result = df2.parse ( text ); test = 1; } } catch ( Throwable t ) { test = 0; } if ( test == 1 ) return true; return false; } private static boolean isValidDate ( int month, int day, int year ) { if ( month > 12 || month < 1 ) { return false; } if ( ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) && ( day > 31 || day < 1 ) ) { return false; } if ( ( month == 4 || month == 6 || month == 9 || month == 11 ) && ( day > 30 || day < 1 ) ) { return false; } if ( month == 2 ) { if ( day < 1 ) { return false; } if ( isLeapYear ( year ) == true ) { if ( day > 29 ) { return false; } } else { if ( day > 28 ) { return false; } } } return true; } private static boolean isLeapYear ( int year ) { if ( year % 100 == 0 ) { if ( year % 400 == 0 ) { return true; } } else { if ( ( year % 4 ) == 0 ) { return true; } } return false; } }
Last edited by iamthwee; Oct 24th, 2007 at 6:59 pm.
*Voted best profile in the world*
![]() |
Similar Threads
- please!!!!!!c++ projects for 12th (C++)
- searching and sorting an access db in vb6 (Visual Basic 4 / 5 / 6)
- Project Ideas Please? (PHP)
- Project to design a internet security plan (Network Security)
- Setting up server as a router (Networking Hardware Configuration)
- Python Graphics Help (Python)
- I just need help starting this program please (C++)
- C++ structures (C++)
- Operating Systems assignment (C++)
- Group Project Ideas (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: how to upload an audio file
- Next Thread: J2me
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan loop machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project radio recursion reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






