Converting a String to an Int? Is it possible?

• July 14th, 2012, 02:28 PM
Gravity Games
Converting a String to an Int? Is it possible?
Hello, Gravity Games here, and I'm working on a world map format for my game in progress. I have already made the format itself, the game reads the file just fine, but my problem is making a header. I need the header to set things that the level data itself can't set, like the maps length/width and the music that plays. Unfortunately, I have a huge problem. Even though the header is just simple numbers (like "27", or "105"), the Strings can't be converted into integers. Is there any way to convert a String to an Int, or some other way around this problem?
• July 14th, 2012, 03:09 PM
Zaphod_b
Re: Converting a String to an Int? Is it possible?
Quote:

Originally Posted by Gravity Games
...Is there any way to convert a String to an Int...

Well, strictly speaking, one doesn't actually convert a String to an int, but you can get an integer value from its String representation:
Code java:

```public class Whatever public static void main(String [] args) { String string1 = "27"; String string2 = "105"; int int1 = Integer.parseInt(string1); int int2 = Integer.parseInt(string2); int int3 = int1 + int2; System.out.println("string1 = " + string1 + ", string2 = " + string2); System.out.println("Sum of ints from string1 and string2 = " + int3); } }```

Output:
Code :

```string1 = 27, string2 = 105 Sum of ints from string1 and string2 = 132```

Reference: Java Integer Class

Cheers!

Z
• July 15th, 2012, 12:21 AM
Gravity Games
Re: Converting a String to an Int? Is it possible?
Thanks a lot Zaphod_b! This is just what I needed! Now the map attributes don't have to be hardcoded.