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
Printable View
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
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.
:)
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
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
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
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
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
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
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.
System.gc()
:)
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++)Quote:
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
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.
Ah yes, didn't see that *slap*. Should've been obvious.
HD
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.
Yeah you could just release the refrence.Sorry i have to say(but i havent been on hear in a while and im bored) :p (A refrence is a handle to an object which is created and stored in memory.) So setting to null would work fine but you wouldnt have a good indication of when the runtime would actually release your stuff.(Yeah thats a real technical term) :rolleyes: :D
As for the way particular languages index blocks of computer memory doesnt Visual Basic index arrays different?