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
converting strings to integers
Page 1 of 16 Replies - 465 Views - Last Post: 13 July 2009 - 04:39 PM
Replies To: converting strings to integers
#2
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
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
Re: converting strings to integers
Posted 12 July 2009 - 10:17 PM
#4
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
int i = Integer.parseInt("String to be converted");
make sure to catch this usually throws invalid number format exception
#5
Re: converting strings to integers
Posted 12 July 2009 - 11:53 PM
You can also use
OR
You should throw NumberFormatException
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
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.
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
Re: converting strings to integers
Posted 13 July 2009 - 04:39 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|