Bukkit Coding – (8) Converting to String

At the end of the last section, I gave you a pretty hard task. I challenged you to try to return the players gamemode in chat. This is the code that you may have tried.

However, if you try this code, you’ll notice Eclipse will give you an error saying that player.sendMessage() requires a string variable. In the code above, I gave player.sendMessage() a GameMode variable. Now how do we turn a GameMode Variable into a string? We can add .toString() to the end of player.getGameMode().

This method only works for variables that contain strings though. For an example, this code WILL NOT WORK because the player variable does not contain a string.

But what if you want to convert an integer to a string? This code WILL NOT WORK either.

You’re unable to directly convert integers (numbers) into string. So how would you do this then? Well as it turns out, Java has an object type called Integers. Integers happen to have a method called toString which converts Integers into Strings. You can use it like so:

Be the first to comment

Leave a Reply

Your email address will not be published.