-1

enter image description here

I am trying to make a program that user is able to enter a date like: 28 -03 - 2014.

This and the program reads that, gives the date of tomorrow like: 29 - march - 2014. The program must check about:

  • String max 10.
  • date of day(2 digit): 1 - 31
  • String: -
  • month (two digit): 1 - 12
  • String: -
  • year : four digits

Here is my code!

    public String month()
    {
            int month = 0;
            switch(month){
            case 1 :monthString = " Janauri";
                    break;
             case 2: monthString = "February"
    .......
    ublic String dateOfTomorrow(int day, int month, int year)

    {
    String Date =  day+ "-" + month+ "- " + year;
    day++;
    if(day > totalDaysInMonth(month));
    {// new  month
    day = 1;
    month++;
    if(month > 12)
    {//new year
    month= 1;
    year ++;
    }
  }
    return Date;

    }
        private boolean  totalDaysInMonth(int day)
        {
            if( day >= 1 && day < 31)
            {
                return true;
            }
            else {
                return false;
                }
        }


        public void actionPerformed(ActionEvent e)
            {
            for ( int i = 1; i<31;);
                String s = tf.getText();
                if ( e.getSource() == b1)
                {

                    l2.setText(s);

                }
                else if (e.getSource ()== b2)
                {
                    l2.setText(monthString);
                }

            }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Use a `JSpinner` with a [`SpinnerDateModel`](http://docs.oracle.com/javase/8/docs/api/javax/swing/SpinnerDateModel.html) instead. **Tips:** 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Mar 28 '14 at 19:57
  • *"when i click button 1/2 it just stack."* If by 'stack' you mean the application causes a stack trace, then.. Always copy/paste error & exception output. – Andrew Thompson Mar 28 '14 at 19:59
  • @AndrewThompson: I'm guessing that he's misspelling "stuck", that he's completely new to spelling as well as to Java. – Hovercraft Full Of Eels Mar 28 '14 at 20:32
  • Sorry for spelling mistakes. I mean stuck. Can't take action n I can either close the program. – Completely_new_to_java Mar 28 '14 at 20:47
  • Did you try fixing your code as per user2275785's suggestion in his answer below? – Hovercraft Full Of Eels Mar 28 '14 at 20:58
  • I did but still is not working correctly. The user is still able to type a date like this: 44- 15 - 2014. – Completely_new_to_java Mar 28 '14 at 22:18

2 Answers2

1

I think your problem is in this loop:

for ( int i = 1; i<31;);

which will never end. Remove that empty loop or change it to:

for ( int i = 1; i<31;i++);
user2275785
  • 199
  • 1
  • 19
0

I don't really understand what you mean by 1/2 stack. But if you make a string of some variables

String Date =  day + "-" + month + "-" + year;

and then change the variables it doesn't have any effect on the string. So you will still get the same date back.

And a tip for better readability makes your variables camel cased. So instead of Date call it date.