Date check problem

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

51 Replies - 6752 Views - Last Post: 04 October 2011 - 01:36 PM Rate Topic: -----

#1 The92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-October 11

Date check problem

Posted 03 October 2011 - 10:24 AM

Hello everybody im doing an assignment and its about date checking program the problem is i dont want to type the month in the program with numbers i need to type the monthes name.. so i curiouse how can i save all the months names in java so if i typed in the month in the program it would say ya its right but if i miss spelled it would say no your wrong im going to show u my code ... but could you plz tell me how to save all the months name ? because i tried to make it with the string but it didnt work


TH CODE :
import java.util.Scanner;
/**
 *
 */
public class q {

    public static void main(String[] args) {

       int month=0;
       int day =0;
       int daysInMonth;
       int year=0;
       boolean monthValid, yearValid, dayValid;
       boolean leapYear;
       
       
     
        Scanner kbd = new Scanner(System.in);
        
        // Progams information
        System.out.println("Date Checker") ;
        System.out.println("------------");
        System.out.println();
    
        System.out.println();
        System.out.println("I will check if the date you enter is valid.");
        System.out.println();
        System.out.println("Enter the date below. Use the form /day month year/");
        System.out.println("(e.g. 29 February 1066).");
        day = kbd.nextInt(); month = kbd.nextInt(); year = kbd.nextInt();kbd.nextLine();
// leap year

    leapYear =(((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0));

// number days in month
    if ((month == 1) || (month == 3) || (month==7) || (month == 8) || ( month == 10) || ( month == 12))
    daysInMonth=31;
    if ((month == 4) || (month == 6) || (month == 9) || ( month == 11))
    daysInMonth=30;
    if (month == 2)
    daysInMonth=28;
    if (leapYear)
    daysInMonth=29;
    {
    System.out.println(day+" "+month+" "+year+" is a valid date!");
    }
    else if 
    {
    System.out.println(day+" "+month+" "+year+" is not a valid date!");
    }


        
    
           
    }
}

This post has been edited by The92: 03 October 2011 - 10:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Date check problem

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Date check problem

Posted 03 October 2011 - 10:37 AM

You can store them in array of string or enumerator and access them using the index. Like:
String[] months = {"Jan","Feb",....};
//then to get the 1st month it will be months[0]...

This post has been edited by smohd: 03 October 2011 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 The92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-October 11

Re: Date check problem

Posted 03 October 2011 - 10:44 AM

View Postsmohd, on 03 October 2011 - 10:37 AM, said:

You can store them in array of string or enumerator and access them using the index. Like:
String[] months = {"Jan","Feb",....};
//then to get the 1st month it will be months[0]...

thanx man for the reply but i haver an extra question...

if i want just to make sure if the validation.. i would right this if - else statement right ?

 if (year>=1990) && (month==String months) && ( month==daysInMonth)
    {
    System.out.println(day+" "+month+" "+year+" is a valid date!");
    }
    else if (year<1990) || (month!=String months) && ( month!=daysInMonth)
    {
    System.out.println(day+" "+month+" "+year+" is not a valid date!");
    }

Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Date check problem

Posted 03 October 2011 - 10:47 AM

if (year>=1990) && (month==String months) && ( month==daysInMonth)

You are missing a close ' ) '

Edit - And an opening '( ' trace your parenthesis in the statement.

This post has been edited by Fuzzyness: 03 October 2011 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Date check problem

Posted 03 October 2011 - 10:50 AM

this statement is wrong (month==String months)
If you want to check like that, then I advice using array list and to check if a month is in the list just months.contains(month) this returns true if the month is there and false if not
Was This Post Helpful? 0
  • +
  • -

#6 The92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-October 11

Re: Date check problem

Posted 03 October 2011 - 10:50 AM

View PostFuzzyness, on 03 October 2011 - 10:47 AM, said:

if (year>=1990) && (month==String months) && ( month==daysInMonth)

You are missing a close ' ) '

Edit - And an opening '( ' trace your parenthesis in the statement.



thanx bro but is it right the if else statment ?
Was This Post Helpful? 0
  • +
  • -

#7 The92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-October 11

Re: Date check problem

Posted 03 October 2011 - 10:56 AM

View Postsmohd, on 03 October 2011 - 10:50 AM, said:

this statement is wrong (month==String months)
If you want to check like that, then I advice using array list and to check if a month is in the list just months.contains(month) this returns true if the month is there and false if not



thank u very much but how can i see if month doesnt contains??
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Date check problem

Posted 03 October 2011 - 10:58 AM

You put all your month in array list say months, then i check you can say If(months.contains(month)) and it will return true or false
Was This Post Helpful? 0
  • +
  • -

#9 The92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-October 11

Re: Date check problem

Posted 03 October 2011 - 11:09 AM

View Postsmohd, on 03 October 2011 - 10:58 AM, said:

You put all your month in array list say months, then i check you can say If(months.contains(month)) and it will return true or false



i did everything but i had this problem i tried to fix but i couldnt


q.java:55: reached end of file while parsing
}
 ^

Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Date check problem

