Results 1 to 14 of 14

Thread: Seperating a string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    80

    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

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    80
    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

  4. #4
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    80
    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

  6. #6
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    80
    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

  8. #8
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    System.gc()

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11
    New Member
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    3
    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.
    Mind over metal

  12. #12
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Ah yes, didn't see that *slap*. Should've been obvious.

    HD

  13. #13

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    80
    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.

  14. #14
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yeah you could just release the refrence.Sorry i have to say(but i havent been on hear in a while and im bored) (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)

    As for the way particular languages index blocks of computer memory doesnt Visual Basic index arrays different?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width