Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,109 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,031 people online right now. Registration is fast and FREE... Join Now!




Converting Between String and Integer

 
Reply to this topicStart new topic

> Converting Between String and Integer

DillonSalsman
Group Icon



post 23 Dec, 2008 - 01:54 PM
Post #1


Lets pretend you are doing a math word problem where all of the words are written out like this:
CODE

If two rabbits produce four rabbits per week, how many rabbits will there be after ten weeks?

Well when you go to do the math, you automatically convert those words (Strings) into numbers (int, double, etc.)
However your computer isn't smart enough to do that yet, so you need to help it!
Lets pretend, for some reason your computer wants you to enter your name and age (in years) at the command line so it can return how old you are in seconds.
CODE

public class HowOldAreYou
{
    public static void main(String[] args)
    {
        //confirm that both name and age are supplied.
        if(args[1] == null)
        {   System.out.println("Please enter your first name and age.");
            System.exit(1);
        }
        //No conversion needed here.  Our main function recieves an array
        //of Strings named args and thats what we are using.
        String name = args[0];
        //uh-oh.. how do we use the String as a number that we can use to
        //calculate how many second we are? Like this!
        int ageInYears = Integer.valueOf(args[1]);
        int ageInSeconds = ageInYears * 365 * 24 * 60 * 60;
        System.out.println("You are " + ageInSeconds + " seconds old.  Don't your feel ancient?");
    }
}


You can also convert between the different number types, but using an even easier method!
CODE

int fifteen = 15;
char fifteenToo = (char) fifteen;
double heyMeToo = (double) fifteenToo;
float dontForgetMe = (float) heyMeToo;


So there you go.

PS: I made this mostly because I just reviewed casting and what not because I posted incorrectly on how to convert an integer into a string.
You use:
CODE

String string = "34";
int number = Integer.parseInt(string);
int numberToo = Integer.valueOf(string);

Not:
CODE

Integer.getInteger(string)
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

BetaWar
Group Icon



post 2 Jan, 2009 - 09:43 PM
Post #2
Hope you don't mind, but as the tutorial really only shows how to convert between a String and Integer (which are both complex dataTypes -- They have a class running them) I changed the title to better reflect the content.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 12:48PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month