Results 1 to 7 of 7

Thread: Help with indexOf() and substring()

  1. #1
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with indexOf() and substring()

    I have to make a program that calculates the volume of a circle and then tells me the number of numbers to the left and right of the decimal. I was told to use indexOf and substring to do this. (i.e 23.456 - 2 numbers to the left and 3 to the right). How would i go about doing this? I cannot find a tutorial to show me how to use it in this way.

    thanks,
    craig

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Firstly, convert the float to a string:
    Code:
    String num = (String)myfloat;
    Then, use indexOf() to find the position of the decimal point, which is one less than the length of the string before that point:
    Code:
    int placesBefore = num.indexOf((int)('.')) + 1;
    Finally, take that away from the length of the string to obtain the number of characters after the point:
    Code:
    int placesAfter = num.length() - placesBefore;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I got it working tho not exactly the way you did it. Can u explain to me what indexOf does and why i might of needed substring?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, it's been a while since I did Java, and I've no idea where that +1 came from. It should actually look like:
    Code:
    String num = (new Float(myfloat)).toString();
    int placesBefore = num.indexOf((int)'.'),
      placesAfter = num.length() - placesBefore - 1;
    Can u explain to me what indexOf does
    Read the API documentation for java.lang.String.
    and why i might of needed substring?
    I'm not entirely sure. It's possible that you were expected to do:
    Code:
    String beforeDecimal = substring(0, placesBefore);
    int afterDecimal = substring(placesBefore + 1);
    int placesAfter = afterDecimal.length();
    ... but that's a terribly inefficient way of doing it. To accomplish the same effect (but this is still inferior if it's only necessary to find the number of places), one could do:
    Code:
    String[] decimalSides = num.split("\.");
    int beforeDecimal = decimalSides[0],
      afterDecimal = decimalSides[1];
    Last edited by Twey; 01-24-2007 at 03:16 PM. Reason: Logic errors.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok thanks. But you were kinda of right about the -1. I had to use it on the after.length() - before - 1 because it it counting the decimal and i dont need it to count that.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Argh, yes, of course. I guess I ought to test my code before posting when I haven't slept for a few days
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Feb 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ya, indexOf basically counts from the start of a string as 0
    where as length starts at one (I belive).

    So
    String Hi = "hello"

    Hi.length() = 5
    hi.subString(1, 2) = "el"
    and Hi.indexOf("H") = 0
    charAt() might also be a useful function for you it gets the Char at a particular position, but you might want to cast the output to a more handy type other than char
    hi.charAt(1) = e
    there are also things like

    strin.split(".");
    //which speaks for its self
    Last edited by Neural Network; 02-08-2007 at 09:54 PM.

Bookmarks

Posting Permissions

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