Error with use of &&, || and !

Problems with if statement

Page 1 of 1

12 Replies - 721 Views - Last Post: 05 October 2009 - 10:29 AM Rate Topic: -----

#1 TKOP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 09

Error with use of &&, || and !

Post icon  Posted 04 October 2009 - 06:58 PM

I am trying to write a program that will allow a user to enter a number for the month and then tell them how many days are in that month. I have entered an if statement to determine if the year is a leap year. So it has to check to see if the year is divisible by 4 and a multiple of 400 but not divisible by 100. I keep getting the error "DateConvert.java:36: operator && cannot be applied to int,int
if ((year/4) && (year * 400) || (! year/100))
^
DateConvert.java:36: operator ! cannot be applied to int
if ((year/4) && (year * 400) || (! year/100))
^
from the compiler. Here is the part of my code that is giving me an error:

 	if ((year/4) && (year * 400) || (! year/100))
			System.out.println("There are " + MONTH4 + " days in " + monthName4);
		else



Why does the compiler say that && cannot be used with int? What's going on?

Is This A Good Question/Topic? 0
  • +

Replies To: Error with use of &&, || and !

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Error with use of &&, || and !

Posted 04 October 2009 - 07:20 PM

The logical operators are looking for boolean values or expressions. In your if statement, you are only providing numerical values. You have to say something like this:

if(year/4 == 400 && year * 400 = 20000)...



In short, compare your mathematical expressions to other values or variables.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error with use of &&, || and !

Posted 04 October 2009 - 07:34 PM

View Postmacosxnerd101, on 4 Oct, 2009 - 06:20 PM, said:

The logical operators are looking for boolean values or expressions. In your if statement, you are only providing numerical values. You have to say something like this:

if(year/4 == 400 && year * 400 = 20000)...



In short, compare your mathematical expressions to other values or variables.

Macosnerd.... year * 400 == 20000

just applying your rules (which are the good rules)
Was This Post Helpful? 1
  • +
  • -

#4 TKOP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 09

Re: Error with use of &&, || and !

Posted 04 October 2009 - 08:59 PM

Well, that worked to get me unstuck so my program will run, but for some reason the if statement is being ignored. Here's my entire code so you can see why it might be skipping over the statement.

import java.util.Scanner;

public class DateConvert
{

	public static final int MONTH1 = 31;
	public static final int MONTH2 = 30;
	public static final int MONTH3 = 28;
	public static final int MONTH4 = 29;
	public static void main(String[]args)
	{

		Scanner kbd = new Scanner(System.in);
		System.out.println("Please enter an integer for a month (1-12): ");
		int monthNumber = kbd.nextInt();
		System.out.println("Please enter an integer for the year: ");
		int year = kbd.nextInt();
		
		String monthName1 = "January";
		String monthName2 = "February";
		String monthName3 = "March";
		String monthName4 = "April";
		String monthName5 = "May";
		String monthName6 = "June";
		String monthName7 = "July";
		String monthName8 = "August";
		String monthName9 = "September";
		String monthName10 = "October";
		String monthName11 = "November";
		String monthName12 = "December";
		
		if (year/4 == 400 && year * 400 == 20000)
			System.out.println("There are " + MONTH4 + " days in " + monthName2);
		else

		switch (monthNumber)
		{
			case 1: 
			System.out.println("There are " + MONTH1 + " days in " + monthName1);
			break;
			case 2:
			System.out.println("There are " + MONTH3 + " days in " + monthName2);
			break;
			case 3:
			System.out.println("There are " + MONTH1 + " days in " + monthName3);
			break;
			case 4:
			System.out.println("There are " + MONTH2 + " days in " + monthName4);
			break;
			case 5:
			System.out.println("There are " + MONTH1 + " days in " + monthName5);
			break;
			case 6:
			System.out.println("There are " + MONTH2 + " days in " + monthName6);
			break;
			case 7:
			System.out.println("There are " + MONTH1 + " days in " + monthName7);
			break;
			case 8:
			System.out.println("There are " + MONTH1 + " days in " + monthName8);
			break;
			case 9:
			System.out.println("There are " + MONTH2 + " days in " + monthName9);
			break;
			case 10:
			System.out.println("There are " + MONTH1 + " days in " + monthName10);
			break;
			case 11:
			System.out.println("There are " + MONTH2 + " days in " + monthName11);
			break;
			case 12:
			System.out.println("There are " + MONTH1 + " days in " + monthName12);
			break;
			default:
			System.out.println("That was an invalid entry. There are only 12 months in a year");
			}
				
	}
} 


Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error with use of &&, || and !

