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

Thread: Check code?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Unhappy Check code?

    i have to write a code to verify the following:
    maxEnd(new int[]{1, 2, 3}) -> {3, 3, 3}
    maxEnd(new int[]{1, 3}) -> {3, 3}
    maxEnd(new int[]{3}) -> {3}

    Here's my code:
    public class OneB {
    public static int[] maxEnd(int[] nums) {
    int max = Math.max(nums[0], nums[nums.length - 1]);
    for (int i = 0; i < nums.length; i++) {
    nums[i] = max;
    }
    return nums;
    }
     
    public static void main(String[]args){
    	System.out.println(maxEnd(new int[]{1, 2, 3}));
     
     
    	}
     
    }

    at the terminal, when i try to test it, i get the result : [I@f7e6a96 instead of {3, 3, 3}
    and idea why this is happening?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Check code?

    [I@f7e6a96
    That is the String returned by an int array's toString() method when you print using the array's name.
    To see the contents of an array, try this
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    hemla (April 30th, 2013)

  4. #3
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Check code?

    You mean: System.out.println("[I@3487a5cc "+ java.util.Arrays.toString(nums)); ? :S

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Check code?

    The id String inside the "s is to put a label on what is printed in case there is more than one printed to be able to identify which print statement printed it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Check code?

    also i get the error, cannot find symbol variable nums after typing in System.out.println("[I@3487a5cc "+ java.util.Arrays.toString(nums))
    could u please correct it? need it really urgent and i dont seem to understand it
    I simply want to be able to see my output

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Check code?

    cannot find symbol variable nums
    Where is the variable: nums defined? The compiler can not find its definition in scope (within the same pair of {}s) where it is being used.

    Post the code that shows where nums is defined and where the code gets the error for cannot find.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Check code?

    Got the desired result!! i was supposed to type in System.out.println(java.util.Arrays.toString(maxEn d(new int[]{1, 2, 3})));
    Thanks a lot for the help!

Similar Threads

  1. Can somone please check my code???
    By rob28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2013, 07:36 PM
  2. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  3. Can Somebody check this code?
    By manager00104 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: January 30th, 2012, 11:27 AM
  4. Please check the code for the errors
    By nrao in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 16th, 2010, 05:37 PM
  5. Check this code out
    By jwill22 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 11th, 2010, 08:34 PM