Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For-each and assigning to a 2d array Java

I'm using a for-each loop to move through a 2d array of chars in a basic java program. It works, except for a small part at the end. Here is my class:

static String letters = "abcdefghijklmnopqrstuvwxyz";

public static boolean checkIsNumeric(String s) {
    boolean haveBeenNonDigitCharacters = false;
    char[] chars = s.toCharArray();
    for(char c : chars)
        if(!Character.isDigit(c)) {
            haveBeenNonDigitCharacters = true;
            break;
        }

    if(haveBeenNonDigitCharacters) 
        return false;
    else 
        return true;
}

public static char[][] createArray() {
    String height;
    String width;
    Scanner s = new Scanner(System.in);
    do {
        System.out.println("How tall do you want the array to be?");
        height = s.next();
    } while(!checkIsNumeric(height));
    do {
        System.out.println("How wide do you want the array to be?");
        width = s.next();
    } while(!checkIsNumeric(width));

    int intHeight = Integer.parseInt(height);
    int intWidth = Integer.parseInt(width);

    char[][] chars = new char[intWidth][intHeight];

    for(char c[] : chars) {
        for(char d : c)
            d = ' ';
    }

    return chars;
}

public static char[][] fillArray(char[][] a) {
    int counter = 0;
    for(char c[] : a) {
        for(char d : c) {
            if(counter >= 26)
                counter = 0;
            d = letters.charAt(counter);
            counter++;
        }
    }
    return a;
}

public static void main(String[] args) {
    char[][] chars = createArray();
    chars = fillArray(chars);

    for(char c[] : chars) {
        for(char d : c) {
            System.out.print(d);
            if(d == ' ')
            System.out.print("a");
        }
        System.out.println("");
    }
}

Basically, what I want to do, is take any 2d array with user-specified dimensions, and fill it with the letters of the alphabet over and over until it is full. The checkIsNumeric method just checks to see if a String is numeric, the createArray method creates the 2d array with the user-specified dimensions, and the fillArray method fills the array with letters from the string. But when I loop through the array at the end of main and print each character, nothing gets printed. Why?

like image 719
eric.mitchell Avatar asked Feb 02 '26 05:02

eric.mitchell


2 Answers

Change fillArray to:

public static char[][] fillArray(char[][] a) {
    int counter = 0;

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
            if(counter >= 26)
                counter = 0;
            a[i][j] = letters.charAt(counter);
            counter++;
        }
    }

    return a;
}

So that the actual array a is being modified and not the copies created by the for-each loop.

Which will give the output (which I assume is correct):

How tall do you want the array to be?
5
How wide do you want the array to be?
4
abcde
fghij
klmno
pqrst

Same goes for createArray if you want each char to be initialised to ' '. In main, if you cast d as an int and print it you'll see that each char is set to it's initial value of 0.

like image 163
AusCBloke Avatar answered Feb 03 '26 19:02

AusCBloke


You are assigning a char (d) the value ' ', then the scope of d goes away. You are not assigning the array's value anything.

like image 35
Jon Lin Avatar answered Feb 03 '26 19:02

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!