Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3240722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:07:03+00:00 2026-05-17T18:07:03+00:00

Hey, This is just a simple exercise from class that I decided to have

  • 0

Hey, This is just a simple exercise from class that I decided to have a go at putting an exception in. According to the book input for this problem is supposed to be in the following format: APRIL 19, 2009 What I’m trying to do with my exception is make sure the user(whoever grades it) follows those parameters so my program works. Does this look ok? Could I have done anything better?

Edit: Thanks for the timely answers! I know I have a lot to learn but hopefully one day I’ll be capable enough to answer questions on here. cheers

import jpb.*;

public class ConvertDate {
    public static void main(String[] args) {
        try{
        SimpleIO.prompt("Enter date to be converted: ");
        String bam = SimpleIO.readLine();
        bam = bam.trim().toLowerCase();
        //empty space is taken off both ends and the entirety is put in lower case
        int index1 = bam.indexOf(" ");
        int index2 = bam.lastIndexOf(" ");
        int index3 = bam.indexOf(",");
        /*critical points in the original string are located using indexing to divide and conquer
        in the next step*/
        String month = bam.substring(0,index1);
        String up = month.substring(0, 1).toUpperCase();
        String rest = month.substring(1,index1);
        String day = bam.substring(index1, index3).trim();
        String year = bam.substring(index2+1);
        //now all the pieces are labeled and in their correct cases
        System.out.println("Converted date: " +day +" " +up +rest +" " +year);
        //everything comes together so perfectly
        } catch(StringIndexOutOfBoundsException e){
            System.out.println("best type that in like the book does on page 125...");}
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:07:04+00:00Added an answer on May 17, 2026 at 6:07 pm

    Here are a few thoughts. These are just my opinion, so take them with a grain of salt or ignore completely if you wish:

    1. I don’t know what that import statement is doing, but it’s possible to do all this without introducing the dependency on the library. You should be aware of the effect that dependencies have.
    2. I hate the kind of comments that you’ve added. I know that students and professors love them, but as a professional I find that they’re less informative than the code itself and only add clutter.
    3. I would not put all this logic into a main method. I’d encapsulate it into a class or static method so I could reuse it. It’s a useless student exercise as written.
    4. You tested the “blue sky” happy path, where everything works. I’d recommend looking at something like JUnit and start thinking about edge and exception cases. Try more inputs that test to break your method. It’ll improve that way. Your current method guarantees that your code will bonk with other cases that are easy to think of (e.g., passing in a string that isn’t month-day-year).

    Here’s how I might write the same thing:

    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.text.ParseException;
    
    public class ConvertDate 
    {
        private static final DateFormat DEFAULT_FORMAT;
    
        static
        {
            DEFAULT_FORMAT = new SimpleDateFormat("MM-dd-yyyy");
            DEFAULT_FORMAT.setLenient(false);
        }
    
        public static void main(String[] args) 
        {
            for (String dateString : args)
            {
                try
                {
                    Date date = DEFAULT_FORMAT.parse(dateString);
                    System.out.println(date);
                }
                catch (ParseException e)
                {
                    System.out.println("invalid date string: " + dateString);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey i have this div that shows as a popup: <div class=popup> </div> Then
Hey. I have an object that has a string property called BackgroundColor. This string
I have a string of test like this: <customtag>hey</customtag> I want to use a
Hey guys. This is a very simple question, I'm sure, but I'm getting myself
Hey guys. I've just published my first Android application to show this guy who
Hey, I'm learning C# and just thought of a cool idea. Considering that a
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html I noticed that in some
Hey, I stumbled upon this site looking for solutions for event overlaps in mySQL
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all, I'm pulling my hair out on this one. I've checked all my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.