1 Replies - 213 Views - Last Post: 24 February 2013 - 03:26 PM Rate Topic: -----

#1 Smitty83647  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 17-April 10

Try-Catch Exception Overworking and Not Working

Posted 24 February 2013 - 03:15 PM

I've been working on a program (two files) which receives input from a user in the form of two numbers separated by the slash character "/" and then two more numbers. The program must error check the input and then produce both a numeric representation of the month/day and a spelled out month and day.

The driver for this seems fine (but provided for reference):
import java.util.Scanner;

public class SmithJeffreyDateDriver
{
	public static void main(String args[])
	{
		Scanner stdIn = new Scanner(System.in);
		String error = null;
		String dateStr = null;
		boolean repeat = true;

		// Creates a loop to continually ask for input, successful or errored
		while (repeat)
		{
			System.out.print("Enter a date in the form mm/dd ('q' to quit): ");
			dateStr = stdIn.nextLine();
			// First error check to determine if input was blank
			if (dateStr != null)
			{
				// Check to determine if user entered q to quit program
				if (!dateStr.equalsIgnoreCase("q"))
				{
					Date date = new Date(dateStr);
					error = date.getError();
					if (error == null)
					{
						// Fetches the print methods from Date.java and then prints
						// a blank line for readability
						date.monthDayNumbers();
						date.monthLtrsDayNbrs();
						System.out.println("");
					}
					// Prints the error, if one exists
					else
					System.out.println(error);
				}
				// Sets boolean value of repeat to false to stop the loop
				else
				repeat = false;
			}
			// Error check message for entering blank - no data
			else
			System.out.println("You must enter something.");
		}
	}
} // End of class SmithJeffreyDateDriver



The driven class, named Date.java--I know it's not a good idea to create your own classes the same as what's available from Sun (Oracle), but it's a requirement of the assignment.

Anyway, the error I'm getting is that the program errors no matter what you enter. Further it doesn't catch the exception if the input provided is less than 3 chareters (i.e. minimum input is 2/5).

I suspect that it has something to do with the way I've embedded the try-catch blocks, but everything I try doesn't seem to make any difference...or makes it worse.

Now I've been working on it long enough that I'm not able to see what I'm doing wrong and I'm out of ideas. Any help would be greatly appreciated.

public class Date
{
	private int month = -1;
	private int day = -1;
	private int[] monthLength = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	private String[] monthFull = {"January", "February", "March", "April", "May", "June",
	"July", "August", "September", "October", "November", "December"};
	private String error = null;
	private String errorMessage = null;

	// Date constructor with error checking
	public Date(String dateStr)
	{
		int mth1 = 0;
		int mth2 = 0;
		int day1 = 0;
		int day2 = 0;
		int dateLength = 0;

		// Creates error message if dateStr is less than minimum length
		if (dateStr == null)
		errorMessage = "0dateStr cannot be null - " + dateStr;

		else if (dateStr.length() < 3)
		errorMessage = "1Invalid date format - " + dateStr;

		else try
		{
			dateLength = dateStr.length();
			errorMessage = "2Invalid date format - " + dateStr;

			int spot = dateStr.indexOf("/");

			if (spot < 0) throw new Exception(error);
			errorMessage = "3Invalid format - " + dateStr;

			if (spot+2 <= dateLength) day1 = Integer.valueOf(dateStr.substring(spot+1, spot+2));
			else throw new Exception(errorMessage);

System.out.println("day: " + day); // test print-remove
System.out.println("day1: " + day1); // test print-remove
System.out.println("day2: " + day2); // test print-remove
System.out.println("dateStr: " + dateStr); // test print-remove

			try
			{
				if (spot+3 <= dateLength)
				{
					day2 = Integer.valueOf(dateStr.substring(spot+2, spot+3));
					day = day1 * 10 + day2;

System.out.println("day: " + day); // test print-remove
System.out.println("day1: " + day1); // test print-remove
System.out.println("day2: " + day2); // test print-remove
System.out.println("dateStr: " + dateStr); // test print-remove

				}
			}

			finally
			{
				if (day2 < 1) day = day1;
			}

			if ((day < 1) || (day > 31)) throw new Exception(error);
			{
				errorMessage = "4Invalid day - " + dateStr;
			}



System.out.println(day); // test print-remove

			//errorMessage = "4Invalid day - " + dateStr;
			int days = monthLength[month-1];

System.out.println(day); // test print-remove
System.out.println(dateStr); // test print-remove

			errorMessage = "5Invalid day - " + dateStr;
			if (day > days) throw new Exception(error);



			try
			{
				if (spot > 1)
				{
					mth2 = Integer.valueOf(dateStr.substring(spot-2, spot-1));
					month = mth1 * 10 + mth2;
				}

System.out.println("month: " + month); // test print-remove
System.out.println("mth1: " + mth1); // test print-remove
System.out.println("mth2: " + mth2); // test print-remove
System.out.println("dateStr: " + dateStr); // test print-remove

			}
			finally
			{
				if (mth1 < 1) month = mth2;
			}

			if ((month < 1) || (month > 12)) throw new Exception(error);

			errorMessage = "6Invalid month - " + dateStr;
			if (spot > 0) mth2 = Integer.valueOf(dateStr.substring(spot-1, spot));


		}

		catch (Exception e)
		{
			error = errorMessage + e.getMessage();
		}
	}

	// Print method to print month-day all in numbers
	public void monthDayNumbers()
	{
		System.out.printf("%02d/%02d\n", month, day);
	}

	// Print method to print in month-day with month spelled out and numbers
	public void monthLtrsDayNbrs()
	{
		System.out.println(monthFull[month-1]+" "+String.valueOf(day));
	}

	// getError method that returns error value after all error checking is done
	public String getError()
	{
		return error;
	}
} // End of class Date



Is This A Good Question/Topic? 0
  • +

Replies To: Try-Catch Exception Overworking and Not Working

#2 pbl  Icon User is offline

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

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

Re: Try-Catch Exception Overworking and Not Working

Posted 24 February 2013 - 03:26 PM

I am afraid you missed the whole concept :) Don't worry your are not the first one

		else try
		{
			dateLength = dateStr.length();
			errorMessage = "2Invalid date format - " + dateStr;
			int spot = dateStr.indexOf("/");
			if (spot < 0) throw new Exception(error);


you do not throw an Exception if you are to catch it yourself it just does not make sense

When a method does not know what to do and cannot handle an error it throws an Exception saying to the caller: sorry I am in a situation that I can't handle so fix it

The best example Integer.parseInt() if an invalid character is encountered, an Exception is thrown, there is nothing else parseInt() can do. So the reponsability to handle the error is returned to the caller that can: correct the error, ignor it, ... or whatever

In your case you can simply

    if (spot < 0) {
        ... do here what you have done in the catch() clause


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1