Posted 03 October 2011 - 11:12 AM

You have to post your updated code, that problem is due to mismatch brakets {}
Was This Post Helpful? 0
  • +
  • -

#11 The92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-October 11

Re: Date check problem

Posted 03 October 2011 - 11:16 AM

View Postsmohd, on 03 October 2011 - 11:12 AM, said:

You have to post your updated code, that problem is due to mismatch brakets {}




 System.out.println("I will check if the date you enter is valid.");
        System.out.println();
        System.out.println("Enter the date below. Use the form /day month year/");
        System.out.println("(e.g. 29 February 1066).");
        day = kbd.nextInt(); month = kbd.nextInt(); year = kbd.nextInt();kbd.nextLine();
// leap year

    leapYear =(((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0));

// number days in month
    if ((month == 1) || (month == 3) || (month==7) || (month == 8) || ( month == 10) || ( month == 12))
    daysInMonth=31;
    if ((month == 4) || (month == 6) || (month == 9) || ( month == 11))
    daysInMonth=30;
    if (month == 2)
    daysInMonth=28;
    if (leapYear)
    daysInMonth=29;
    {

 // checks the validation
    if ((year>=1990) && (months.contains(month)) && ( month==daysInMonth))
    {
    System.out.println(day+" "+month+" "+year+" is a valid date!");
    }
    if ((year<1990)
    {
    System.out.println(year+ " is not a valid year!");
    }
    if (month=!daysInMonth)
    {
    System.out.println(day+" is not a valid date for "+ month+" "+year);
    else
    {
    System.out.println(day+" "+month+" "+year+" is not a valid date!");
    }
    
    }
}


and thats a new problem MAAAN java isso hard !!! :(




q.java:60: 'else' without 'if'
else
^
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Date check problem

Posted 03 October 2011 - 11:18 AM

leapYear =(((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)); I believe you have 1 too many ' ( ' at the first one -> (((year
Mis read it my apology, see below though.

edit- Also:
if (month=!daysInMonth)
    {
    System.out.println(day+" is not a valid date for "+ month+" "+year);
    else
    {
    System.out.println(day+" "+month+" "+year+" is not a valid date!");
    }


You are missing a closing ' } ' for the if statement there

This post has been edited by Fuzzyness: 03 October 2011 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Date check problem

Posted 03 October 2011 - 11:18 AM

Remove opening { at line 19 of your updated code, seems to be the problem
Your new problem is because you didnt close if (month=!daysInMonth){

This post has been edited by smohd: 03 October 2011 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#14 The92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-October 11

Re: Date check problem

Posted 03 October 2011 - 11:21 AM

View PostFuzzyness, on 03 October 2011 - 11:18 AM, said:

leapYear =(((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)); I belive you have 1 too many ' ( ' at the first one -> (((year



bro when i deleted the brackets it said


q.java:35: ';' expected
leapYear =((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0));

i did everything and now its saying


q.java:35: ';' expected
leapYear =((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0));
^
1 error
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Date check problem

Posted 03 October 2011 - 11:22 AM

Yes, put that ' ( ' , I misread that line, lots of parenthesis there got jumbled. See smohd and my edit.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »