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
public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner...
public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner keyboard = new Scanner(System.in);        System.out.println("Enter a date in the format month/day/year");        String date = keyboard.nextLine();        //Make a copy        String dateCopy = date;               //Extract the values        //start with month        //indexOf() is used to find the index of a specified character in a givenString        int workingIndex = date.indexOf("/");...
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...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
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); } }
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...
Complete the following program so it prompts the user for a line of text and then...
Complete the following program so it prompts the user for a line of text and then displays the line in reverse. For instance if the input provided by the user is Good Day then the program would display yaD dooG import java.util.Scanner; public class Program2 { public static void main(String[] args) { Scanner kb = new Scanner(System.in);
Write a program that asks the user to type in ages. They will type a negative...
Write a program that asks the user to type in ages. They will type a negative age when they finish entering the ages. The program will print out the average of all of the ages and the oldest age entered. It should end with a newline. Sample output #1 Type a negative for age to exit Enter your age: 21 Enter your age: 22 Enter your age: 21 Enter your age: -8 The average age is: 21.33 The oldest person...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT