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/

Recommended Answers

All 6 Replies

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

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

Monthday, Monthname are declared as int, 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 as int.
Declare a different variable

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

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

switch (Monthname) {
case 1: monthDescription = "January";
dayDescription = "31";
int Monthname = input.nextInt();
int Monthday = input.nextInt();

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

Monthday, Monthname are declared as int, 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 as int.
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;

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.

This think:

dayDescription = (Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0) ? [B]28[/B]: [B]29[/B];

Returns int. 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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.