Question

/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...

/* Program Name: BadDate.java

Function: This program determines if a date entered by the user is valid.

Input: Interactive

Output: Valid date is printed or user is alerted that an invalid date was entered.

*/

import java.util.Scanner;

public class BadDate

{

public static void main(String args[])

{

// Declare variables

Scanner userInput = new Scanner (System.in);

String yearString;

String monthString;

String dayString;

int year;

int month;

int day;

boolean validDate = true;

final int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;

// This is the work of the housekeeping() method

// Get the year, then the month, then the day

Scanner input = new Scanner(System.in);

// variable = input.nextLine();

System.out.println("Enter the Month");

monthString = input.nextLine();

System.out.println("Enter the Day");

dayString = input.nextLine();

System.out.println("Enter the Year");

yearString = input.nextLine();

// Convert Strings to integers

month = Integer.parseInt(monthString);

day = Integer.parseInt(dayString);

year = Integer.parseInt(yearString);

// This is the work of the detailLoop() method

// Check to be sure date is valid

if( year <= MIN_YEAR ) // invalid year

validDate = false;

if ( month < MIN_MONTH || month > MAX_MONTH ) // invalid month

validDate = false;

if ( day < MIN_DAY || day > MAX_DAY ) // invalid day

validDate = false;

// This is the work of the endOfJob() method

// Test to see if date is valid and output date and whether it is valid or not

if( validDate == true )

{ System.out.println("(month)/ (day)/ (year) is a valid date");

// Output statement

}

else

{

System.out.println("(month)/ (day)/ (year) is an invalid date");

// Output statement

}

} // end of main() method

} // end of BadDate class


HOW DO I GET AN INVALID AND VALID INPUT?

Homework Answers

Answer #1

Note: I have highlighted the modified code . Do Check it.

Code to copy:

   /* Program Name: BadDate.java
   Function: This program determines if a date entered by the user is valid.
   Input: Interactive
   Output: Valid date is printed or user is alerted that an invalid date was entered.
   */

   import java.util.Scanner;
   import javax.swing.JOptionPane;

   public class BadDate
   {
       public static void main(String args[])
       {
           // Declare variables
           Scanner userInput = new Scanner (System.in);
           String yearString;
           String monthString;
           String dayString;
           int year;
           int month;
           int day;
           boolean validDate = true;
           final int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
           // This is the work of the housekeeping() method
           // Get the year, then the month, then the day
           Scanner input = new Scanner(System.in);
           // variable = input.nextLine();
           System.out.println("Enter the Month");
           monthString = input.nextLine();
           System.out.println("Enter the Day");
           dayString = input.nextLine();
           System.out.println("Enter the Year");
           yearString = input.nextLine();
           // Convert Strings to integers
           month = Integer.parseInt(monthString);
           day = Integer.parseInt(dayString);
           year = Integer.parseInt(yearString);
           // This is the work of the detailLoop() method
           // Check to be sure date is valid
           if( year <= MIN_YEAR ) // invalid year
               validDate = false;
           if ( month < MIN_MONTH || month > MAX_MONTH ) // invalid month
               validDate = false;
           if ( day < MIN_DAY || day > MAX_DAY ) // invalid day
               validDate = false;
           // This is the work of the endOfJob() method
           // Test to see if date is valid and output date and whether it is valid or not
           if( validDate == true )
           {
               System.out.println(month+"/"+day+"/"+year + " is a valid date");
               // Output statement
           }
           else
           {
              System.out.println(month+"/"+day+"/"+year + " is an invalid date");
               // Output statement
           }
       }// end of main() method
   } // end of BadDate class

Output Screenshot:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
I am trying to take a string of numbers seperated by a single space and covert...
I am trying to take a string of numbers seperated by a single space and covert them into a string array. I have created the following code but it only works if the numbers are seperated a a comma or something similar to that. Example of what I am trying to achieve: string input = "1 1 1 1 1" turn it into.... int[] x = {1,1,1,1} so that it is printed as... [1, 1, 1, 1]    This is...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the range between −25 and 25 THIS IS MY CODE! please edit this...Thank you import java.util.Scanner; public class Array { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int[] integs = new int[51]; System.out.println("Enter integers 0-50 [enter -1 to quit]: "); int nums = scan.nextInt(); while (nums != -1 && nums>=0 && nums<=50) { integs[nums]++; nums = scan.nextInt(); } for...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT