|
-
Oct 15th, 2002, 10:07 AM
#1
Thread Starter
Lively Member
Seperating a string
Here is my predicament i want to take the values out of a string and put them into variables. Ex.
(9.2, 8)
I want to put 9.2 in a variable x and 8 in a variable y.
How would i go about doing that?
Thanks
-
Oct 15th, 2002, 10:25 AM
#2
Well, you've got a few choices. You can use the substring method (its been a while, I forget if its substring, or substr) of String, and parse the number values using the appropriate Number classes (Double probably for the number with a decimal, and Integer for the whole number).
Or you can use a StringTokenizer if you are expecting a variable number of values.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 15th, 2002, 10:33 AM
#3
Thread Starter
Lively Member
I have tried the string tokenizer bit but i can't quite get the information that i need out of it. Let me look into substring. Ill try it and get back with you.
Thanks
-
Oct 15th, 2002, 11:31 AM
#4
Addicted Member
StringTokeniser is the best way.
Declare the StringTokeniser -
String str = "(9.2, 8)"; // Whatever
StringTokeniser strTok = new StringTokeniser(str, ",");
Something like that. You can then loop through. I've found it's more useful.
String.substring is useful but you have to know how long the substrings are.
E.g. String str = "(9.2, 8)";
to get 9.2 - String ninepointtwo = str.substring(1, 4);
to get 8 - String eight = str.substring(6, 7);
It's not as useful if you don't know the indexes.
HD
-
Oct 15th, 2002, 02:34 PM
#5
Thread Starter
Lively Member
Hey that works. I didn't know that you could choose how you could tokenize(if that is even correct). Like with a comma.
Now i have to figure out how to cut off the parenthesis.
Thanks
-
Oct 16th, 2002, 05:38 AM
#6
Addicted Member
Use the substring method. Thats not a problem now because the parentheses are always first and last (I assume). Therefore:
String withoutParentheses = oldString.substring(1,(oldString.length-1));
Or something like that - my Java is playing up at the moment so I can't actually test it.
Regards HD
-
Oct 16th, 2002, 08:57 AM
#7
Thread Starter
Lively Member
Thanks i figured it out. You guys are great.
Here is another one for you. How do you reinitialize an array?
How do you delete it or put null values in it?
I thought this would work but boy was i wrong.
for(int numC = 1; numC<x.length; numC++)
{ x[numC]=null
y[numC]=null;
side[numC]=null;
}
it is an array of type double.
Thanks
-
Oct 16th, 2002, 10:21 AM
#8
Addicted Member
Java, due to its garbage collection should allow you to just reallocate the array - or you could just make the array equal to null??
I'm not sure but:
double[] a = new double[12];
// Do some stuff
// Want to reinitialise it
a = new double[3];
This is fine I think.
Obviously if you want to delete it:
a = null;
Would work.
Regards
HD
-
Oct 16th, 2002, 11:24 AM
#9
And I think the System class has a garbageCollect method you can call to force a collection, if you want to free the memory immediatly.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 16th, 2002, 02:12 PM
#10
System.gc()
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 16th, 2002, 05:15 PM
#11
New Member
Originally posted by Magnus_Lei
Thanks i figured it out. You guys are great.
Here is another one for you. How do you reinitialize an array?
How do you delete it or put null values in it?
I thought this would work but boy was i wrong.
for(int numC = 1; numC<x.length; numC++)
{ x[numC]=null
y[numC]=null;
side[numC]=null;
}
it is an array of type double.
Thanks
I am pretty sure the solution Hairy Dave suggested works, but if you are wondering what is wrong with your code, the problem here is your loop for(int numC = 1; numC<x.length; numC++)
The array in java start with their first element at location 0, so start your loop at int numC = 0, if you start at 1 you will get an ArrayOutofBoundsException.
-
Oct 17th, 2002, 03:28 AM
#12
Addicted Member
Ah yes, didn't see that *slap*. Should've been obvious.
HD
-
Oct 17th, 2002, 08:15 AM
#13
Thread Starter
Lively Member
Actually i never got a bounds out of array error. I got an incompatible type error. It's cool i got it though. Took me a while.
The code worked out good. The goal was to create an n-sided polygon and stop when i returned to the first point.
It was very interesting trying to figure it out.
-
Oct 19th, 2002, 12:15 AM
#14
Dazed Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|