Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes who can tell what is the method parseInt( )? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "who can tell what is the method parseInt( )?" Watch "who can tell what is the method parseInt( )?" New topic
Author

who can tell what is the method parseInt( )?

Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
int i = 5;
int j = i.parseInt("6");
what is the result and how to use the method parseInt( )?


help you means help me
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
string s="1234";
int Nb=Integer.parseInt("1234");// Nb==1234(an int)


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Deepak Kumar
Greenhorn

Joined: Jan 25, 2002
Posts: 4
parseInt(String) is a method used to convert the string into its equivalent integer value.But this should be applied with care.One should use it only when u are sure the string contains only numerals.
eg
String s="1234"
int i=Integer.parseInt(s);
its perfect
but
String s="abcd"
int i=Integer.parseInt(s);
Its not right.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
yes vdeepak is right.
I didn't pay attention.
I should have done
int nb=Integer.parseInt(s)// Where s="1234"
So this method convert a string to an int.
But you can convert a string to a double too, like this:
Double nb=Double.parseDouble(s)
Brent Parrish
Greenhorn

Joined: Jan 25, 2002
Posts: 4
You can cast an int to a double, but not the way
you've suggested. Firstly, Double is a wrapper class and is treated as an object not a primitive
value. There's a big difference between Double and
double. Additionally, there is no parseDouble() method available in the Double class.
When using explicit casts you need to understand the difference between a narrowing conversion and a widening conversion. You can't explicitly cast a
double value to an int, but, you can cast an int to a double. A good book that describes this better than I can is "The Complete Java 2 Certification Guide" published by Sybex.
Here's some sample code to cast the int i to a double.
public class NumberCasts {
static String s="1234";
static int i=Integer.parseInt(s);
static double d = (double)i;
public static void main(String[] args) {

System.out.println("Variable d = " + d);
}
}
The output is "Variable d = 1234.0"
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Sorry if my post was confused.
My english is not fluent.
In fact I was talking about string not int.
int nb=Integer.parseInt(s)// Where s="1234"

You can see that 1234 is between quote. It is a string.
YOU SAID
Additionally, there is no parseDouble() method available in the Double class.

This method exist see at the JAVA 1.3 API

parseDouble
public static double parseDouble(String s)
throws NumberFormatExceptionReturns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
Parameters:
s - the string to be parsed.
Returns:
the double value represented by the string argument.
Throws:
NumberFormatException - if the string does not contain a parsable double.
Since:
1.2
See Also:
valueOf(String)

Hope it helps!!!
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
As stated by Brent

Here's some sample code to cast the int i to a double.
public class NumberCasts {
static String s="1234";
static int i=Integer.parseInt(s);
static double d = (double)i;
public static void main(String[] args) {
System.out.println("Variable d = " + d);
}
}
The output is "Variable d = 1234.0"



You don't need an expicit cast when assigning an int value to a double variable.
The following code will also work fine .
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Originally posted by Shivaji Marathe:
As stated by Brent

this code will work also

It Will Work Fine (I hope)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: who can tell what is the method parseInt( )?
 
Similar Threads
Unexpected ArrayIndexOutOf BoundsException.
Extract all integers from a text file, how to do?
parseInt (String s, int radix)
What parseInt() method do?
Alternate Replacement of ParseInt in java ?