# converting strings to integers

Page 1 of 1

## 6 Replies - 928 Views - Last Post: 13 July 2009 - 04:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=114426&amp;s=72d5f2fc287ee80839d531a29e7adf2e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hlln

Reputation: 5
• Posts: 89
• Joined: 12-July 09

# converting strings to integers

Posted 12 July 2009 - 09:52 PM

How do you convert a string to a integer
[code]
out.print("Enter a time");
String s = scan.next();
char a = s.charAt(0);
char b = s.charAt(1);
int c = (int)a;
int d = (int)b;
int e = a*10;
int f = b*10;
[code/]
A person inputs number in a string
how bout it is 10
then how can i make the string multiply or to carry out calculations
So how do you put that 10 in a string and make it into an int so i can use calculations with it?
thank you

Is This A Good Question/Topic? 0

## Replies To: converting strings to integers

### #2 syfran

• D.I.C Lover

Reputation: 83
• Posts: 1,103
• Joined: 12-July 09

## Re: converting strings to integers

Posted 12 July 2009 - 10:16 PM

hlln, on 12 Jul, 2009 - 08:52 PM, said:

How do you convert a string to a integer

out.print("Enter a time");
String s = scan.next();
char a = s.charAt(0);
char b = s.charAt(1);
int c = (int)a;
int d = (int)b;
int e = a*10;
int f = b*10;

A person inputs number in a string
how bout it is 10
then how can i make the string multiply or to carry out calculations
So how do you put that 10 in a string and make it into an int so i can use calculations with it?
thank you

```try{
int intVersion = Integer.parseInt(stringVersion);
} catch(NumberFormatException e) {
// Code to execute in the event that stringVersion isn't actually a Integer.
}

```

Where intVersion is the integer that stringVersion represents.

This post has been edited by syfran: 12 July 2009 - 10:17 PM

### #3 jpav

Reputation: 3
• Posts: 29
• Joined: 08-July 09

## Re: converting strings to integers

Posted 12 July 2009 - 10:17 PM

hlln, on 12 Jul, 2009 - 08:52 PM, said:

How do you convert a string to a integer

Look at the parseInt and valueOf methods in the Integer class.

This post has been edited by jpav: 12 July 2009 - 10:18 PM

### #4 akash2004u

Reputation: 0
• Posts: 4
• Joined: 24-March 09

## Re: converting strings to integers

Posted 12 July 2009 - 11:32 PM

use the parseInt Method of the Integer wrapper class

int i = Integer.parseInt("String to be converted");

make sure to catch this usually throws invalid number format exception

### #5 ronaldjames

Reputation: 4
• Posts: 42
• Joined: 29-June 09

## Re: converting strings to integers

Posted 12 July 2009 - 11:53 PM

You can also use
```int int_value=Integer.decode("String to be converted");

```

OR

```int int_value=Integer.valueOf("string to be converted");

```

You should throw NumberFormatException

This post has been edited by ronaldjames: 12 July 2009 - 11:55 PM

### #6 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: converting strings to integers

Posted 13 July 2009 - 01:40 AM

As everyone says, Integer.parseInt() is the method to use.

Don't worry about all this catching of NumberFormatException. Plenty of time for that when your class covers exceptions. Teaching exceptions at the same time as converting String to int is like teaching gears to someone who is still trying to understand how a wheel works.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: converting strings to integers

Posted 13 July 2009 - 04:39 PM

cfoley, on 13 Jul, 2009 - 12:40 AM, said:

Teaching exceptions at the same time as converting String to int is like teaching gears to someone who is still trying to understand how a wheel works.

lol I'll have to remember that one