Results 1 to 7 of 7
- 09-13-2012, 08:20 PM #1
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
is it possible to convert String type of value into integer using type conversion?
import java.io.*;
class inout
{
public static void main(String args[])throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int a=(int)d.readLine();
System.out.println(a);
}
}
if u compile this it produce an error message as "inconvertable types".
why it comes?
i mean, we can convert the byte to int, chat to int or int char or any other datatype to other.
but why string type converted into int?
- 09-13-2012, 08:44 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Re: is it possible to convert String type of value into integer using type conversion
Strings and ints are two entirely different things (e.g. what would the int value of a String "foo" be?); not all is lost though: the Integer class has static mehods that can convert Strings to ints.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-14-2012, 12:44 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Re: is it possible to convert String type of value into integer using type conversion
As mentioned by jos some strings cant be changed but if an String contains only an "int" then you can use Integer.parseInt(yourStringHere); to return that number
- 09-14-2012, 06:20 AM #4
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: is it possible to convert String type of value into integer using type conversion
thanks for ur reply sir.
sir I know how to convert string into integer.
but my question is,
we have type conversion concept to convert one datatype to another.
for example,
float c=(float)10/3;
here , we dividing 2 integers.
if u divide 2 ints we get int
but by using typecasting the int value is converted into float.
in the same way,
i want to convert string to int. bcz we have typecasting.
but why it produce an error.
send me the answer plz
-
Re: is it possible to convert String type of value into integer using type conversion
It produces an error because as has already been stated, it simply can't be done. I'm not sure how we can state this again so that it's clear to you. You've been given techniques above on how to convert from String to int.
- 09-14-2012, 11:16 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: is it possible to convert String type of value into integer using type conversion
Maybe he wants to convert character by character. Doesn't converting a character to integer give its ASCII decimal representation?
Edit:
something like this
% 26 68 48 26 59 60 27Java Code:... String convertedWord = ""; for( int i = 0; i < word.length() ; i++ ) { int character = Integer.parseInt(word.substring(i,i+1)); convertedWord += Integer.toString(character) + " "; } System.out.println(convertedWord); ...
or
% 9001Java Code:int character = 0; for( int i = 0; i < word.length() ; i++ ) { character += Integer.parseInt(word.substring(i,i+1)); } System.out.println(character);Last edited by fultonwilcox; 09-14-2012 at 11:24 AM.
- 09-14-2012, 12:33 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: is it possible to convert String type of value into integer using type conversion
Primitives != Objects.
You cannot cast from one to the other.
The compiler knows this so gives you an error.
In fact (with auto-boxing) it's a little more complex.
The compiler sees you cast an object to an int (or other primitive).
So it checks if that object could be of the Integer class (or other relevant primitive wrapper).
If not it then fails.Please do not ask for code as refusal often offends.
Similar Threads
-
Trying to convert Java Date to type int to fit in mysql table with field type int(11)
By fortwnty420 in forum New To JavaReplies: 4Last Post: 08-01-2011, 10:29 AM -
Incompatible type for method. Can't convert java.lang.String to char.
By renu in forum New To JavaReplies: 1Last Post: 07-27-2010, 06:01 PM -
Type conversion
By hannes in forum New To JavaReplies: 2Last Post: 12-18-2009, 11:29 AM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
How to cast an Object into a specific type (Integer/String) at runtime
By mailtogagan@gmail.com in forum Advanced JavaReplies: 2Last Post: 12-03-2007, 01:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks