1

Having issues on this program getting the output correct. I've included parameters that help decipher what is a valid date input (i.e. 3/22/2014) vs. an invalid input (i.e. 3/57/2014). These are located within my for loop in the if statement. However, when I run the program, I keep receiving the else statement's output and I'm not sure why. I've switched array types from String and int with no luck. Do you see my error? Thanks

  import java.util.Scanner;
  import javax.swing.*;
public class ConvertDate
    {
    public static void main(String[] args)
    {
     String enteredDate, month, day, year;
     final String[] monthID = {"January", "February", "March", "April", "May", "June", "July",      
        "August", "September", "October", "November", "December"};

     final int[] monthIDArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
     final String[] monthValidSingleInputs = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",  
        "11", "12"};
     final String[] monthValidDosInputs = {"01", "02", "03", "04", "05", "06", "07", "08", "09", 
        "10", "11", "12"};
     final String[] monthsDays = {"31", "28", "31", "30", "31", "30", "31", "31", "30", "31", 
        "30", "31"};
     final int[] intDaysArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

     final int JANUARY = 1;
     final int DECEMBER = 12;
     int selectedDate;
     int x;

     enteredDate = JOptionPane.showInputDialog(null, "Enter a date in the format MM/DD/YYYY");

Using indexOf in order to distinguish inputs inside of /

     int slashIndex = enteredDate.indexOf("/");
  //Finding the month
        month = enteredDate.substring(0, slashIndex);
           int intMonth = Integer.parseInt(month);
        enteredDate = enteredDate.substring(slashIndex + 1);
        enteredDate = enteredDate.trim();
  //Finding the day
        slashIndex = enteredDate.indexOf("/");
        day = enteredDate.substring(0, slashIndex);
           int intDay = Integer.parseInt(day);
        enteredDate = enteredDate.trim();
        enteredDate = enteredDate.substring(slashIndex + 1);
  //Finding the year
        year = enteredDate.substring(0, 4);
           int intYear = Integer.parseInt(year);
        enteredDate = enteredDate.trim();

Output statements

  for(x = 0; x < monthID.length; ++x)
{
if(((month == monthValidSingleInputs[x]) || (month == monthValidDosInputs[x])) && (intDay <= 
       intDaysArray[x]) && ((intMonth >= JANUARY) && (intMonth <= DECEMBER)))
           JOptionPane.showMessageDialog(null, "Date is: " + monthID[x] 
           + " " + intDay + ", " + year);
           else
              JOptionPane.showMessageDialog(null, "Please re-run program, entries not valid.");
              break;
     }}}
ewbrowning
  • 107
  • 1
  • 10

1 Answers1

1

The primary issue was with your for loop. Since you had been using the break statement inside the for loop, it was getting terminated after your first iteration.

And another issue with your code was that, you were using == to compare strings. You can't compare two strings using == operator. You should use the method equals() or equalsIgnoreCase() to compare two strings.

You have to change the block indicated as 'output statements' in order to get the correct output. The modified code is shown below:

    boolean flag=false;

    for(x = 0; x < monthID.length; ++x)
    {
      if(((month.equals(monthValidSingleInputs[x])) || (month.equals(monthValidDosInputs[x]))) && 
          (intDay <= intDaysArray[x]) && ((intMonth >= JANUARY) && (intMonth <= DECEMBER)))
      {
           JOptionPane.showMessageDialog(null, "Date is: " + monthID[x] + " " + intDay + ", " + year);
           flag=true;
           break;
      }
     }

     if(flag==false)
       JOptionPane.showMessageDialog(null, "Please re-run program, entries not valid.");
  }
}
Praveen Vinny
  • 2,372
  • 6
  • 32
  • 40