Question

Write a Java program that repeats the following until the user wishes to stop: Asks the...

Write a Java program that repeats the following until the user wishes to stop:

  • Asks the user for a date of the form September 30, 2017 (you will need to use a StringTokenizer to break apart the pieces)
  • Prints out tomorrow's date like: October 1, 2017

Here is a sample of what your program might look like (user input is in red):

Enter today's date: September 30, 2017
Tomorrow's date: October 1, 2017

Do you want to go again? (y/n) y

Enter today's date: December 31, 2017
Tomorrow's date: January 1, 2018

Do you want to go again? (y/n) y

Enter today's date: August 16, 2000
Tomorrow's date: August 17, 2000

Do you want to go again? (y/n) n

Hints

To help shorten your code, please:

  • Create an array of month names, like this:
String[] monthNames = {"January","February,"March","April","May","June","July"
                       "August","September","October","November","December"};
  • Create an array of days per month, like this:
int[] monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};

Now, you can easily check to see how many days are in the month you're in to see if tomorrow should be in the next month. For example, if the user entered "March 31", you can find that "March" is at index 2 in monthNames. Then you look see monthDays[2] is 31 (the number of days in March), which is the same as the day entered. You then know that tomorrow will be the first of the next month (monthNames[3], which is "April")

You will want separate cases for:

  • The end of the year
  • The end of a month (other than December)
  • The middle of a month

In this program, you may assume that February has 28 days. (If you get everything else working, try modifying it to work for leap years. Leap years are usually divisible by 4, but we exclude years that are divisible by 100 and include years that are divisible by 400. For example, 1996, 1600, and 2000 are leap years. 2003 and 1900 are not.)

Here are the usual days per month (as was included in the monthDays array above):

January -> 31 days
February -> 28 days
March -> 31 days
April -> 30 days
May -> 31 days
June -> 30 days
July -> 31 days
August -> 31 days
September -> 30 days
October -> 31 days
November -> 30 days
December -> 31 days

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String choice;
  
  
do{
String[] monthNames = {"January","February","March","April","May","June","July","August","September","October","November","December"};
int[] monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
  
System.out.print("Enter today's date (example: September 30, 2017): ");
boolean givenMonthOK=false; boolean givenDayOK=false; boolean givenYearOK=false;
int givenMonth=-1; int givenDay=-1; int givenYear=-1;
//try{
String inputDate=sc.nextLine();
  
String[] dateTokens = new String[3];
  
StringTokenizer st1 = new StringTokenizer(inputDate, " ");
int i=-1;
while (st1.hasMoreTokens())
dateTokens[++i]=st1.nextToken();
  
  
if (dateTokens[2].length()>0){
givenYear = Integer.parseInt(dateTokens[2]);
givenYearOK=true;
  
boolean leap = false;
if(givenYear % 4 == 0){
if( givenYear % 100 == 0){
if ( givenYear % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
  
if (leap==true)
monthDays[1]=29;
}
if (givenYearOK==true){
for(int j=0;j<12;j++){
if (dateTokens[0].equalsIgnoreCase(monthNames[j])){
givenMonthOK=true;
givenMonth=j;
}
}
}
if (givenMonthOK==true){
if (dateTokens[1].length()>1){
givenDay = Integer.parseInt(dateTokens[1].substring(0,(dateTokens[1].length()-1)) );
if (givenDay>=1 && givenDay<=monthDays[givenMonth])
givenDayOK=true;
}
}
  
}
catch (Exception e){
System.out.println("Given date is not in proper format!!!");
}
  
if(givenMonthOK==true && givenDayOK==true && givenYearOK==true){
String nextDateMonth=monthNames[givenMonth];
int nextDateDay=givenDay+1;
int nextDateYear=givenYear;
  
if (givenDay==monthDays[givenMonth]){
nextDateDay=1;
if (givenMonth==11){
nextDateMonth=monthNames[0];
nextDateYear=givenYear+1;
}
else
nextDateMonth=monthNames[givenMonth+1];
}
  
System.out.print("Tomorrow's date: "+nextDateMonth+" "+nextDateDay+", "+nextDateYear);
}
else{
System.out.println("Given date is not in proper format!!!");
}
  
System.out.println("Do you want to go again? (y/n)");
choice=sc.nextLine();
}while((choice.length()>0)&&(choice.charAt(0)=='Y'||choice.charAt(0)=='y'));
}
}

Add a comment
Know the answer?
Add Answer to:
Write a Java program that repeats the following until the user wishes to stop: Asks the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT