6 Replies - 582 Views - Last Post: 13 July 2009 - 04:39 PM Rate Topic: -----

#1 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: converting strings to integers

Posted 12 July 2009 - 10:16 PM

View Posthlln, 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

Was This Post Helpful? 1
  • +
  • -

#3 jpav  Icon User is offline

  • New D.I.C Head

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

Re: converting strings to integers

Posted 12 July 2009 - 10:17 PM

View Posthlln, 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

Was This Post Helpful? 0
  • +
  • -

#4 akash2004u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 ronaldjames  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1948
  • View blog
  • Posts: 4,048
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: converting strings to integers

Posted 13 July 2009 - 04:39 PM

View Postcfoley, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1