Hi how's everyone doing, please help me with this program, i am doing this from last 4 to 5 hours, it's over my head, please help.

here's the what program suppose to do:

Given an input data file containing dates, one date per line, write a Java application Project.java which
will read lines of text from data file and put them into a partially-filled array of strings, and will then print
the contents of the array to a series of three JOptionPane message dialog boxes with text areas. The
first message box will display a column of the dates in the original order in which they were listed in the
file. The second message box will display a column of the dates sorted in lexicographical order, as
strings. The third message box will display a column of the dates in ascending order by date (earliest
date first).

import javax.swing.*;
public class pro1111
{
public static void main (String [] args)
{
TextFileInput in = new TextFileInput("Dates.txt");
TextFileOutput out = new TextFileOutput("Dates1.txt");
String line = in.readLine();
while ( line != null ) {
StringTokenizer st = new StringTokenizer(line, "/");
int monthNumber = Integer.parseInt(st.nextToken());
int day = Integer.parseInt(st.nextToken());
int year = Integer.parseInt(st.nextToken());
out.println(monthNumberToName(monthNumber)
+ " " + day + ", " + year);
line = in.readLine();
}
}
 
private static compareDate(string date1, string date2)
{
String date1 = "09/27/2004";
String date2 = "05/04/2002";
if (compareDate(date1, date2) < 0)
System.out.println("date1 is earlier than date2.");
return 0;
}
private static void display(short[] numbers,
int lengthFilled)
{
final String lineBreak = System.getProperty("line.separator");
// Create text area for output:
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
// Print numbers to output text area:
for ( int i = 0; i < lengthFilled; i++ )
textArea.append(numbers[i] + lineBreak);
// Display the output text area via a message dialog box:
JOptionPane.showMessageDialog(null, textArea);
}

Recommended Answers

All 10 Replies

You need to seriously inspect your design. The compareDate method is not called anywhere, except from within itself, and then only with two date Strings (a date String is not a Date, just so you know) that are also defined within the method. Were this method ever called from anywhere else, it would lead to an infinite recursion error.

You need to go back to paper and chart out exactly how the program should operate, and then translate that plan of operations to code.

ok here what i was suppose to do

Program should contain the following methods:

public static void main (String args[])
the main method for this program, it should call the
methods below as appropriate.

public static int readFile (String filename, String[] dates)
Reads from the file whose filename is given as a parameter, and fills array of strings
represending dates. Lines of the file that do not have proper date format (MM-DD-YYYY or
MM/DD/YYYY) or which represent invalid dates (such as 13/32/2006) should be rejected by
printing an error message to the console and not including them in the array. The test for validity
should be done by your isValidDate method below. The returned value is the number of dates
successfully put in the array.

public static void sortLines (String[] lines, int length)
Sorts the array of strings lexicographically using selection sort.

public static void sortDates (String[] dates, int length)
Sorts the array in date order using selection sort. In order to sort the date strings in order by
date, you will need to write an additional method called compareDate that will behave similarly
to compareTo method of the String class, except that it will sort by date rather than
lexicographically. To compare dates date, you will need to split the string into parts representing
the month, day, and year.

public static int compareDate(String date1, String date2)
compares two dates. Returns a negative number if date1 precedes date2, 0 if date1 is the
same as date2, or a positive number if date1 succeeds date2. This method is called from
within sortDate, which date strings by date.

public static void displayResults(String[] students, int length)
Prints the contents of the array to a JOptionPane.

public static boolean isValidDate(String date)
Returns true if and only if date has the proper format (MM-DD-YYYY or MM/DD/YYY) and has
appropriate values for the month (1 to 12) and day. Valid days may range from 1 to 31 in the
months of January, March, May, July, August, October, and December, and may range from 1
to 30 for the months of April, June, September, and November. In February, days may range
from 1 to 29 in a leap year, 1 to 28 in other years. Call your isLeapYear method, described
below, to determine whether a year is a leap year.

public static boolean isLeapYear(int year)
Returns true if and only if year is a leap year. A year is a leap year if it is a multiple of 400 or if
it is a multiple of 4 but not a multiple of 100.

You misunderstood what I said. I am not going to do it for you, and posting the assignment text won't help. Take this text and a piece of paper. For each of the methods listed draw a little box on the paper and label it as the method and write a word or two to describe its purpose. Then begin drawing lines between these boxes with some text explaining the relationship to describe how these methods are to work with each other. Then take this information and begin coding your methods (remembering to call the write methods in the write order from the write places) as per your drawn up action/design plan.

look what i understood i wrote code, i know it's wrong, so take my program (you don't need to do it for me), and put pointers, explation what i am doing wrong, what i need to add , and what need to removie. that's what i am asking, so look at the code and help.

/*
 * You should give your class a package as it is truely frowned upon these days
 * to place your classes in the "main" namespace.
 */
 
