GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2009, 07:44
yjy0079 yjy0079 is offline
New Member
 
Join Date: Oct 2009
Posts: 1
yjy0079 is on a distinguished road

Convert string to integer


how do you convert string to integer?
  #2  
Old 29-Oct-2009, 09:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Convert string to integer


Quote:
Originally Posted by yjy0079
how do you convert string to integer?
First of all you need to be precise about terminology. It's important:


In java, there is a String class (giving rise to String objects). It's String, not string.

Now, an int is a Java primitive (suitable for easy integer calculations), but an Integer is a class (giving rise to Integer objects---suitable for lots of things). Integer objects, are immutable. The value of an Integer object is determined when the object is created, and can not be changed (You can use the int value of an Integer in an expression to create a new Integer, but you can't change an Integer object itself.)

So: Do you want to convert a Java String to an int primitive or to a Integer object? How are you going to use the result of the conversion?

Some examples:

JAVA Code:
//
//  StringToInt.java
//

public class StringToInt {
  //public static void main(String[] args) {
  public static void main(String[] args) {

    // Here is a way to create a String object and set its value.
    //
    String aString = "42";

  //
  // A: Two ways to convert a Java String object to a Java Integer object
  //
  
    // 1: Use the constructor for a Java Integer object
    //
    Integer iInt=new Integer(aString);
    System.out.println("1:  iInt = " + iInt);
  
    // 2: Use the valueOf method of the Integer class.
    //
    Integer iiInt = Integer.valueOf(aString);
    System.out.println("2: iiInt = " + iiInt);

    // The following will ***NOT*** work!
    //    iiInt = iiInt + 1;
    //
    // Here is one way to effectively increment the int value of an Integer object
    //
    iiInt = new Integer(iiInt.intValue() + 1);
    System.out.println("3: iiInt = " + iiInt + '\n');
  

  //
  // B: Two ways to convert a Java String object to an int
  //
  
    // 1: Use parseInt
    //
    int i = Integer.parseInt(aString);
    System.out.println("4:  i    = " + i);
  
    // 2: Get the Integer value of the string, and convert
    // to an int with intValue() method of the java Integer class
    //
    int ii = Integer.valueOf(aString).intValue();
    System.out.println("5: ii    = " + ii);

    //
    // It's easy to increment an int
    // You could also write
    //    ++ii;
    // or
    //    ii++;
    ii = ii + 1;
    System.out.println("6: ii    = " + ii);

  }
}

Output:
Code:
1: iInt = 42 2: iiInt = 42 3: iiInt = 43 4: i = 42 5: ii = 42 6: ii = 43

Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.