tags:

views:

321

answers:

4

What function is to be used in java so as to print the integer value of a string.
Please explain, maybe with the help of an example.

Example String:

String str = new String("HELLO");
+3  A: 

If you want to find say the ASCII integer representation of an entire string and print that out you could use the String.charAt(..) method.

public class test 
{
   public static void main( String argv[] )
   {
      String str = new String("HELLO");
      int age = 0; 
      for( int i = 0; i < str.length(); i++ ) {  // Loop through the string 
        age += (int) str.charAt(i);  // Add the current character's int representation to the age. 
        System.out.println( "Character: " + str.charAt(i) + " Integer value: " + (int)str.charAt(i) );
      }

      System.out.println( "Age: " + age );
   }
}

Corrisponding output:

Character: H Integer value: 72
Character: E Integer value: 69
Character: L Integer value: 76
Character: L Integer value: 76
Character: O Integer value: 79
Age: 372
Brian Gianforcaro
its giving a runtime error. do i have to import some library
No, it's probably throwing a NumberFormatException. Can you post details about an example string you are trying to parse? Also check mk's answer and see if that works for you.
Brian Gianforcaro
i am trying to get the integer value of the string "HELLO"
Ok so you HELLO actually doesn't contain any integers [1-9] so it's always going to throw that exception. It sounds like you don't want to know the actual value of a integer inside of a string. But you want to know the integer value of every character in a string? If so then try the second method I listed.
Brian Gianforcaro
hey i did not get ur last comment. can you explain in detail.I am giving the question which i need to solve:Convert a string to an integer and store the converted value in integer variable age.
I updated the answer with what I think you are trying to do?
Brian Gianforcaro
thnks. act i had the confusion whether the integer of hello is equal to the sum of the integers of each character
+3  A: 

You mean convert a String into an integer?

You can do:

int val = Integer.parseInt(str);
Chi
+1  A: 

One of the two functions:

Integer.valueOf(num); //returns Integer (Object)
Integer.parseInt(num); //returns int (primitive)

are what you want. Docs: parseInt, valueOf. However, it may often throw an exception, which you may not want. I often use:

/** returns int value or null if invalid */
public static Integer intOf(String num) {
 if (num == null) return null;
 try {
  return Integer.valueOf(num);
 } catch (NumberFormatException e) {
  return null;
 }
}
mk
with the help of above function i can get the corresponding value of a character only. how will it work for a string???
The string "hello" (see your question) has no corresponding numeric value. Unless you mean: "hello".hashCode()
mk
A: 

Try

String inputStr = "the age is 44";

String patternStr = "[0-9]+"; //or "([0-9]+)" ?
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();

if (matchFound) {
    // System.out.println(matcher.groupCount());
    return matcher.group(0);
} else {
    return null;
}

(from http://www.exampledepot.com/egs/java.util.regex/Group.html )

mk