I have the following as my code, and i cant figure out why i am getting this error. I think it has to do with my While statement. Please help!

import javax.swing.*;
import java.lang.String;
  
public class FollowingDate
{
  public static void main(String[] args)
  {
    String date, month, day, year;
    int daysFeb = 28;
    int numDay, numYear, indexOfFirstSlash, indexOfSecondSlash;
    int slashCount, numMonth, charCount;
    final int daysJan = 31, daysMar = 31, daysApr = 30, daysMay = 31;
    final int daysJun = 30, daysJul = 31, daysAug = 31, daysSep = 30;
    final int daysOct = 31, daysNov = 30, daysDec = 31;
    
    date = JOptionPane.showInputDialog("Enter a date in the form: "
                                             + "month/day/year");
    
    while (!date.equals("#"))
    {
    for (charCount = 0; charCount < date.length(); charCount++)
    {
      if (date.charAt(charCount) != "/")
      {
        if (date.charAt(charCount) < '0' || date.charAt(charCount) > '9')
          System.out.println("Incorrect input format");
    
    indexOfFirstSlash = date.indexOf('/');
    indexOfSecondSlash = date.indexOf('/', indexOfFirstSlash + 1);
    
    month = date.substring(0, indexOfFirstSlash);
    day = date.substring(indexOfFirstSlash + 1, indexOfSecondSlash);
    year = date.substring(indexOfSecondSlash + 1, date.length());
    
    numMonth = Integer.parseInt(month.trim());
    numDay = Integer.parseInt(day.trim()) + 1;
    numYear = Integer.parseInt(year.trim());
    
    if (numYear % 4 == 0){
      if (numYear % 400 == 0)
        daysFeb = daysFeb + 1;
      else if (numYear % 100 != 0)
        daysFeb = daysFeb + 1;
    }    
    
    if (numMonth == 1 && numDay > daysJan){
      numMonth++;
      numDay = 1;
    }
    else if (numMonth == 2 && numDay > daysFeb){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 3 && numDay > daysMar){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 4 && numDay > daysApr){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 5 && numDay > daysMay){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 6 && numDay > daysJun){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 7 && numDay > daysJul){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 8 && numDay > daysAug){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 9 && numDay > daysSep){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 10 && numDay > daysOct){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 11 && numDay > daysNov){
      numDay = 1;
      numMonth++;
    }
    else if (numMonth == 12 && numDay > daysDec){
      numMonth = 1;
      numYear++;
      numDay = 1;
    }

    switch (numMonth)
    {
      case 1:
        month = "January";
        break;
      case 2:
        month = "February";
        break;
      case 3:
        month = "March";
        break;
      case 4:
        month = "April";
        break;
      case 5:
        month = "May";
        break;
      case 6:
        month = "June";
        break;
      case 7:
        month = "July";
        break;
      case 8:
        month = "August";
        break;
      case 9:
        month = "September";
        break;
      case 10:
        month = "October";
        break;
      case 11:
        month = "November";
        break;
      case 12:
        month = "December";
        break;
    }
    
    JOptionPane.showMessageDialog(null, "The following date is: " +
                                  month + " " + numDay + ", " + numYear);
    }
    )
  )
  }
}
VernonDozier commented: Used code tags on first post. +11

Recommended Answers

All 2 Replies

Thank you for using code tags! Please point out the line that is giving you the error (you can highlight it in red or just tell us what line to look for).

commented: I like the way you gave +rep for the use of code tags +3

@VernonDozier : I appreciate the way you gave a +rep to someone who used code tags right from the first post, because thats really something rare to see. He deserved nothing short of it.

@madden88chw: There are some problems in your code here :

while (!date.equals("#"))
    {
    for (charCount = 0; charCount < date.length(); charCount++)
    {
      if (date.charAt(charCount) != "/")
      {
        if (date.charAt(charCount) < '0' || date.charAt(charCount) > '9')
          System.out.println("Incorrect input format");

If I have correctly understood the logic of your program here then, what you are doing is taking a date from the user then giving out the following date ( by adding 1 into the day value), you keep doing this for as many days as the user likes till he finally enters a '#' after encountering which you terminate. So basically your while loop should terminate just before the termination of the main method. But in the for loop here you are first comparing the date to be in the format that you require it to be, so you just run along the date string validating the values. So your for loop should be terminating after System.out.println("Incorrect input format"); So you are missing a '}' there.
Then the first if construct ( if (date.charAt(charCount) != "/") ) houses the second if construct ( if (date.charAt(charCount) < '0' || date.charAt(charCount) > '9') ) where you want to check whether the value, if not a '/', is a digit. So the first if construct too should end after the SOP statement and before the for loop, so put a '}' there too.

Now moving towards the end of your code

}
    )
  )
  }
}

You do not need the closing paranthesis ')' over here. The three braces '}' one each for while, main and class are just fine.

Also there is one another error in your program which you might not have noticed is in this statement. I won't tell it directly, but to give you a hint just check the docs for String.charAt

date.charAt(charCount) != "/"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.