0

I'm working on a calender program for school... Task: Create an Applet with three fields (dd,mm,yyyy) and a get birthday button... The Applet will then display(through an algorithm of strings and loops) the correct month, year, and date with the birthday highlighted... And dates must be off set correctly... For example... DEC 14 the 1st must fall underneath the W column... PROGRAM RUNS PERFECT! Except... I can't get the invalid input to return correctly... For example, If I put month 13, I get invalid month input: 13(which is good) but If I put 14+ I get nothing... Same thing with days past 31... Heres what the error check looks like and I have the days and months in an array...

//HW 10         BirthdayApplet

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
//Applet..
public class BirthdayApplet extends Applet implements ActionListener {
//Constants...
private static final int FIRST_DAY = 3;
//Instance Variables...
private int inputDay, inputMonth, inputYear;
private boolean displayCalendar = false; // flag to display calendar
private int firstDayofInputMonth = 0;
//Array of days in int...
private int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 
    31, 30, 31, 30, 31};
//Array of months in string...
private String monthNames[] = {"", "January", "February", "March",
    "April", "May", "June", "July", "August", "September", "October",
    "November", "December"};
//Labels...
private Label monthLabel;
private Label dayLabel;
private Label yearLabel;
//TextFields...
private TextField monthField;
private TextField dayField;
private TextField yearField;
//Button...
private Button displayButton;
//Init...
public void init(){
//Creation of Labels...        
    monthLabel = new Label("Month (mm):");
    dayLabel = new Label("Day (dd):");
    yearLabel = new Label("Year (yyyy):");
//Creation of Fields...
    monthField = new TextField (10);
    dayField = new TextField (10);
    yearField = new TextField (10);
//Creations of Button...
    displayButton = new Button("Display Birthday");
    displayButton.addActionListener(this);
//Addition of all created to applet...        
    add(monthLabel);
    add(monthField);
    add(dayLabel);
    add(dayField);
    add(yearLabel);
    add(yearField);
    add(displayButton);
//Set window size...      
    setSize(1000,1000);
}
//Action...    
public void actionPerformed (ActionEvent e){
    if (e.getSource() == displayButton){
        displayCalendar = true;
//Convert input strings to int...            
        inputMonth = Integer.parseInt(monthField.getText());
        inputDay = Integer.parseInt(dayField.getText());
        inputYear = Integer.parseInt(yearField.getText());
//First day of month getter...               
        firstDayofInputMonth = getDayOfWeek(getDay(inputMonth, 1));
    }
    repaint();//Repaint on new action...
}
//Paint...   
public void paint (Graphics g){
    Font Default = new Font("TimesRoman", Font.ROMAN_BASELINE, 20);//Set Font....
    Font BOLD = new Font("TimesRoman", Font.BOLD, 23);
    g.setFont(Default);
        int xCoord = 450;
        int yCoord = 100;
        int Counter=0;//Value traversing through loop....
        if (displayCalendar == true){
//Month/Day Validation...        
            if(inputMonth>12 || inputMonth<1)
                g.drawString("INVALID Month input: "+inputMonth , 520, 75);            
            else if(inputDay>31|| inputDay<1)
                g.drawString("INVALID Day input: "+inputDay , 520, 100);
//If good data...        
            else{

Help?

user3498796
  • 71
  • 1
  • 3
  • 1
    Do you consider 31th of April as valid? – Meno Hochschild Apr 04 '14 at 16:40
  • For this program yes... Dr. Said thats how she wanted it because she hasn't taught us how to handle exceptions... If I had if(inputday>dayInMonth[inputmonth].... How would it know how many days do not exist in a non existent month? – user3498796 Apr 04 '14 at 16:42
  • 1) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. 2) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Apr 04 '14 at 23:45
  • She's already a doctor so I think there is no stopping here. She is a brilliant lady. I'm only a sophomore and this is still bootcamp... She is preparing us for bigger things... – user3498796 Apr 05 '14 at 03:59

1 Answers1

0

Your logic is incorrect. If you have determined that inputDay is invalid, then the else will run regardless of whether inputMonth is invalidated.

Make the inputDay condition an else if so that the else condition runs only when both conditions are false (validated).

if(inputMonth>12 || inputMonth<1)
        g.drawString("INVALID Month input: "+inputMonth , 520, 75);            
else if(inputDay>31|| inputDay<1)
        g.drawString("INVALID Day input: "+inputDay , 520, 100);
//If good data...        
else{

Of course under normal circumstances, you would need to add better validations, for things such as November 31st, February 29th (except leap days), etc.

rgettman
  • 176,041
  • 30
  • 275
  • 357