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

Thread: Why am I getting the wrong sum in netbeans?

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why am I getting the wrong sum in netbeans?

    Netbeans is giving me the wrong sum.
    package sum;
     
    import java.util.Random;
     
    public class Sum {
     
        public static void main(String[] args) {    
            int[] x=new int[2];
            int[] y=new int[2];        
            Data data=new Data();        
            Random rand=new Random();
            int r1,r2;//Results of random numbers.
     
            r1=rand.nextInt(2);
            System.out.println("First random number is: "+r1);
            r2=rand.nextInt(2);
            System.out.println("Second random number is: "+r2);
     
            if(r1==0){
                x=data.a();
            }else if(r1==1){
                x=data.b();
            }
     
            if(r2==0){
                y=data.a();
            }else if(r2==1){
                y=data.b();
            }
     
            int result=x[0]+y[0];
     
            System.out.println("The sum is: "+result);
            //I get the wrong sum when I get '1' and '0'.
        }
    }
    This is a the java class of the data.
    package sum;
     
    public class Data {
        int[] x=new int[2];
        public int[] a(){
            x[0]=300;
            x[1]=3;
            return x;   
        }
        public int[] b(){
            x[0]=250;
            x[1]=2;
            return x;
        }
    }

    This all the codes.
    I donīt know why it is giving me the wrong answer. Thanks in advance.
    Last edited by jms; April 24th, 2019 at 12:03 PM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Why am I getting the wrong sum in netbeans?

    Please repost your code and include the classes. What you posted are just parts of the code.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why am I getting the wrong sum in netbeans?

    Hi, I have reposted all the codes. It is a small test program for learning. Regards.

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Why am I getting the wrong sum in netbeans?

    The problem is that you are passing back a reference to an array from your Data class. So let's say you first assign the array to x. Then when you call the method again, you are changing the array to another value which also affects the value x has. To solve this problem, make the array local to both Data.a and Data.b.

    Regards,
    Jim

    --- Update ---

    Here is a simpler example to explain the problem.

    public class SimpleExample {
     
       public static void main(String[] args) {
    	  MyData data = new MyData();
    	  int[] x = data.a(); // x[0] = 200
    	  int[] y = data.b(); // x[0] and y[0] both = 900;
                                         // this is because the call to data.b() also changed the array  that
                                         // x has a reference to.  x and y share the same array.
    	  System.out.println(x[0] + y[0]);
       }
     
    }
     
    class MyData {
       int array[] = new int[1];
     
       public int[] a() {
    	  array[0] = 200;
    	  return array;
       }
     
       public int[] b() {
    	  array[0] = 900;
    	  return array;
       }
    }

  5. The Following User Says Thank You to jim829 For This Useful Post:

    jms (April 24th, 2019)

  6. #5
    Junior Member
    Join Date
    Apr 2019
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why am I getting the wrong sum in netbeans?

    Thank you very much!!!

Similar Threads

  1. Sum of Factorials
    By tmt in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 11th, 2014, 12:25 AM
  2. Sum from a file
    By mikerousse in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 4th, 2014, 05:04 PM
  3. sum of digits
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 02:40 PM
  4. Calendar help - who is wrong, me or Netbeans?
    By wren10514 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 19th, 2012, 09:59 AM
  5. Sum of intervals
    By cisneros778 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 21st, 2012, 04:08 PM