Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Converting a String to an Int? Is it possible?

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default 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?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Converting a String to an Int? Is it possible?

    Quote Originally Posted by Gravity Games View Post
    ...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:
    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:
    string1 = 27, string2 = 105
    Sum of ints from string1 and string2 = 132


    Reference: Java Integer Class



    Cheers!


    Z
    Last edited by Zaphod_b; July 14th, 2012 at 02:14 PM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Gravity Games (July 14th, 2012)

  4. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default 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.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

Similar Threads

  1. [SOLVED] Issue with converting a string to a date
    By dave_nj in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 2nd, 2012, 02:14 PM
  2. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  3. Help with code for converting 4 digit string to integer
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 09:38 PM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. Converting to String
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 13th, 2010, 06:09 PM