Results 1 to 4 of 4
  1. #1
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Trouble with exceptions

    Having problems with runnning the exceptions in my program. Can someone please explain if I have done this corrrectly.

    Here is my Main:
    Java Code:
    import java.util.Scanner;
    
    public class DateTester{
    	public static void main(String[] args) 
    	{
    		//The String variable to store user input. 
    		String input;
    		//int x=1;
    
    
    			try
    			{
    				//Get the user input from the keyboard
    				Scanner keyboard = new Scanner(System.in);
    				//Prompt the user to enter a date in the format: MM/DD/YYYY. This will then use delimiter ("/") we created in Date class.
    				System.out.println("Enter date to parse (MM/DD/YYYY format):");
    				input = keyboard.nextLine();
    				//Object created to reference the Date class. Holds Private variables for: Month, Day, & Year
    				Date dc = new Date(input);
    				//Print the date to the screen
    				System.out.println("The date is: " + input);
    				System.out.println("The month is: " + dc.getMonth());
    				System.out.println("The day is: " + dc.getDay());
    				System.out.println("The year is: " + dc.getYear());
    				//x=2;
    			}
    			catch (IllegalArgumentException e)
    			{
    				System.out.println(e.getMessage());
    			}
    	}
    }
    Date class
    Java Code:
    import java.util.StringTokenizer;
    
    /**Get the month, and return the  field**/
    public class Date
    {
    	private String month;  //Variable to hold the value for month.
    	private String day;  //Variable to hold the value for day.
    	private String year;  //Variable to hold the value for year.
    	
    	//create the month constructor
    	public Date(String dateStr)
    	{
    		/**	this.month = month;
    		*	this.day = day;
    		*	this.year = year;**/
    		//Create a string tokenizer object.
    		//identify the delimeter for the string as "/"
    		StringTokenizer strTokenizer = new StringTokenizer(dateStr, "/");
    
    		//Extract the month token
    		month = strTokenizer.nextToken();
    		//Extract the day token
    		day = strTokenizer.nextToken();
    		//Extract the year token
    		year = strTokenizer.nextToken();
    		
    	}
    
    
    	/*************************************************
    	**		getMonth, getDay, & getYear Methods		**
    	*************************************************/
    	
    	//getMonth Method and return the month field
    		public String getMonth()
    		{
    			return month;
    		}
    		
    	//getDay Method and return the day field
    		public String getDay()
    		{
    			return day;
    		}
    		
    	//getYear Method and return the year field
    		public String getYear()
    		{
    			return year;
    		}
    }
    Exception class (one of)
    Java Code:
    public class DayException(String dateStr)	
    {
    	public int DayException (String day){//Create the if statement pseudo code
    		if (day < 1 && day > 31){
    			throw new IllegalArgumentException ("Invalid Day. Enter again");
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,089
    Rep Power
    17

    Default Re: Trouble with exceptions

    Quote Originally Posted by gnarkill10 View Post
    Exception class (one of)
    Java Code:
    public class DayException(String dateStr)	
    {
    	public int DayException (String day){//Create the if statement pseudo code
    		if (day < 1 && day > 31){
    			throw new IllegalArgumentException ("Invalid Day. Enter again");
    		}
    	}
    }
    If you think that's valid Java code, you need to go back to the fundamental tutorials.
    Trail: Learning the Java Language (The Java Tutorials)

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

  3. #3
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Trouble with exceptions

    No I do not think it is valid code. Why would I post it here if it was valid code, if I had the answer I wouldn't need your help.

    My main problem I think is getting the exception classes to be throw to my main method. Thank you for your help :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,089
    Rep Power
    17

    Default Re: Trouble with exceptions

    You're putting the cart before the horse. Go through the tutorial trail I linked. All of it.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Having trouble catching exceptions
    By fatabass in forum New To Java
    Replies: 2
    Last Post: 03-17-2012, 04:02 AM
  2. WOW need help with Exceptions
    By starplayerrob in forum New To Java
    Replies: 4
    Last Post: 12-12-2011, 10:49 AM
  3. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 09:06 PM
  4. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •