Jump to content

error

- - - - -

  • Please log in to reply
9 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I atrying to run the following program. I can write a program to validate time with the help of this program. but this program is showing a error message. Please tell me the reason
operator || cannot be applied to boolean,int




class Dates

{

    int dd, mm, yy;

    public Dates()

    {

        dd=0;

        mm=0;

        yy = 0;

    }

    public Dates(int d, int m, int y)

    {

        dd=d;

        mm=m;

        yy=y;

    }

    public boolean validate()

    {

        boolean valid=false;

        if((mm==1||mm==3||mm=5||mm==7||mm==8||mm==10||mm==12) && (dd>=1 && dd<=31))

        valid=true;

        else if ((mm==4||mm==6||mm==9||mm==11)&&(dd>-1&&dd<=30))

        valid=true;

        else if(mm==2&&(yy%4!=0)&&(dd>=1&&dd<=28))

        valid=true;

        else if(m==2 &&(yy%4==0)&&(dd>=1&&dd<=29))

        valid=true;

        return valid;

    }

    public void print()

    {

        System.out.println(dd+"/"+mm+"/"+yy);

    }

}




#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You're missing an equal sign (=) at 'mm=5'
You also did dd>-1 instead of dd>=1 at the 2nd if-statement.
Also missing an 'm' at 'm==2' @4th if-statement.

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
In Java, data types are treated more strictly than in C/C++. Int's are not necessarily compatible with booleans.

Here's the offending line:

if((mm==1||mm==3||mm=5||mm==7||mm==8||mm==10||mm==12) && (dd>=1 && dd<=31))

You used the assignment operator with # 5. Needs a double equals sign instead.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
wimDC beat me to the punch.
*bows to faster keyboard skills*
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts

gregwarner said:

In Java, data types are treated more strictly than in C/C++. Int's are not necessarily compatible with booleans.

Here's the offending line:

if((mm==1||mm==3||mm=5||mm==7||mm==8||mm==10||mm==12) && (dd>=1 && dd<=31))

You used the assignment operator with # 5. Needs a double equals sign instead.

The = mistakenly placed instead of == is such a frequent mistake in c, java and quite a few other languages, that a coding convention / practice was developed by linux kernel (i think) and was widely adopted.

ALWAYS put the CONSTANT in a comparison on LEFT side.


if (x ==4 ) // instead of this


if (4==x) // use this



You will always get a compile time error if you mistakenly type a single =

#6
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thank you for finding out my error. I have corrected the mistake. Now my class is compiles successfully.

wim DC said:

You're missing an equal sign (=) at 'mm=5'
You also did dd>-1 instead of dd>=1 at the 2nd if-statement.
Also missing an 'm' at 'm==2' @4th if-statement.


#7
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thank you for finding out my error. I have corrected the mistake. Now my class is compiles successfully.

gregwarner said:

In Java, data types are treated more strictly than in C/C++. Int's are not necessarily compatible with booleans.

Here's the offending line:

if((mm==1||mm==3||mm=5||mm==7||mm==8||mm==10||mm==12) && (dd>=1 && dd<=31))

You used the assignment operator with # 5. Needs a double equals sign instead.


#8
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thank you for finding out my error. I have corrected the mistake. Now my class is compiles successfully.

fayyazlodhi said:

The = mistakenly placed instead of == is such a frequent mistake in c, java and quite a few other languages, that a coding convention / practice was developed by linux kernel (i think) and was widely adopted.

ALWAYS put the CONSTANT in a comparison on LEFT side.


if (x ==4 ) // instead of this


if (4==x) // use this



You will always get a compile time error if you mistakenly type a single =


#9
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Here is my corrected code.

Now the question is, how can I get output if the date entered false.

If i enter a invalid date, I want to get a message "You have entered invalid date" instead of the date i have entered.
Suppose i have input date as 32/14/2001 i am getting the output as 32/14/2001. But i want to get output as "You have entered invalid date"

class Dates

{

    int dd, mm, yy;

    public Dates()

    {

        dd=0;

        mm=0;

        yy = 0;

    }

    public Dates(int d, int m, int y)

    {

        dd=d;

        mm=m;

        yy=y;

    }

    public boolean validate()

    {

        boolean valid=false;

        if((mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12) && (dd>=1 && dd<=31))

        valid=true;

        else if ((mm==4||mm==6||mm==9||mm==11)&&(dd>-1&&dd<=30))

        valid=true;

        else if(mm==2&&(yy%4!=0)&&(dd>=1&&dd<=28))

        valid=true;

        else if(mm==2 &&(yy%4==0)&&(dd>=1&&dd<=29))

        valid=true;

        return valid;

    }

    public void print()

    {

        System.out.println(dd+"/"+mm+"/"+yy);

    }

}


#10
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

fayyazlodhi said:

ALWAYS put the CONSTANT in a comparison on LEFT side.
You will always get a compile time error if you mistakenly type a single =

I like this thought.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users