Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Problems With Throwing Exceptions in Program-Help Appreciated

  1. #1
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Post Problems With Throwing Exceptions in Program-Help Appreciated

    Below is the code that I have now, there are some errors but I was wondering if you guys could offer me some guidance to finalize this program so that it is not as open ended. I do not really know where to start, and I have been stuck on this program for weeks. Thank you so much!

     
     public static void main(String[] args) throws DayException {
            Scanner console = new Scanner(System.in);
            int monthValue;
            int dayValue;
            int yearValue;
            int[] daysOfMonth = {12, 31, 28, 31, 30, 31, 30, 31, 30, 31};
            final String[] monthNames
                    = new String[]{"Jan", "Feb", "March", "April", "May",
                        "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
     
            while (true) {
                // Gets date from the user.
                System.out.print("Enter a date in the format mm dd yyyy: ");
                monthValue = console.nextInt();
                dayValue = console.nextInt();
                yearValue = console.nextInt();
     
                 // Examine the month, day, year. 
                 //Value for the month, day, year that is entered.
                 // If it is not, throw an exception.  
     
                try {
                  if (monthValue < 1 && monthValue > 12) 
                      throw new MonthException();   
                }
                   catch (MonthException e) {
                     System.out.println(e.getMessage());
                }
     
                 try {
                   if (dayValue < 1 && dayValue > 31 )
                       throw new DayException();
               }
                   catch (DayException e) {
                    System.out.println(e.getMessage());
                }
     
               try {
                   if (yearValue <= 1000 && yearValue >= 3000)
                       throw new YearException();
               }
               catch (YearException e) {
                    System.out.println(e.getMessage());
     
     
                console.nextLine();  // To flush input buffer.
                System.out.print("Enter another date? (yes/no) ");
                String response = console.nextLine();
                if (!response.toUpperCase().equals("Yes")) {
                    System.exit(0);
                }
            }
     
    class MonthException extends RuntimeException {
     
        public MonthException() {
            super("Invalid month value entered. Try again.");
          }
        }
       }
     }
     
    class YearException extends RuntimeException {
     
        public YearException() {
            super("Invalid year value entered. Try again.");
            }
        }
     
    class DayException extends RuntimeException {
     
         public DayException() {
             super("Invalid day value entered. Try again.");
            }
        }
     }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    guidance to finalize this program
    Do you have any specific questions?

    One problem I see is the missing class definition and any needed import statements
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (May 12th, 2014)

  4. #3
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    Yes. I would like the program to display: "The date you entered is November 20, 1981." When you enter: mm dd yyyy: 11 20 1981 (When a correct date is entered.) So basically it converts dates from numerical month/day/year format to normal "month day, year" format.

    Then when a wrong date is entered I want it to throw an exception and state an error message.

       public static void main(String[] args) throws DayException {
            Scanner console = new Scanner(System.in);
            int monthValue;
            int dayValue;
            int yearValue;
            int[] daysOfMonth = {12, 31, 28, 31, 30, 31, 30, 31, 30, 31};
            final String[] monthNames
                    = new String[]{"Jan", "Feb", "March", "April", "May",
                        "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
     
            while (true) {
                // Gets date from the user.
                System.out.print("Enter a date in the format mm dd yyyy: ");
                monthValue = console.nextInt();
                dayValue = console.nextInt();
                yearValue = console.nextInt();
     
                 // Examine the month, day, year. 
                 //Value for the month, day, year that is entered.
                 // If it is not, throw an exception.  
     
                try {
                  if (monthValue < 1 && monthValue > 12) 
                      throw new MonthException();   
                }
                   catch (MonthException e) {
                     System.out.println(e.getMessage());
                }
     
                 try {
                   if (dayValue < 1 && dayValue > 31 )
                       throw new DayException();
               }
                   catch (DayException e) {
                    System.out.println(e.getMessage());
                }
     
               try {
                   if (yearValue <= 1000 && yearValue >= 3000)
                       throw new YearException();
               }
               catch (YearException e) {
                    System.out.println(e.getMessage());
     
     
                console.nextLine();  // To flush input buffer.
                System.out.print("Enter another date? (yes/no) ");
                String response = console.nextLine();
                if (!response.toUpperCase().equals("Yes")) {
                    System.exit(0);
                }
            }          
            }
        }
     private static class MonthException extends RuntimeException {
     
        public MonthException() {
           super("Invalid month value entered, month exceeds excepted value."
                   + "Please try again.");
            }
            }
        }
     
    class YearException extends RuntimeException {
     
        public YearException() {
            super("Values between 1000 and 3000 are only allowed,"
                    + " Please try again.");
            }
        }
     
    class DayException extends RuntimeException {
     
         public DayException() {
             super("Invalid day value entered, only accept values between 1 and 31."
                     + " Please try again.");
            }
        }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    What problems are you having making the code do what you want?

    Can you copy the contents of the command prompt window and paste it here showing the program's input and output?

    The posted code won't compile for testing because of missing class definition and import statements.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    I do not know where to start...

    this is what it displays"
    Enter a date in the format mm dd yyyy: 13 111 111
    Enter a date in the format mm dd yyyy: 22 332 232
    Enter a date in the format mm dd yyyy: "

    Whenever I enter a data set that is false, it does not throw the exception......do not know why it does not...

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    Please post code that will compile and execute for testing. The posted code will not compile.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (May 13th, 2014)

  9. #7
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    Here it is:
    Thank you for your help!

     
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
            int monthValue;
            int dayValue;
            int yearValue;
            int[] daysOfMonth = {12, 31, 28, 31, 30, 31, 30, 31, 30, 31};
            final String[] monthNames
                    = new String[]{"Jan", "Feb", "March", "April", "May",
                        "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
     
            while (true) {
                // Gets date from the user.
                System.out.print("Enter a date in the format mm dd yyyy: ");
                monthValue = console.nextInt();
                dayValue = console.nextInt();
                yearValue = console.nextInt();
     
                // Examine the month, day, year. 
                // Value for the month, day, year that is entered.
                // If it is not the range of the accepted value, throw an exception.  
     
               try { // MonthException
                   if (monthValue < 1 && monthValue > 12) 
                      throw new MonthException();   
                }
                   catch (MonthException e) {
                     System.out.println(e.getMessage());
                }
     
               try { // Day Exception
                   if (dayValue < 1 && dayValue > 31 )
                       throw new DayException();
               }
                   catch (DayException e) {
                    System.out.println(e.getMessage());
                }
     
               try { // Year Exception
                   if (yearValue <= 1000 && yearValue >= 3000)
                       throw new YearException();
               }
                   catch (YearException e) {
                    System.out.println(e.getMessage());
               }
                try { // Leap Year
                   if ((yearValue % 4 == 0) && (dayValue > 31)) 
                       throw new DateException();
                }
                   catch (DateException e) {
                    System.out.println(e.getMessage());   
                   }
     
                console.nextLine();  // To flush input buffer.
                System.out.print("Enter another date? (yes/no) ");
                String response = console.nextLine();
                if (!response.toUpperCase().equals("no")) {
                    System.exit(0);
     
            }          
            }
        }
     private static class MonthException extends RuntimeException {
     
        public MonthException() {
           super("Invalid month value entered, month exceeds excepted value."
                   + "Please try again.");
            }
            }
     
    private static class DateException extends RuntimeException {
     
        public DateException() {
            super("Entered date is a leap year.");
            }
        }
        }
     
    class YearException extends RuntimeException {
     
        public YearException() {
            super("Values between 1000 and 3000 are only allowed,"
                    + " Please try again.");
            }
        }
    class DayException extends RuntimeException {
     
         public DayException() {
             super("Invalid day value entered, only accept values between 1 and 31."
                     + " Please try again.");
            }
        }

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    Here it is:
    That code is no better than what has been posted previously. It needs import statements and a class definition.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (May 13th, 2014)

  12. #9
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    my bad, sorry. If this does not work, then I might have a bug in the program........

     
    import java.util.Scanner;
    public class W10Assign9 {
     
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
            int monthValue;
            int dayValue;
            int yearValue;
            int[] daysOfMonth = {12, 31, 28, 31, 30, 31, 30, 31, 30, 31};
            final String[] monthNames
                    = new String[]{"Jan", "Feb", "March", "April", "May",
                        "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
     
            while (true) {
                // Gets date from the user.
                System.out.print("Enter a date in the format mm dd yyyy: ");
                monthValue = console.nextInt();
                dayValue = console.nextInt();
                yearValue = console.nextInt();
     
                // Examine the month, day, year. 
                // Value for the month, day, year that is entered.
                // If it is not the range of the accepted value, throw an exception.  
     
               try { // MonthException
                   if (monthValue < 1 && monthValue > 12) 
                      throw new MonthException();   
                }
                   catch (MonthException e) {
                     System.out.println(e.getMessage());
                }
     
               try { // Day Exception
                   if (dayValue < 1 && dayValue > 31 )
                       throw new DayException();
               }
                   catch (DayException e) {
                    System.out.println(e.getMessage());
                }
     
               try { // Year Exception
                   if (yearValue <= 1000 && yearValue >= 3000)
                       throw new YearException();
               }
                   catch (YearException e) {
                    System.out.println(e.getMessage());
               }
                try { // Leap Year
                   if ((yearValue % 4 == 0) && (dayValue > 31)) 
                       throw new DateException();
                }
                   catch (DateException e) {
                    System.out.println(e.getMessage());   
                   }
     
                console.nextLine();  // To flush input buffer.
                System.out.print("Enter another date? (yes/no) ");
                String response = console.nextLine();
                if (!response.toUpperCase().equals("no")) {
                    System.exit(0);
     
            }          
            }
        }
     private static class MonthException extends RuntimeException {
     
        public MonthException() {
           super("Invalid month value entered, month exceeds excepted value."
                   + "Please try again.");
            }
            }
     
    private static class DateException extends RuntimeException {
     
        public DateException() {
            super("Entered date is a leap year.");
            }
        }
        }
     
    class YearException extends RuntimeException {
     
        public YearException() {
            super("Values between 1000 and 3000 are only allowed,"
                    + " Please try again.");
            }
        }
    class DayException extends RuntimeException {
     
         public DayException() {
             super("Invalid day value entered, only accept values between 1 and 31."
                     + " Please try again.");
            }
        }

  13. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

     if (monthValue < 1 && monthValue > 12)

    Can you think of a case where this will evaluate to true?

  14. The Following User Says Thank You to copeg For This Useful Post:

    Knowledge_Feeds_The_Mind (May 13th, 2014)

  15. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    Look at this code:
     if (monthValue < 1 && monthValue > 12)
    How many numbers are less than 1 AND also greater than 12?
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (May 13th, 2014)

  17. #12
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    I changed it to "(monthValue < 1 || monthValue > 12)" Sorry about that.
    import java.util.Scanner;
    public class W10Assign9 {
     
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
            int monthValue;
            int dayValue;
            int yearValue;
            int[] daysOfMonth = {12, 31, 28, 31, 30, 31, 30, 31, 30, 31};
            final String[] monthNames
                    = new String[]{"Jan", "Feb", "March", "April", "May",
                        "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
     
            while (true) {
                // Gets date from the user.
                System.out.print("Enter a date in the format mm dd yyyy: ");
                monthValue = console.nextInt();
                dayValue = console.nextInt();
                yearValue = console.nextInt();
     
                // Examine the month, day, year. 
                // Value for the month, day, year that is entered.
                // If it is not the range of the accepted value, throw an exception.  
     
               try { // MonthException
                   if (monthValue < 1 || monthValue > 12) 
                      throw new MonthException();   
                }
                   catch (MonthException e) {
                     System.out.println(e.getMessage());
                }
     
               try { // Day Exception
                   if (dayValue < 1 && dayValue > 31 )
                       throw new DayException();
               }
                   catch (DayException e) {
                    System.out.println(e.getMessage());
                }
     
               try { // Year Exception
                   if (yearValue <= 1000 && yearValue >= 3000)
                       throw new YearException();
               }
                   catch (YearException e) {
                    System.out.println(e.getMessage());
               }
                try { // Leap Year
                   if ((yearValue % 4 == 0) && (dayValue > 31)) 
                       throw new DateException();
                }
                   catch (DateException e) {
                    System.out.println(e.getMessage());   
                   }
     
                console.nextLine();  // To flush input buffer.
                System.out.print("Enter another date? (yes/no) ");
                String response = console.nextLine();
                if (!response.toUpperCase().equals("no")) {
                    System.exit(0);
     
            }          
            }
        }
     private static class MonthException extends RuntimeException {
     
        public MonthException() {
           super("Invalid month value entered, month exceeds excepted value."
                   + "Please try again.");
            }
            }
     
    private static class DateException extends RuntimeException {
     
        public DateException() {
            super("Entered date is a leap year.");
            }
        }
        }
     
    class YearException extends RuntimeException {
     
        public YearException() {
            super("Values between 1000 and 3000 are only allowed,"
                    + " Please try again.");
            }
        }
    class DayException extends RuntimeException {
     
         public DayException() {
             super("Invalid day value entered, only accept values between 1 and 31."
                     + " Please try again.");
            }
        }

  18. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    Does is work now?
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (May 14th, 2014)

  20. #14
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    No. It still does not convert the date that I input, sigh, this is really troublesome it still give me this
    "Enter a date in the format mm dd yyyy: 12 11 1995, Enter another date? (yes/no) no"

    Thank you!

  21. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    It still does not convert the date that I input
    What do you want the program to output? Can you post an example of the desired output?

    Where in the code is this conversion being done and results being printed?
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (May 14th, 2014)

  23. #16
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    FYI, the logic correction pointed out above applies to all logic (check your other if statements in the last code posted in post 12, which I presume is the current working copy)

  24. The Following User Says Thank You to copeg For This Useful Post:

    Knowledge_Feeds_The_Mind (May 14th, 2014)

  25. #17
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    This is what the output should look like:

    "Please enter a date in the format mm dd yyyy: 12 99 2013
    The day value is dubious. Please try again.

    Please enter a date in the format mm dd yyyy: 44 2 2012
    That month is incorrect. Please try again.

    Please enter a date in the format mm dd yyyy: 11 20 1981
    The date you entered is November 20, 1981. "

    What sort of conversion has to be done and where should I put it?

  26. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    Take a look at the Date class and the SimpleDateFormat class for how to format a date into a String.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (May 14th, 2014)

  28. #19
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problems With Throwing Exceptions in Program-Help Appreciated

    what is ur question ??? plz tell me clearly i promise u to solve ur problem up to my maximum extend.

Similar Threads

  1. I need help with fixing the exceptions in my Program.
    By ShafeeBhula in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 28th, 2013, 08:35 PM
  2. [SOLVED] My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();
    By Chromejuice in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 10:52 PM
  3. problems running recursion, any help would be grealty appreciated!
    By local127001 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2012, 01:17 PM
  4. problems with exceptions
    By gerry123 in forum Exceptions
    Replies: 6
    Last Post: July 23rd, 2011, 07:00 PM
  5. nextLine problems, any help appreciated
    By Schmitz14 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 5th, 2009, 01:23 AM