1 Replies - 1008 Views - Last Post: 24 February 2011 - 06:00 PM Rate Topic: -----

#1 Guest_Steve*


Reputation:

Get date, then print date + 1 day. Trouble on if statements.

Posted 24 February 2011 - 04:05 PM

The Instructions
- Repeat the following until the user just presses Enter
- Ask the user for a date of the form February 28, 2010 (you will need to use a StringTokenizer to break apart the pieces)
- Print out tomorrow's date like: March 1, 2010

- To help shorten your code, please:
- Create an array of month names
- Create an array of days per month
- 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
- Try to combine the cases of months with 30 days, months with 31 days, and February
- You will likely want a separate case for the last day of December

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


import java.util.*;

 

 

public class Proj5 {

 

  public static void main(String[] args) {

   Scanner s = new Scanner(System. in );

   String [] months = { "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" };

    int [] days = {31,28,31,30,31,30,31,31,30,31,30,31};

   

    String date;

    do {

     System. out .print( "Enter a date (Month Day, Year): " );

     date = (s.nextLine());

        if (!date.equals( "" )){

          StringTokenizer st = new StringTokenizer(date, " ," );

          String monthIn = st.nextToken();

          int day = Integer.parseInt(st.nextToken());

          int year = Integer.parseInt(st.nextToken());

          for ( int i=0; i<12; i++){

            for ( int j=0; j< 12; j++){

              if (months[i].equals(monthIn)){

               

               }

              //if (day[j].equal(days))

    }

   }

    while (!date.equals( "" )); 


I'm having trouble on the If statements I'm not sure how to get my days of each month to line up after it finds the month they typed in. And then how to group months together.

Is This A Good Question/Topic? 0

Replies To: Get date, then print date + 1 day. Trouble on if statements.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get date, then print date + 1 day. Trouble on if statements.

Posted 24 February 2011 - 06:00 PM

You have two imbricated for() loops to find the month, you need only one that returns the month index

   int monthIndex = -1;   // init not found
   for(int i = 0; i < months.length(); i++) {
     if(monthIn.equals(month[i])) {
        monthIndex = i;    // remind its index
        break;             // exit loop
     }
   }

   if(monthIndex == -1) {
      System.out.println("Month " + monthIn + " is invalid");
      return;
   }

   // ok now you know that number of days in that month is days[monthIndex]
   if(day < 0 || day > days[monthIndex]) {
      ... day is invalid
   }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1