Posted 04 October 2009 - 09:14 PM

Don't want to .... don't know the words but your code is an horror !!!

String[] monthStr = {"January", "February", ....."December");
int[] dayByMonth = {31, 28, 31, 30, 31, 30,,....30, 31};

if(leapYear) dayByMonth = 29;

System.out.println("There are " + dayByMont[monthNumber] + " days in " + monthStr[monthNumber]);



3 lines of code instead of 48... and this is because you do not use the Maya calendar which as 48 months :)
Was This Post Helpful? 1
  • +
  • -

#6 TKOP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 09

Re: Error with use of &&, || and !

Posted 04 October 2009 - 09:31 PM

I like what you're showing me, but that's more advanced than we are in my programming class. If I did that my teacher would think I'm a more advanced programmer than I am. But there is some stuff there that I can use. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error with use of &&, || and !

Posted 04 October 2009 - 09:34 PM

View PostTKOP, on 4 Oct, 2009 - 08:31 PM, said:

I like what you're showing me, but that's more advanced than we are in my programming class. If I did that my teacher would think I'm a more advanced programmer than I am. But there is some stuff there that I can use. Thanks for the help!

You are welcomed... one day you will learn how to use array... I hope :)
I appreciate your Thanks
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Error with use of &&, || and !

Posted 04 October 2009 - 09:38 PM

View Postpbl, on 4 Oct, 2009 - 08:34 PM, said:

Macosnerd.... year * 400 == 20000

just applying your rules (which are the good rules)



Thanks for catching my typo. I was in a hurry and made a mistake.
Was This Post Helpful? 1
  • +
  • -

#9 TKOP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 09

Re: Error with use of &&, || and !

Posted 04 October 2009 - 09:47 PM

So is there a way to fix my problem so that the if statement is not ignored without jumping ahead of the class? Why would the compiler be ignoring the if statement? If I enter 2 for February and 2008 for a leap year, it says February has 28 days. :crazy:
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error with use of &&, || and !

Posted 04 October 2009 - 09:47 PM

View Postmacosxnerd101, on 4 Oct, 2009 - 08:38 PM, said:

View Postpbl, on 4 Oct, 2009 - 08:34 PM, said:

Macosnerd.... year * 400 == 20000

just applying your rules (which are the good rules)



Thanks for catching my typo. I was in a hurry and made a mistake.

No problem you are welcome...
Was This Post Helpful? 0
  • +
  • -

#11 TKOP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 09

Re: Error with use of &&, || and !

Posted 05 October 2009 - 10:16 AM

Can someone tell me why my if statement is being ignored? When I run the program and enter February 2004, which is a leap year, I get "There are 28 days in February" instead of "There are 29 days in February".
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Error with use of &&, || and !

Posted 05 October 2009 - 10:23 AM

Because the following if statement was an example designed to illustrate comparisons, not to be incorporated into your program. In other words, the values and variables are mock fields and variables. You need to design your own if statement.

if (year/4 == 400 && year * 400 == 20000)
			System.out.println("There are " + MONTH4 + " days in " + monthName2);


Was This Post Helpful? 1
  • +
  • -

#13 TKOP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 09

Re: Error with use of &&, || and !

Posted 05 October 2009 - 10:29 AM

OK Thanks for the info and help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1