0

I am developing a simple program to print seat numbers, where the row are numbered from 1-5 and columns from a-e. the code i am using is as follows

public class JavaApplication5 {
    public static void main(String[] args) {
        int j =1,k;
        int i;
        char c;
        String[] arr = new String[25];
        for( i = 0;i < arr.length;i++)
        {
            while(j <= 5)
            {
                for(k = 97;k < 102; k++)
                {
                    c = ((char)k);
                    arr[i] = j + "" + c;
                    System.out.println(arr[i]);
                }
                j++;
            }
        }
    }
}

this displays desired result. but when I print an element outside the for loop I get the result as null like below

public static void main(String[] args) {
    int j =1,k;
    int i;
    char c;
    String[] arr = new String[25];
    for( i = 0;i < arr.length;i++)
    {
        while(j <= 5)
        {
            for(k = 97;k < 102; k++)
            {
                c = ((char)k);
                arr[i] = j + "" + c;
            }
            j++;
        }
    }
    System.out.println(arr[6]);
}

how to solve this?

1
  • If you have a solution which displays the desired result what is your question? Commented Sep 19, 2015 at 13:13

4 Answers 4

1

this will leave all elements as null

String[] arr = new String[25];

this will iterate until j == 5 so only until arr[5]

while(j <= 5) {
   j++;
}

Therefore arr[6] is still null

Sign up to request clarification or add additional context in comments.

Comments

1

Change

arr[j] = j + "" + c;

instead of

arr[i] = j + "" + c;

Now it works.

public static void main(String[] args) {
    int j = 1, k;
    int i;
    char c;
    String[] arr = new String[25];
    for (i = 0; i < arr.length; i++) {
        while (j <= 5) {
            for (k = 97; k < 102; k++) {
                c = ((char) k);
                arr[j] = j + "" + c;
            }
            j++;
        }
    }
    System.out.println(arr[1]);
    System.out.println(arr[2]);
    System.out.println(arr[3]);
    System.out.println(arr[4]);
    System.out.println(arr[5]);
    System.out.println(arr[6]); // null because your check j <= 5 in while loop
}

Comments

0

You can access the array elements normally outside of the loop. In your example, arr[6] is just null. The fault is not in the way you access it. (Although i cant see the bug yet ;))

Comments

0

The problem in your code is that you simply write 5 time on index 1, then 5 time on index 2 and so on. So you never wrote on index 6. Your code should be changed to code below:

String[] arr = new String[25];
int i = 0;
int j = 1;
while (j <= 5) {
    for (k = 97; k < 102; k++) {
        c = ((char) k);
        arr[i++] = j + "" + c;
    }
    j++;
}
System.out.println(arr[6]);

Because your loops run 5*5 times, then your i index will never pass arr array length. But you better control it like this to prevent your code from being error prone:

if(i < arr.length) {
    arr[i++] = j + "" + c;
} else {
    break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.