Results 1 to 4 of 4
Thread: Trouble with exceptions
- 08-16-2012, 02:47 PM #1
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
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:
Date classJava 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()); } } }
Exception class (one of)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; } }
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"); } } }
- 08-16-2012, 04:04 PM #2
Re: Trouble with exceptions
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)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-17-2012, 05:30 AM #3
Member
- Join Date
- Aug 2012
- Location
- Tamworth, Australia
- Posts
- 42
- Rep Power
- 0
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 :)
- 08-17-2012, 07:50 AM #4
Similar Threads
-
Having trouble catching exceptions
By fatabass in forum New To JavaReplies: 2Last Post: 03-17-2012, 04:02 AM -
WOW need help with Exceptions
By starplayerrob in forum New To JavaReplies: 4Last Post: 12-12-2011, 10:49 AM -
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 09:06 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 08:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks