0

my problem is i am not able to update the value of an object array...

The code is :

public class GlobalVariable {

    public int noOfSms = 0;
    public CheckingClass object = new CheckingClass ();
    public static void main(String[] args) {
       GlobalVariable call = new GlobalVariable ();
       call.driver();
    }

    private void driver() {
        for(int i = 0 ; i < 3 ; i++){
        object = CheckingFun();

     //   System.out.println("The No Of Sms"+noOfSms);
       System.out.println("Array Value"+object.array[noOfSms] + "     The number value"+object.number);
        }
    }

    private CheckingClass CheckingFun() {

        System.out.println("The No Of Sms "+noOfSms + "\n");
        object.array[noOfSms] = noOfSms;
        object.number = noOfSms;
        noOfSms = noOfSms + 1;

        return object;

    }

The other class is :

public class CheckingClass {
public int number ; 
public int[] array = new int [5];

}

Here object is an object of another class. My problem is in this line

object.array[noOfSms] = noOfSms;

The output is as follows

The No Of Sms 0

Array Value0     The number value0
The No Of Sms 1

Array Value0     The number value1
The No Of Sms 2

Array Value0     The number value2

What i don't understand is that why isn't the value of array updated. When the value of number which is also an attribute of the object is updated.

But the output i want is :

 Array Value0     The number value0
The No Of Sms 1

Array Value1     The number value1
The No Of Sms 2

Array Value2    The number value2
user1318860
  • 67
  • 1
  • 4
  • 10
  • 1
    I would suggest to post some compilable code. – marc wellman Jun 29 '12 at 13:36
  • 3
    You do realize that you're updating a different slot in the array each time with `[noOfSms]`, right? – Thomas Jun 29 '12 at 13:36
  • @Thomas yes i do i want the index of array to be updated each time i come in that function – user1318860 Jun 29 '12 at 13:42
  • 1
    before your give me -1 for a bad question ....Kindly answer it so that i do get something useful after bearing a damage to what little reputation i have !!!! – user1318860 Jun 29 '12 at 13:47
  • Post some compilable code. You posted non compilable code, and an output that doesn't come from the code you posted. How do you want us to discover where the problem is? You also don't say what output you expect. Only the one you get. – JB Nizet Jun 29 '12 at 13:51
  • @ JB Nizet i have edited my question i hope now it helps !!! – user1318860 Jun 29 '12 at 13:57

2 Answers2

2

You're printing array[0] each time in your output, while each loop is updating array[0]. array[1], etc.

Edit:

You're incrementing noOfSms at the end of CheckingFun(). So for the first pass, you set array[0] = 0, but before you reach your println, noOfSms has been incremented and is now 1. So you print array[1], which has not been initialized.

Thomas
  • 5,074
  • 1
  • 16
  • 12
  • I was checking so change it to array[0]... but previously i was doing array[noOfsms] and it still gives me the same output :( – user1318860 Jun 29 '12 at 14:02
0

object.array[0] You repeatedly calling this function for three times

So array[0] value is 0

`object.array[i]`
aravindKrishna
  • 440
  • 2
  • 11