954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Incompatible types

Hello,
I am new to the forum and to Java have no programming experience. try to write a program that enters a year and month as integers and displays the number of days in the month.
here is what I have written any help would be much appericated.

import java.util.Scanner;

public class Program3
{
public static void main(String [] args) {

Scanner input = new Scanner (System.in);
//Enter a Month 1-12
System.out.print("Enter a Monthname: ");
int Monthname = input.nextInt();

//Enter a day 1-31
System.out.print ("Enter a Monthday: " );
int Monthday = input.nextInt();

//Enter a Year
System.out.print ("Enter a Year: ");
int Year = input.nextInt();


switch (Monthname) {
case 1: Monthname = "January";
Monthday = "31";
break;

case 2: Monthname = "February"; Monthday =(Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0)? 28 : 29;
break;

case 3: Monthname = "March";
Monthday = "31"; break;

case 4: Monthname = "April"; Monthday = "30"; break;

case 5: Monthname = "May"; Monthday = "31"; break;

case 6: Monthname = "June"; Monthday = "30";
break;

case 7: Monthname = "July"; Monthday = "31";
break;
case 8: Monthname = "August";
Monthday = "31"; break;

case 9: Monthname = "September"; Monthday = "30";
break;
case 10: Monthname = "October";
Monthday = "31"; break;

case 11: Monthname = "November"; Monthday = "30";
break;

case 12: Monthname = "December";
Monthday = "31"; break;

default: System.out.println ("Errors: invalid data");
System.exit(0);


}
//Display results
System.out.println("Monthname + Year" + "has" + Monthday + "days");
}


}

this is what I wrote any help in correcting the incompatible types for the case.
I am using JCreator

Thanks/

droeph
Newbie Poster
2 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
int Monthname = input.nextInt();
int Monthday = input.nextInt();

switch (Monthname) {
case 1: Monthname = "January";
Monthday = "31";


Monthday, Monthname are declared asint, at the beginning of your code. But then you do this:
Monthname = "January";
"January" is a String. You cannot put a String ("January") inside a variable declared asint.
Declare a different variable

int Monthname = input.nextInt();
int Monthday = input.nextInt();

String monthDescription = "";
String dayDescription = "";

switch (Monthname) {
case 1: monthDescription = "January";
dayDescription = "31";
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
int Monthname = input.nextInt();
int Monthday = input.nextInt();

switch (Monthname) {
case 1: Monthname = "January";
Monthday = "31";

Monthday, Monthname are declared asint, at the beginning of your code. But then you do this: Monthname = "January"; "January" is a String. You cannot put a String ("January") inside a variable declared asint. Declare a different variable

int Monthname = input.nextInt();
int Monthday = input.nextInt();

String monthDescription = "";
String dayDescription = "";

switch (Monthname) {
case 1: monthDescription = "January";
dayDescription = "31";

Ok I made the changes but I still get incompatible types for
case 2 : MonthDescription = "February";
dayDescription = (Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0) ? 28: 29;

droeph
Newbie Poster
2 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
Ok I made the changes but I still get incompatible types for case 2 : MonthDescription = "February"; dayDescription = (Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0) ? 28: 29;


You made the changes? What changes did you make? You need to post the changes.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This think:

dayDescription = (Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0) ? <strong>28</strong>: <strong>29</strong>;

Returnsint. The bold ones 28, 29 at the above code are ints. so you need to store them in an int value or have the expression return then as String: "28", "29".

You need to be more careful with these errors and learn how to fix them yourself. You should be able to fix these yourself and not go running for help with every single glitch.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

hi
i am working on my final year project i am facing same kind of problem any suggestion for me

gotorightway123
Newbie Poster
4 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
hi i am working on my final year project i am facing same kind of problem any suggestion for me

My suggestion is search relevant threads or start your own.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You