import javax.swing.*;

public class pro1111 {

  public static void main (String [] args) {
    /*
     *  I assume you have your own TextFileInput and TextFileOutput
     *  classes as these are no part of the JDK.  And if so, you have not
     *  imported them.
     */
    TextFileInput in = new TextFileInput("Dates.txt");
    TextFileOutput out = new TextFileOutput("Dates1.txt");
    String line = in.readLine();  // I also assume these classes handle the exceptions
    while ( line != null ) {
      // StringTokenizer is a bit old fashioned.  You should probably look into
      // Scanner and or String.split().  But what you really want is to simply read
      // the entire String and not break it down into pieces.
      StringTokenizer st = new StringTokenizer(line, "/"); 
      int monthNumber = Integer.parseInt(st.nextToken());
      int day = Integer.parseInt(st.nextToken());
      int year = Integer.parseInt(st.nextToken());
      out.println(monthNumberToName(monthNumber) + " " + day + ", " + year);
      // You should also be storing the dates here rather than simply printing them
      // out.  Probably with an ArrayList.  Then, after you have them read in and
      // stored somewhere, you can start using another loop or two to compare them
      // and get them into the proper order.
      line = in.readLine();
    }
  }
   
  private static compareDate(string date1, string date2) {
    // Why do you designate two dates (in String format and it should be String not
    // string) and then define two new date Strings.  You also have not defined a
    // return type.
    String date1 = "09/27/2004";
    String date2 = "05/04/2002";
    // calling compareDate here is recursion.  Is this really what you want to do?
    // I don't think so.  Also, doing it when the method defines it own date Strings
    // guarntees infinite recursion.
    if (compareDate(date1, date2) < 0)
      System.out.println("date1 is earlier than date2.");
    return 0;
  }
  
  private static void display(short[] numbers, int lengthFilled) {
    // Why are the declared arguments here a short array and an int when according
    // to your assignment it should be a String array (and suppossedly of student
    // names which I don't understand at all since the entire has been concerning
    // dates up until this point.  I think you copied that wrong.) and an int.
    // Other than that, this method actually looks like it might do something for
    // you.  Where did you copy it from?
    final String lineBreak = System.getProperty("line.separator");
    // Create text area for output:
    JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    // Print numbers to output text area:
    for ( int i = 0; i < lengthFilled; i++ )
      textArea.append(numbers[i] + lineBreak);
    // Display the output text area via a message dialog box:
    JOptionPane.showMessageDialog(null, textArea);
  }

  /*
   * Where are all the rest of the listed methods?  Should they generate
   * themselves?  Really, you could at least put a few skeleton methods for
   * them in here so that it at least looks like you tried.  You also have not
   * placed a closing brace "}" for your class.
   */

Find the remarks in the "code" (and I am using that term very generousy) above. Really, even if it is your first day (which it is not with this type of assignment) you could put in a little more effort.

You must throw this code away and start with what I suggested in my last post. Maybe that way you will gain at least a little insight into what it is like to actually be a developer (or at least of the development process).

commented: well said (iamthwee) +5

ok, thank u for the help, i am working on it as soon as i am done, i will post it here

masijade, man you got attuide son ,you know how to write a code and all stuff, remebmer one day you were on my postion. Son there's other community board on the net where i can get help.

It's not me that has attitude. I have told you what you needed to do a few posts ago, and that is to start with paper and try to design a flowchart/schematic for this program, and then start with the coding. But you insisted that I look at your code and place pointers in there, well I did. It is not my fault that the only method that is even anywhere remotely usable is the display method. It is also not my fault that the display method is obviously copied from some other place since all of sudden it is taking about studentNames instead of dates and uses the wrong type of array. If you don't like the code review that you got, then maybe you should put a little effort into writing the code next time. You say you spent four five hours, but seemingly you did not even try to compile those parts of it that you had or many of the problems that were pointed out would have been fixed. You would have imported the classes that needed to be imported, and the method parameters would at least have had the right types. I was simply being honest in my code review. Attempting to either "sugar-coat" the review, or do the work for you (which I am sure was what you actually wanted) would not help you, it would hurt you. The second might have let you pass this assignment, but then you would be even more lost than you are now (as hard as that is to believe). Why don't you ask your questions on geekinterview.com? They seem to have the level of expertise (and intelligence) that you want.

commented: no good +0
commented: N/A +6

masijade: if you an't gonna help stop posting in my thread. don't give me no explation, stop posting in my thread.

commented: Cheater +0

Stop attempting to call me out simply because you don't like the good advice you are getting, and accept it, and I will stop posting in it. It's easy, accept your task, do your work (or at least make an honest attempt at it), and remain civil (especially when you are the one that needs help), and will find that things run much more smoothly, effeciently, and productively.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.