Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

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


(!)

Baronwyn's Avatar
Baronwyn Baronwyn is offline
Computer Specs
Member with 9 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Beginner
08-Jul-2012, 12:58 PM #1
Solved: Java -- how to get substring from a string of numbers with spaces
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.
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,255 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
10-Jul-2012, 09:52 AM #2
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.

Last edited by Ent; 10-Jul-2012 at 10:00 AM..
Baronwyn's Avatar
Baronwyn Baronwyn is offline
Computer Specs
Member with 9 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Beginner
10-Jul-2012, 12:44 PM #3
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.
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,255 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
10-Jul-2012, 12:47 PM #4
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

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


Tags
1955, index out of range, java, substring

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