• 0

Java help


Question

I have the following code, but I cannot figure out how to do it. I stopped at march, but basically where the input is numbers 1-12 the code should print out the numbers corresponding month name. Ex. 1 = January, 2 = February etc. After correcting this, I have to assign whether or not the year they input is a leap year, causing ( ex. february to have 29 days ) the # of days to change.

import java.util.Scanner;
public class DaysInMonth
{
	public static void main(String[]args)
	{
	 Scanner input = new Scanner(System.in);

	  System.out.print("Enter the number of a month and a year: ");
	  int Input = input.nextInt();
	  if (Input=1)
			System.out.print("January");
	  else if (Input=2)
			Sytem.out.print("February");
	  else if (Input=3)
		 System.out.print("March");

	}
}

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Best to have an array of 12 Strings containing the month names. After checking that the month number is valid you can then look up the name in the array. A year is a leap year if it is divisible by 4 (leaves a remainder of 0 when divided by 4 (hint)), unless it is divisible by 100, althoug divisible by 400 (eg 2000) IS a leap year.

Feel free to post your code for help and suggestions, after you have given it a good try!

Link to comment
Share on other sites

  • 0

import java.util.Scanner;
public class DaysInMonth
{
	public static void main(String[]args)
	{
	 Scanner input = new Scanner(System.in);
	  System.out.print("Enter the number of a month: ");
	  int Input = input.nextInt();
	  if(Input<=0)
			  System.out.println("Invalid Value");
	  else if(Input>12)
			  System.out.println("Invalid Value");
	  else if(Input==1)
			  System.out.println("January");
	  else if(Input==2)
			  System.out.println("February");
	  else if(Input==3)
			  System.out.println("March");
	  else if(Input==4)
			  System.out.println("April");
	  else if(Input==5)
			  System.out.println("May");
	 else if(Input==6)
			  System.out.println("June");
	  else if(Input==7)
			  System.out.println("July");
	  else if(Input==8)
			  System.out.println("August");
	  else if(Input==9)
			  System.out.println("September");
	  else if(Input==10)
			  System.out.println("October");
	  else if(Input==11)
			  System.out.println("November");
	  else if(Input==12)
			  System.out.println("December");

			System.out.println("Enter the year: ");
	 			 int Inputyear = input.nextInt();	
			if(Inputyear %4==0)
					System.out.println("Leap Year!");
			else System.out.println("Just a regular old year");

			if((Input %2==0)&&(Input==2))
					System.out.println("has 31 days");
			else if(Input%2 !=0)
					System.out.println("has 30 days");
			else if(Input==2)
				   System.out.println("has 28 days");


	}
}

Is what I came up with, close but not perfect, Id prefer the result to show "Month name" + Year + has x days in one string instead of different lines.

Link to comment
Share on other sites

  • 0
import java.util.Scanner;
public class DaysInMonth
{
	public static void main(String[]args)
	{
		String end = "";
		String[] months = {"January","February","March","April","May","June",
		"July","August","September","October","November","December"};
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of a month: ");
		int Input = input.nextInt();

		if(Input < 1 || Input > 12) {
		  System.out.println("Invalid Date Specified.");
		} else {
		  end += months[Input-1];
		}
			System.out.println("Enter the year: ");
				  int Inputyear = input.nextInt();
				  end += " " +Inputyear;
			if(Inputyear %4==0) {
				System.out.println("Leap Year!");
			}
			else {
				System.out.println("Just a regular old year");
			}

			if(Input %2==0) {
				end += " has 31 days.";
			}
			else if(Input%2 != 0) {
				end += " has 30 days.";
			}
			else if(Input==2) {
				end += " has 28 days.";
			}
			System.out.println(end);

	}
}

Link to comment
Share on other sites

  • 0
import java.util.Scanner;
public class DaysInMonth
{
	public static void main(String[]args)
	{
		String end = "";
		String[] months = {"January","February","March","April","May","June",
		"July","August","September","October","November","December"};
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of a month: ");
		int Input = input.nextInt();

		if(Input < 1 || Input > 12) {
		  System.out.println("Invalid Date Specified.");
		} else {
		  end += months[Input-1];
		}
			System.out.println("Enter the year: ");
				  int Inputyear = input.nextInt();
				  end += " " +Inputyear;
			if(Inputyear %4==0) {
				System.out.println("Leap Year!");
			}
			else {
				System.out.println("Just a regular old year");
			}

			if(Input %2==0) {
				end += " has 31 days.";
			}
			else if(Input%2 != 0) {
				end += " has 30 days.";
			}
			else if(Input==2) {
				end += " has 28 days.";
			}
			System.out.println(end);

	}
}

It is generally bad practise to declare variables with capital letter.

Scanner input = new Scanner(System.in);
int Input = input.nextInt();

should be:

Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();

And you should also try to reuse the variables that you declare, so instead of

int Inputyear = input.nextInt();

use:

input = scanner.nextInt();

Link to comment
Share on other sites

  • 0
It is generally bad practise to declare variables with capital letter.

Scanner input = new Scanner(System.in);
int Input = input.nextInt();

should be:

Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();

And you should also try to reuse the variables that you declare, so instead of

int Inputyear = input.nextInt();

use:

input = scanner.nextInt();

bad practise? do you have any idea how bytecode compiles? it makes no difference. plus its not my code so don't whine at me, I fixed the bottom up a little bit for OP.

Link to comment
Share on other sites

  • 0

Just because it compiles it doesn't mean it's not bad practice. The first word of a variable name (and method names at that) should start with lower case (unless constant, in which case the whole name should be upper case with words separated by underscores) and class names should start with an upper case.

e.g.

public class MyClass {
	public static void main(String[] args) {	// lower case 'm' in main
		ClassName startWithLowerCase = new ClassName();
	}
}

Link to comment
Share on other sites

  • 0

You can't use Input==2 to check for February because you already modify Input by -1.

So if you enter 2 for February, its now 1.

I would check if the mod 2 != 0 AND Input != 1 for 30 day months, and then else it would be 28, unless the year is / 4 then its 29.

Link to comment
Share on other sites

  • 0
.....

Is what I came up with, close but not perfect, Id prefer the result to show "Month name" + Year + has x days in one string instead of different lines.

print instead of println should do it, but I would recommend that you concatenate it all into one string as you go along (as I think someone suggested).

bad practise? do you have any idea how bytecode compiles? it makes no difference. plus its not my code so don't whine at me, I fixed the bottom up a little bit for OP.

That's good because these conventions have nothing to do with compiling.

Conventions such as these make your code more readable. This helps you when you have to go back to code after a long gap, it helps anyone marking your work when in school/uni and later down the line it helps people who have to edit your code for whatever reason. Comments and conventions ftw.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.