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 4 of 4

Thread: Amazing code

  1. #1
    Junior Member
    Join Date
    May 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Amazing code

    Hi all

    i saw slice code in linkedin.com unfortunately i can't undrestand that on the other hand it's woderfull slice for me
    is anybody here to help me
    i know char t[] convert to numeric so we are gaining t[]'s ascii code there are 53.42,107,63,64 ; i undrestand until last for
    my question is how we are gaining 42 in result or what is working exactly last for
    thank you all

    here it is
    static char t[] = {'5','*','k','@','?'};
    public static void main(String[] args) {
    System.out.println(function(t));
    }
    protected static int function (char s[])
    {
    int[] d = new int [s.length];{
    for (int i = 0; i < s.length; i++) {
    d[i] = (int) s[i];}
    }
    int p =0, h =d.length-1;
    for (;
    !(d[p] >= d[h--]
    &&
    ++h == p++) {}

    return d[h];
    }
    Last edited by karakral; May 30th, 2018 at 08:27 AM.

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

    Default Re: Amazing code

    how we are gaining 42
    You can see the char for an int by casting it to a char: System.out.println((char)42);
    You can see the int value for a char by casting it to an int: System.out.println((int)'*');

    When posting code be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Amazing code

    my question isn't about cast my question is i trace program my answer was 64
    my question is what is that exactly
    for (;
    !(d[p] >= d[h--]
    &&
    ++h == p++) {}
    my variables are p = 0 and h = 4 , so d[p] equals d[4] , d[4] inclusive 64
    why my answer is 42 why my answer isn't 63 or 64

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

    Default Re: Amazing code

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Please post a complete program that will compile and execute for testing.
    Make sure the code is properly formatted with indentations to make it readable.

    my question is what is that exactly
    for (;
    It looks like a for statement. See the tutorial: https://docs.oracle.com/javase/tutor...bolts/for.html
    The for statement has 3 parts separated by ;s
    for (initialization;
    termination;
    increment)
    Any part can be empty. The posted code only has the termination part.
    The statement executes until the termination part is false.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Degree vs. Amazing porfolio
    By Daryn in forum The Cafe
    Replies: 3
    Last Post: June 18th, 2014, 09:43 AM