• 0

[Java] Date Validation App


Question

Hey there,

Could somebody put this all in one class for me? Every time I try to do this, it doesn't work. I thought perhaps somebody on here would know how to do it.

Also, could you try and eliminate the use of java.io.*?

I just want to import javax.swing.*

Thanks people, help is greatly appreciated.

http://www.csc.liv.ac.uk/~frans/COMP101/we...#dateValidation

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

? public static BufferedReader ? ?keyboardInput = new 
 ? ? ?	BufferedReader(new InputStreamReader(System.in));

Take that out and you can take out java.io.*;

Replace:

date = new Integer(keyboardInput.readLine()).intValue();

With:

date = new Integer(yourtextbox.getText()).intValue();

I did a direct copy and paste and it worked for me.

DateValidation.zip

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

what I meant was, in the one file. So you have the .java called DateValidation, and everything is in that one file.

Sorry if I left that out.

Link to comment
Share on other sites

  • 0

if (kjordan == female)
{
   I lubs you
}
else
{
   Thanks mate.
}

I'll fix this stuff. I got that when trying to execute from JCreator. Compiled fine.

Exception in thread "main" java.lang.Error: Do not use javax.swing.JFrame.setLayout() use javax.swing.JFrame.getContentPane().setLayout() instead

at javax.swing.JFrame.createRootPaneException(JFrame.java:465)

at javax.swing.JFrame.setLayout(JFrame.java:531)

at DateValidation.init(DateValidation.java:26)

at DateValidation.<init>(DateValidation.java:36)

at DateValidation.main(DateValidation.java:42)

Link to comment
Share on other sites

  • 0

Actually... can I get you to something else for me, please? I hate to be an arse, but can you do this?

It just needs to be simple.

Thankyou very very very very very very very very very much.

instructions.txt

Link to comment
Share on other sites

  • 0

String input = new String("06-08-04"); // get the input and change it to string here
int day = input.subString(0, 1);
int month = input.subString(3, 4);
int year = input.subString(6, 7); // you might have to typecast the above three...

if ( (day &lt; 28 &amp;&amp; day &gt; 0) || (day &gt;= 28 &amp;&amp; day &lt;= 30 &amp;&amp; month != 2) ) // February?
  System.out.println(day + stringMonth(month) + ", " + year);  
/* write a method called stringMonth() which basically has a big switch statement to convert an int month into a word (i.e. convert 03 into "March") */
else if (day == 31)
  if (month != 1 &amp;&amp; month != 3 ... // put in all the months with 31 days here
      System.out.println("Invalid date.");
  else
      System.out.println(day + month() + ", " + year);

Its hardly complete. For one thing, it doesnt handle leap years.. And the year will print out as "04" rather than "2004". But that should be easy enough to fix.

Oh, yeah, that's for a console app too. For your JApplet you'll want to replace the System.out.println's with the JApplet printing utility.

Link to comment
Share on other sites

  • 0
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class DateValidation extends Applet implements ActionListener 
{   
	static private final int MIN_MONTH_NUMBER = 1;
	static private final int MAX_MONTH_NUMBER = 12;
	static private final int MIN_DAY_NUMBER = 1;
	TextField input;                                         
	Label check;
	public void guiinit() 
	{
  input = new TextField(10);                   
  Label y = new Label("Input a date in DDMMYYYY format:");
  check = new Label();
  input.addActionListener(this);
  this.setLayout(new GridLayout(3,1));
  this.add(y);
  this.add(input);
  this.add(check);
	} 
	public DateValidation() 
	{
  guiinit();
	}
                           
	public static void main(String argv[]) 
	{
  new DateValidation();
	}
	public void actionPerformed(ActionEvent e) 
	{
  String date = input.getText();
  if ((date.length() == 8) &amp;&amp; (date.substring(2,3).equals("-")) &amp;&amp; (date.substring(5,6).equals("-")) &amp;&amp; (CheckNumber(date.substring(0,2))) &amp;&amp; CheckNumber(date.substring(3,5)) &amp;&amp; CheckNumber(date.substring(6,8)))
  {
 	 int day = Integer.parseInt(date.substring(0, 2));
 	 int month = Integer.parseInt(date.substring(3, 5));
 	 int year = Integer.parseInt(date.substring(6, 8));
 	 if (monthNumberWithinRange(month)) 
 	 {
    if (dayNumberWithinRange(month,day,year))
   	 check.setText("Date format OK");
 	 }
  } 
  else { check.setText("Invalid input"); }
	}
	public boolean CheckNumber(String s) 
	{
  try 
  {
 	 Integer.parseInt(s);
  } 
  catch (NumberFormatException e) 
  {
 	 return false;
  }
  return true;
	}
	public boolean monthNumberWithinRange(int monthNumber) 
	{
  if ((monthNumber &lt; MIN_MONTH_NUMBER) || 
 	 (monthNumber &gt; MAX_MONTH_NUMBER)) 
  {
 	 check.setText("ERROR: " + monthNumber + 
    " is an invalid month number");
 	 return(false);
  }
  else return(true);
	}

	public boolean dayNumberWithinRange(int monthNumber,int dayNumber,int yearNumber) 
	{
  if  ((dayNumber &lt; MIN_DAY_NUMBER) || 
 	 (dayNumber &gt; getNumDaysInMonth(yearNumber,monthNumber))) 
  {
 	 check.setText("ERROR: " + dayNumber + 
    " is an invalid day number");
 	 return(false);
  }
  else return(true);
	}
    
	private int getNumDaysInMonth(int yearNumber,int monthNumber) 
	{
  int numDaysInMonth=0;

  switch(monthNumber) 
  {
 	 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    numDaysInMonth = 31;
    break;
 	 case 4: case 6: case 9: case 11: 
    numDaysInMonth = 30;
    break;
 	 case 2:
    // Test for leap year
    if (((yearNumber%4 == 0) &amp;&amp; (yearNumber%100 != 0)) || 
   	 (yearNumber%400 == 0))
   	 numDaysInMonth = 29;   
    else numDaysInMonth = 28;
    break;
 	 default:
    check.setText("ERROR: Cause unknown");
  }   
  return(numDaysInMonth); 
	}
}

Edited by kjordan2001
Link to comment
Share on other sites

  • 0
Why is everyone doing this guy's homework for him? If he's too lazy or too stupid to figure it out then let him fail!

I was trying to just give him pointers on how to go about doing it, not do the whole thing for him :/

Link to comment
Share on other sites

  • 0
Why is everyone doing this guy's homework for him? If he's too lazy or too stupid to figure it out then let him fail!

I actually didn't know it was homework at first, I thought he was just trying to get something from that site working. Changing from JFrame to Applet isn't that hard, so I just did it. I think we should actually outlaw homework questions unless they have part of it done. Then it's hard to tell whose asking homework questions and who just wants help learning a language :/

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.