Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default 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?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default 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

  4. #4
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default 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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    fultonwilcox is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default 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

    Java 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);
    
    ...
    % 26 68 48 26 59 60 27
    or
    Java Code:
    int character = 0;
    for( int i  = 0; i < word.length() ; i++ )
    {
        character += Integer.parseInt(word.substring(i,i+1));
    
    }
    System.out.println(character);
    % 9001
    Last edited by fultonwilcox; 09-14-2012 at 11:24 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default 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.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 08-01-2011, 10:29 AM
  2. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  3. Type conversion
    By hannes in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 11:29 AM
  4. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  5. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •