This discussion is archived
8 Replies Latest reply: Oct 6, 2007 10:43 PM by 807605 RSS

string to date... wouldnt work... :'(

807605 Newbie
Currently Being Moderated
could any 1 please tell me how come my program nt working!!
import java.lang.*;
import java.awt.event.*; 
import java.util.Scanner;


public class DayandYear
{
     public static void main(String[] args)
     {
          //Declare Variables
          
          int day, month, year, showYear;
          boolean leapYear;
          String date, charMonth;
          
          //Input from User
          Scanner scan = new Scanner(System.in);
          System.out.println("Please enter the date with (/): ");
          date = scan.next();
          
          //Convert String to variables int day, month, year.
          
          String[] splitDate = date.split ("/");
          
          day = Integer.parseInt(splitDate[0]);
          
          month = Integer.parseInt(splitDate[1]);
          
          year = Integer.parseInt(splitDate[2]);          
          
          //Display month by name
          charMonth = "????";
          if(month >= 1 && month <= 12)
          {
          if(month == 1)
          charMonth = "January";
          
          else if(month == 2)
          charMonth = "February";
          
          else if(month == 3)
          charMonth = "March";
          
          else if(month == 4)
          charMonth = "April";
          
          else if(month == 5)
          charMonth = "May";
          
          else if(month == 6)
          charMonth = "June";
          
          else if(month == 7)
          charMonth = "July";
          
          else if(month == 8)
          charMonth = "August";
          
          else if(month == 9)
          charMonth = "September";
          
          else if(month == 10)
          charMonth = "October";
          
          else if(month == 11)
          charMonth = "November";
          
          else if(month == 12)
          charMonth = "December";
          
          else
          System.out.println("INVALID DATE");}
          
          
          //Year
          showYear = year;
          
          if(year < 100)
          {showYear = year + 2000;}
          
          else if(year >= 1000)
          {showYear = year;}
          
          else
          {System.out.println("INVALID YEAR");} 
          
          //Normal or Leap Year
                    
          if((showYear % 400)== 0)
          {leapYear = true;}
          
          else if((showYear % 100)== 0)
          {leapYear = false;}
          
          else if((showYear % 4)== 0)
          {leapYear = true;}
          
          else 
          {leapYear = false;}          
          
          //Output
          if(leapYear)
          {if(month == 2 && day == 29)
          System.out.println("Your date is " + charMonth + " " + day + ", " + showYear);
          }
          
          else if(!leapYear)
          {if(month == 2 && day == 29)
          System.out.println("Your date is INVALID");
          }
          
          else
          {System.out.println("Your date is " + charMonth + " " + day + ", " + showYear);}

     }
     
}
  • 1. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    OgvIEt4LiFegO wrote:
    could any 1 please tell me how come my program nt working!!
    Please learn the rules here. First you tell us what the program should be doing, and then next you tell us what it is doing instead. Show full error messages if possible. Your job is to make our job (all volunteers, mind you) of helping you as easy as possible.
  • 2. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    petes1234 is right, of course - you should say both what you expect to happen and what is happening. But where did you get that bracket style from?

    Just for the record here's the output bit again
    //Output
    if(leapYear) {
        if(month == 2 && day == 29) {
            System.out.println("Your date is " + charMonth + " " + day + ", " + showYear);
        }
    } else if(!leapYear) {
        if(month == 2 && day == 29) {
            System.out.println("Your date is INVALID");
        }
    } else {
        System.out.println("Your date is " + charMonth + " " + day + ", "     + showYear);
    }
    How can it ever reach that last else part? I mean it's going to follow if(leapYear) or else if(!leapyear), isn't it?

    [Edit] Changed it to how I would format it. Glad to see you got the split() business sorted out. And a hint: consider dealing with the Feb 29 case first, and deal with leapyears within that.
  • 3. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    ooo........my bad i was in a hurry....

    that program is for taking a user input string in this format... m(m)/d(d)/yy(yy) ... ( ) mean optional.... then display it...

    EX... input : 1/1/2001
    output: January 1, 2001



    there r no error code.... and it compiled....... but when i run it... after entering the string the program doesnt output anything.... maybe because of the split() method that i used
  • 4. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    after entering the string the program doesnt output anything....
    It does if you enter Feb 29!
  • 5. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    OMG.... thank alot.... i didnt c that, it work now after i take out one else.... i guess i shouldnt be rushing
              if(leapYear)
              {
                                    if(month == 2 && day == 29)
              System.out.println("Your date is " + charMonth + " " + day + ", " + showYear);
              }
              
              else
              {
                                    if(month == 2 && day == 29)
              System.out.println("Your date is INVALID");
              else
              System.out.println("Your date is " + charMonth + " " + day + ", " + showYear);
              }
  • 6. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    it work now after i take out one else
    Great.

    Have you considered simplifying those if-else-if-else-if-... bits to decode the month name? You could use a switch statement. Or have an array of month names:
    String[] monthNames = {"January", "Febuary", "March", /*etc*/};
    // later...
    charMonth = monthNames[month - 1];
  • 7. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    i am reading about switch statement.... i do need to make it shorter seen i still need to add valid date range for each month....

    charMonth = monthNames[month - 1];
    i m nt quite get that statement
  • 8. Re: string to date... wouldnt work... :'(
    807605 Newbie
    Currently Being Moderated
    charMonth = monthNames[month - 1];
    i m nt quite get that statement
    If you set up an array of month names like I said, then when someone enters a date like 1/2/3 you will a month value of 2, right? So you can get the value of charMonth from the array as monthNames[1] which will be "February".

    Th reason why you say "month-1" and not "month" is that arrays start at 0, but the month number entered by the user starts at 1.

    Try this out:
    import java.util.Scanner;
    
    public class MonthArrayEg {
        public static void main(String[] args) {
            String[] monthNames = {
                "January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"
            };
              
            Scanner in = new Scanner(System.in);
            System.out.print("Enter a month number --> ");
            int month = Integer.parseInt(in.next());
              
            String monthStr = monthNames[month - 1];
            System.out.println("You entered " + monthStr);
        }
    }