The Artima Developer Community
Sponsored Link

Java Answers Forum
parse a string into an array

2 replies on 1 page. Most recent reply: Mar 13, 2006 4:37 AM by joe auerbach

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

parse a string into an array Posted: Mar 12, 2006 8:24 PM
Reply to this message Reply
Advertisement
Quick assignment question (so just pointing should be sufficient).

A part of a lab i'm working on requires that I take nonformatted information from a sentance and turn it into an object.

For example, if you say, "Create a rectangle that is 12 by 15 by 32," I must locate the word rectangle and the numbers 12, 15, and 32 and use them to create that rectangle.

I'm pretty stuck here. This is the only part of the assignment that I just truly have no plan for. Odds are I should make each space separates item a piece of an ArrayList, perhaps, then scan the array for key words (cube, rectangle, whatever) and then find a specific number of doubles past that, but I can't turn that into code.

Not true, I COULD turn that into code if I could turn the sentance into an array. that would be easier. An incrementing if statment that fince the keyword and locates doubles in order. A few problems here, though.

a) I can't fogure out, for the life of me, how to turn a sentance into an array.

b) can I find a string of numbers? I can't use mathmatical operators, can I? I can't ask if a string is greater than zero effectively. Is asking if it starts with 0-9 too complicated? Am I overlooking some easy way to identify the figures?

Anyway, it's a pretty basic question, but I dedicated this weekend to preparing for this lab and this is the only part that I'm really stuck on and I'd rather not be up till four AM the day before class (as usual) trying to figure it out, so this is me asking for help in advance.

thanks.


nabakumar

Posts: 23
Nickname: nkmeitei
Registered: Jun, 2005

Re: parse a string into an array Posted: Mar 13, 2006 4:03 AM
Reply to this message Reply
I thnk for your problem the split ,method which is available in the String class will come to the rescue.
e.g. String str = "Create a rectangle that is 12 by 15 by 32"

String []splits = str.split(" ");
Here ,
splits[0] will be "Create"
splits[1] will be "a"
splits[2] will be "rectangle"
splits[3] will be "that"
and so on ....
By this you can easily fetch the values.
If u want more intelligence in your code , u can read Regular expression in this url

http://java.sun.com/j2se/1.4.2/docs/api/

But if u r using split method be careful of arrayindexoutofbounds and consecutive blank spaces .
say e.g. "Create a rectangle that is 12 by 15 by 32"
here there will be a problem with the previous split call
better use this one :
String []splits = str.split("[ ]+");
This is a regular expression ..
ok all the best .....
slam

joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

Re: parse a string into an array Posted: Mar 13, 2006 4:37 AM
Reply to this message Reply
That is an excellent idea. Thank you very much.

Flat View: This topic has 2 replies on 1 page
Topic: How do i rectify this?! Previous Topic   Next Topic Topic: Exist in Java?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use