1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Java -- how to get substring from a string of numbers with spaces

Discussion in 'Software Development' started by Baronwyn, Jul 8, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. Baronwyn

    Baronwyn Thread Starter

    Joined:
    Jul 6, 2012
    Messages:
    9
    Rank beginner at Java. Need to pull out the month, day, and year separately from an integer. Here's what I have done. It's compiling now (that's an improvement!), but gives me a "String index out of range" error.

    Since the spaces are causing trouble, I believe, the error throws on the indicated line -- as soon as the code runs into the first space. How can I do extract this information? The format of the birthdate, with spaces, cannot change.

    //Get the date of birth.
    System.out.println("Please enter the renter's date of birth (mm dd yyyy): ");
    birthMonth = kb.nextInt();
    String birthDateString = Integer.toString(birthMonth);
    String birthMonthString = birthDateString.substring(0,1);
    String birthDayString = birthDateString.substring(3,4); //error throws on this line
    String birthYearString = birthDateString.substring(6,9);
    birthMonth = Integer.parseInt(birthMonthString);
    birthDay = Integer.parseInt(birthDayString);
    birthYear = Integer.parseInt(birthYearString);


    Thanks.
     
  2. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,409
    You've marked this solved, but no responses have been given. Can you please confirm that you've found the solution yourself, or else click "mark unsolved" to alert helpers that assistance may still be needed.

    Edit: Or, I suppose I could just look at the next thread in the forum and see for myself. :D
     
  3. Baronwyn

    Baronwyn Thread Starter

    Joined:
    Jul 6, 2012
    Messages:
    9
    I marked it solved because I solved it. Is that the wrong protocol on this site? My apologies. I wanted to close it out so no one would waste time on it.

    Yes, I kept going. Problems I can't solve and programs that don't work are annoying! I can't let them go till I figure it out. (Eventually I did figure this one out. It's done now. Time to go over to the other thread and close that out too, if I didn't already do that last night.)

    Thanks.
     
  4. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,409
    No, you did the right thing.
    It's just that every so often you get someone who clicks it by accident and then wonders why no-one comes to help. I just find it's easier to check.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1060151