Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine if String entered by user is in the proper format
    text
    copied!<p>I'm trying to check if a String is entered by the user in a specific format. I cannot use SimpleDateFormat, catch, split or anything along those lines since I have not learned it. I'm at a very novice level and have learned how to use and manipulate Strings. The user is to enter mm/dd/yyyy (I'm using JOptionPane) so it is a String. I've pulled the month, day, and year as sub-strings (and converted to integers for another use). However, everything goes wrong if it is entered m/dd/yy. Since people can't be trusted to follow directions, I need something to catch incorrect entries. I have learned about Gregorian Calendar, but not sure that will help. In the end I need to use arrays to turn mm/dd/yyyy to MMM/dd/yyyy. So I was going to first check that the entry was valid, then convert to integers and use the number to coincide with the month array, then make sure the day is within the number of days in the month. Finally, I need to give the day in the year it is and that is why I parsed(?) to integers. Everything else works as long as the user enters it properly. Feel free to suggest any other edits. I did try to use an object, but had a very difficult time with it so changed to a parallel array. </p> <pre><code>import javax.swing.*; import java.util.*; public class ConvertDate { public static void main(String[] agrs) { int numOfDay; String date, mnthName = ""; int monthNumber, dayNumber, yearNumber; String month = "", day = "", year = ""; int validDays = 31; int monthNum[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; String[] monthName = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int[] daysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; date = JOptionPane.showInputDialog(null, "Please enter a date. Use format mm/dd/yyy"); month = date.substring(0, 2); day = date.substring(3, 5); year = date.substring(6, 10); yearNumber = Integer.parseInt(year); monthNumber = Integer.parseInt(month); dayNumber = Integer.parseInt(day); for(int x = 0; x &lt; 12; x++) { if(monthNumber == monthNum[x]) { mnthName = monthName[x]; validDays = daysInMonth[x]; } } GregorianCalendar dayOfYear = new GregorianCalendar(yearNumber, (monthNumber - 1), dayNumber); numOfDay = dayOfYear.get(GregorianCalendar.DAY_OF_YEAR); if(dayNumber &gt; validDays || dayNumber &lt; 0 || monthNumber &lt; 0 || monthNumber &gt; 12) // if(!date.equals("**/**/****")) does not work, nor if(!date.equals(**"/"**"/"****)) JOptionPane.showMessageDialog(null, "Invalid date or invalid format."); else JOptionPane.showMessageDialog(null, mnthName + " " + dayNumber + ", " + year + " is \nthe " + numOfDay + " day of the year."); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload