Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Confusion with the question about this coding

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confusion with the question about this coding

    What does mean by ((int)((string)((int)"7")+"5"))

    The result of i = ((int)((string)((int)"7")+"5"))
    1) An integer i, where i = 12
    b) A String, i where i = 12
    c) A String, i where i = 75
    d) An integer i, where i = 75
    e) An Exception

    I would like to know what does mean by (int)(string) and then ((int)"7")+ "5"))
    Does it mean I need to convert int to string or vice versa?

    public static void printnum1() {

    /*int i = 12;


    /*i = ((int)((string)((int)"7")+"5"));

    System.out.println(i); */

    }
    I'm not following the questions


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusion with the question about this coding

    What does mean by ((int)((string)((int)"7")+"5"))
    What happens when you compile that code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusion with the question about this coding

    It shows an error message on i = ((int)((string)((int)"7")+"5")); when I run

    public static void printnum1() {

    int i = 12;


    i = ((int)((string)((int)"7")+"5"));

    System.out.println(i);
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusion with the question about this coding

    It shows an error message
    Ok, what is the error message?

    When posting code be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusion with the question about this coding

    It says "String can not be resolved to a type or can not cast from String to int"

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusion with the question about this coding

    You should copy the error message, not retype it and make mistakes.
    I assume the error message said this:
    string can not be resolved to a type

    The compiler could not find the definition for the class: string
    Java is case sensitive. The java class name is String.

    can not cast from String to int
    A String object can not be cast to an int. No object can be cast to a primitive.
    To convert a String to an int, use the Integer class's parse method.

    Where did that statement come from? What is it supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusion with the question about this coding

    Obviously you can tell that I'm new to programming...
    I'm kind of confused with i = ((int)((string)((int)"7")+"5")) when question a) says An integer, where i = 12;

    These are test questions that I need to answer.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusion with the question about this coding

    That statement is not a valid java statement.
    You have already posted 2 errors that it has.

    It needs to be rewritten to compile.
    First define what you want the code to do. Then write the code to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusion with the question about this coding

    what does mean by ((int)((string)((int)"7")+"5")) ? just could not get through what I need do by this statement

    Thanks

    --- Update ---

    I agree with you, it might be mistaken or purposely given me invalid statement! what you think from your expertise, thank you

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confusion with the question about this coding

    The statement does not make sense as coded.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Confusion with the question about this coding

    look in java int to String or vice verse is not possible. but in place of " " if u place ' ' and in place of String if u place char data type then ur code look like this ((int)((char)((int)'7')+'5')) and it is a valid expression which mean is that
    "1st u r converting char 7 to its equivalent int type= 55 and char value of 5 is 53 then both 53 and 55 is added result 108 is converted to char again its converted to int so output is 108
    but according to ur question it will give compile time error "inconvertible types" it is not an Exception
    There is lot of difference between Compile time error and Exception
    Last edited by rita khatei; May 14th, 2014 at 02:28 PM.

Similar Threads

  1. Simple coding question, 1 error
    By Extrocity in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 12th, 2014, 08:51 PM
  2. AVL tree confusion, syntax confusion, just general confusion really...
    By platterofhotfish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2013, 06:04 AM
  3. Very basic coding question, I'd appreciate feedback.
    By amsh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2012, 01:35 PM
  4. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM
  5. [SOLVED] simple question, coding unrelated, how to delete multiple tabs?
    By Perd1t1on in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 07:44 PM

Tags for this Thread