[JAVA]I need 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,
Andy
Re: [JAVA]I need help with indexOf() and substring()
You should 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 so
Code:
int placesBefore = num.indexOf((int)('.')) + 1;
and then take that away from the length of the string to obtain the number of characters after the point
Code:
int placesAfter = num.length() - placesBefore;
Re: [JAVA]I need help with indexOf() and substring()
I got it workin tho not exactly the way you did it. Can u explain to me what indexOf does and why i might of needed substring?
Re: [JAVA]I need help with indexOf() and substring()
I've have no idea why I put the +1 in there, sorry about that one. It should actually look like
Code:
String num = (new Float(myfloat)).toString();
int placesBefore = num.indexOf((int)'.'),
placesAfter = num.length() - placesBefore - 1;
[edit]
'Can u explain to me what indexOf does' - Read API (http://java.sun.com/j2se/1.4.2/docs/...ng/String.html) tut for java.lang.String
'and why i might of needed substring?' - I'm not entirely sure, but I think that you were expected to do
Code:
String beforeDecimal = substring(0, placesBefore);
int afterDecimal = substring(placesBefore + 1);
int placesAfter = afterDecimal.length();
or you could do
Code:
String[] decimalSides = num.split("\.");
int beforeDecimal = decimalSides[0],
afterDecimal = decimalSides[1];
which would be probably better.
Re: [JAVA]I need help with indexOf() and substring()
Ok thanks 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 lol
Re: [JAVA]I need help with indexOf() and substring()
I suppose I should test my stuff I post before posting :tongue:
Re: [JAVA]I need help with indexOf() and substring()
I think i got it now thanks for this.
Code:
class Andy
{
void Andy()
{
}
Andy()
{
}
}
I know Andy() is a constructor, but the class Andy is not right?
Re: [JAVA]I need help with indexOf() and substring()
Yeah, the second Andy() is a constructor, but the class isn't a constructor. A constructor is a method that sets up an instance of its class to be ready for use.
Re: [JAVA]I need help with indexOf() and substring()
SO it has 2 constructors right?
I read somethign about what the void meant and didnt really understand it, can you try and explain it to me?
Re: [JAVA]I need help with indexOf() and substring()
Quote:
Originally Posted by
Andy88
SO it has 2 constructors right?
No, only one constructor. The other method has a return type, so it's just a standard instance method.
Quote:
Originally Posted by
Andy88
I read somethign about what the void meant and didnt really understand it, can you try and explain it to me?
It's the return type declared when a method doesn't return a value.