Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[JAVA]I need help with indexOf() and substring()

Newbie Spellweaver
Joined
Aug 9, 2008
Messages
8
Reaction score
0
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
 
Joined
Dec 1, 2007
Messages
2,795
Reaction score
480
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;
 
Newbie Spellweaver
Joined
Aug 9, 2008
Messages
8
Reaction score
0
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?
 
Joined
Dec 1, 2007
Messages
2,795
Reaction score
480
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 ( ) 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.
 
Last edited:
Newbie Spellweaver
Joined
Aug 9, 2008
Messages
8
Reaction score
0
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
 
Newbie Spellweaver
Joined
Aug 9, 2008
Messages
8
Reaction score
0
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?
 
Newbie Spellweaver
Joined
Aug 9, 2008
Messages
8
Reaction score
0
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?
 
Joined
Dec 1, 2007
Messages
2,795
Reaction score
480
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.

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.
 
Back
Top