Results 1 to 5 of 5

Thread: even array fail

  1. #1
    Swane is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default even array fail

    so the script is: (the problem is that it is suppose to check if the arrays have the same content in an order but when it does different numbers in sais that the flag is true, why?)
    class HWMission_1
    {
    public static void main(String[] args)
    {
    int []a=new int[5];
    int []b=new int[5];
    fill(a);
    fill(b);
    print(a);
    print(b);
    boolean flag=same(a,b);
    System.out.println(flag);
    }

    public static boolean same(int[] a,int[] b)
    {
    for(int i=0;i<a.length;i++)
    if(a[i]==b[i]) return false;
    return true;
    }

    public static void fill(int []a)
    {
    int up=9;
    int down=1;
    for(int i=0;i<a.length;i++)
    a[i]=down+(int)(Math.random()*(up-down+1));
    }

    public static void print(int []a)
    {
    for(int i=0;i<a.length;i++)
    System.out.print(a[i]+" ");
    System.out.println();
    }
    }

  2. #2
    Swane is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: even array fail

    wait i solved it because i needed to do != instead of ==

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: even array fail

    Is the same( ... ) method supposed to return false if two elements in the two arrays are the same? I guess you wanted != instead of ==

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Swane is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: even array fail

    i did but i just didnt see that needle in the hay stack

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: even array fail

    It is only a needle in the haystack when you keep poking a stick at the code from far away as if it is something alien; you'll quickly find it if you take the time to run through it piece by piece and perhaps add some System.out.println() statements to figure out the flow of the program and the values of the variables. At the risk of sounding dirty: you have to get intimate with the code to figure out where problems are.

    That is going to be an important thing to learn as before you know it you will be doing this in hundreds or thousands of lines of code spread over dozens of source files.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Fail to get a resource in URL format
    By xarzu in forum New To Java
    Replies: 5
    Last Post: 07-09-2014, 02:56 AM
  2. Fail in Jcreator
    By salahuddin in forum New To Java
    Replies: 6
    Last Post: 01-10-2012, 10:00 PM
  3. All applets fail to start
    By dathoedezt in forum Java Applets
    Replies: 4
    Last Post: 01-14-2011, 12:25 AM
  4. connection fail
    By dixit.ashish in forum New To Java
    Replies: 4
    Last Post: 02-03-2010, 09:57 PM
  5. Replies: 5
    Last Post: 03-26-2009, 07:24 AM

Posting Permissions

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