473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using If-Else-If statement but it keeps saying it is an invalid entry

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Enter the day of the month. ");
  2.   dayOfMonth = keyboard.nextInt();
  3.  
  4.    System.out.println("Enter the month. ");
  5.   monthName = keyboard.nextLine();
  6.  
  7.    System.out.println("Enter the year. ");
  8.   int Year = keyboard.nextInt();
  9.  
  10.   if (Year/4){
  11.   if (Year/ 4 && Year/100)
  12.    {isItLeapYear=true;}
  13.   else if (Year/4 && Year/100 && Year/400)
  14.    {isItLeapYear=true;}
  15.  } else {isItLeapYear=false;}


it says that I cannot use Year/ because it is an int not Boolean..
Oct 23 '13 #1
4 1615
This is what is says NumDaysLeftInYear.java:61: error: incompatible types
if (Year/4){
^
required: boolean
found: int
NumDaysLeftInYear.java:62: error: bad operand types for binary operator '&&'
if (Year/ 4 && Year/100)
^
first type: int
second type: int
Oct 23 '13 #2
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

The binary operator && compares two booleans. A boolean is either true or false. Year / 4 and Year / 100 is not a true or false statement. A comparison of the result to an expected value is a true or false statement but just the result itself is neither true or false. It's like asking if 1 + 1 is true or false. It is not a true or false question. But asking if 1 + 1 is equal to 2 is a true or false question.

Also, you should know that you don't need to check Year / 4 twice. There's no point in doing so.
Oct 23 '13 #3
rajujrk
107 100+
Hi,

Please go over Java Basics.

if statement allows only boolean type

Expand|Select|Wrap|Line Numbers
  1. if(<boolean_value>){
  2.         -----
  3. }{
  4.         -----
  5. }
  6.  
Year is int, then Year/4 will return int or double type

these types are not allowed inside the if statement

Thanks
Raju
Oct 30 '13 #4
Nepomuk
3,112 Expert 2GB
What you probably wanted is something like this:
Expand|Select|Wrap|Line Numbers
  1. if (Year % 4 == 0){
  2.   if (Year % 4 == 0 && Year % 100 == 0)
  3.     {isItLeapYear=true;}
  4.   else if (Year % 4 == 0 && Year % 100 == 0 && Year % 400 == 0)
  5.    {isItLeapYear=true;}
  6.  } else {isItLeapYear=false;}
The Modulo-operator (%) will calculate what number is left when dividing the first number by the second one; so for example 9 % 4 == 1 as there is a number x for which x * 4 + 1 == 9.

Mind you, the else if-case will never be reached as all years that fulfil the condition is divisible by 4, 100 and 400 also fulfil the condition is divisible by 4 and 100 (which is the condition in the if-condition before the else if).
Oct 30 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
2
by: bashanitwonline | last post by:
Hi All, I have DB2 8.1 on my system. I have created a procedure as follows. CREATE PROCEDURE DB2ER000.XYZ() LANGUAGE SQL BEGIN INSERT INTO DB2ER000.A ( C,D,E) VALUES (10,20,30); END It...
0
by: Lavs | last post by:
Hi Guys... Is there anyone who could help me on how to add an image file to a database using SQL Statement... I am using a database in MS ACCESS 2003, and my application was designed in VB 6.0. I...
0
by: brainfulkale | last post by:
My question is that "How to print on VDU without using printf statement in unix" In DOS i use far pointer as cls { char far *s; s=0xB8000000; *s='A'; } But if i wanted to print in unix jow...
5
by: konaravikumar | last post by:
writing a query to get the second highest value in atable by using select statement
1
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
Is using a jump statement more faster than using if statement with a jump statement example for(int i=0; i < 10; i++) { if(a == null) {
8
reginaldmerritt
by: reginaldmerritt | last post by:
Hi, I have two report layouts that are used to run 12 different reports using where statement. for example DoCmd.OpenReport "SomeReport", _ acViewPreview, _ ...
1
by: sakura | last post by:
Hi. I'm using asp.net and c# language. How can i display a GridView using select statement of two column in one table? This is my coding: if (sdr.Read()) { if(namaPembekal.Equals(sdr)...
2
by: lenniekuah | last post by:
Hullo Good Friends, I am back again seeking help. Please help me. I am using C#NET2008 Window Application, This is the situation. Should either one of this control, txtXMLFolder.Text ,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.