Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

How Can I fill a 2D array with random characters.

Options
  • 15-05-2011 2:19pm
    #1
    Registered Users Posts: 266 ✭✭


    public static void main(String[] args)
    {
    char mychars[][] = new char [5][6];
    for(int r=0; r<mychars.length; r++)
    for(int c=0; c<mychars[0].length; c++)
    {
    mychars[r][c] = ???????
    System.out.print("\t"+mychars[r][c]);
    }
    }//main

    should i use this somehow:
    {'a','b','c','d','e','f','g','h','i','j','k','l','n','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    Any help appreciated.


Comments

  • Closed Accounts Posts: 9,139 ✭✭✭-Trek-


    Maybe you could use the random method to pick a number between 97 - 122 (a-z in ascii) and convert it to a char.

    char c = (char)randomValue;


  • Registered Users Posts: 266 ✭✭Gerb68


    Even if I dont use random. Is there an easier way to declare a 2d array as characters...

    public static void main(String[] args)
    {
    char mychars[][] = new char [5][6];
    for(int r=0; r<mychars.length; r++)
    {
    System.out.print("\n");
    for(int c=0; c<mychars[0].length; c++)
    {
    mychars[0][0]='a';
    mychars[0][1]='b';
    mychars[0][2]='c';
    mychars[0][3]='d';
    mychars[0][4]='e';
    mychars[0][5]='f';
    mychars[1][0]='g';
    mychars[1][1]='h';
    mychars[1][2]='i';
    mychars[1][3]='j';
    mychars[1][4]='k';
    mychars[1][5]='l';
    mychars[2][0]='m';
    mychars[2][1]='n';
    mychars[2][2]='o';
    mychars[2][3]='p';
    mychars[2][4]='q';
    mychars[2][5]='r';
    mychars[3][0]='s';
    mychars[3][1]='t';
    mychars[3][2]='u';
    mychars[3][3]='v';
    mychars[3][4]='w';
    mychars[3][5]='x';
    mychars[4][0]='y';
    mychars[4][1]='z';
    mychars[4][2]='a';
    mychars[4][3]='b';
    mychars[4][4]='c';
    mychars[4][5]='d';

    System.out.print(" "+mychars[r][c]);

    }//end of c
    }//end of r
    System.out.print("\n\n\n\n");
    }//main


  • Registered Users Posts: 255 ✭✭boblong


    Haven't tested this, so no promises, but this is probably along the lines of what you want. Let us know how you get on.
    public class Main {
        
        public static void populateRandom(char[][] host) {
            int max = 122;
            int min =  97;
            
            for (int i=0; i<host.length;i++) {
                for (int j=0;j<host[i].length;j++) {
                    int random_int = min + (int)(Math.random() * ((max - min) + 1));
                    assert random_int <= 122;
                    assert random_int >= 97;
                    host[i][j] = (char)random_int;
                }
            }
        }
        
        public static void main(String[] args) {
            char[][] host = new char[5][5];
            populateRandom(host);
            
            //test
            for (int i =0;i<host.length;i++) {
                for (int k=0; k<host[i].length;k++) {
                    System.out.print(host[i][k]+" ");
                }
                System.out.println();
            }
        }
    
    }
    


  • Registered Users Posts: 266 ✭✭Gerb68


    Some code in there abit advanced for me. I'm going over a past exam paper and the question:
    Process a 5 row 6 column 2d array of characters and determine how many of these characters are vowels.
    The vowels determination I think I can manage its just populating the array with characters is where I,m stuck.

    With my Exam tomorrow. I've already spent too much time on this therefor getting behind with other areas of java.

    Thanx for the help.

    I thought this would work:

    char mychars[][] = { {'a','b','c','d','e','f'},
    {'g','h','i','j','k','l'},
    {'m','n','o','p','q','r'},
    {'s','t','u','v','w','x'},
    {'y','z','a','b','c','d'} };

    It compiles but when I run I get an exception.


  • Registered Users Posts: 255 ✭✭boblong


    Oh so they don't have to be truly pseudorandom?

    You can make a simple 2d char array like this:
    char [][] chars = {{'a','b','c','d'},{'e','f','g','h'}};
    

    If you're still getting exceptions feel free to paste your code up here for us to have a look at if you want. Good luck on the exam.


  • Advertisement
  • Registered Users Posts: 266 ✭✭Gerb68


    This will compile but still giving an exception:

    class VowelTest{
    public static void main(String[] args)
    {
    int count=0;
    char [][] mychars = {{'a','b','c','d'},{'e','f','g','h'}};
    for(int r=0; r<mychars.length; r++)
    {
    System.out.print("\n");
    for(int c=0; c<mychars[0].length; c++)
    {
    System.out.print(" "+mychars[r][c]);
    if (mychars[r][c]=='a' || mychars[r][c]=='e' || mychars[r][c]=='i' || mychars[r][c]=='o' || mychars[r][c]=='u')
    count++;
    }//end of c
    }//end of r
    System.out.print("\n\n vowels appear "+count+" times");
    System.out.print("\n\n\n\n");
    }//main


    }//class


  • Registered Users Posts: 255 ✭✭boblong


    That compiled and ran for me, what is the exception being thrown (ie. can you show us the output from the program, errors and all)?

    Shot in the dark, but if it is a ClassNotFound exception it might be because you called your class VowelTest but your file [something else].java


  • Registered Users Posts: 266 ✭✭Gerb68


    boblong wrote: »
    Shot in the dark, but if it is a ClassNotFound exception it might be because you called your class VowelTest but your file [something else].java

    Your exactly right.

    My apologies I had saved it as VowelTestb as I was running a couple of variations of the program.

    Cant believe I did that(As well now and not tommorrow).

    Thats all been a great help, thanx alot. I can move on now.


Advertisement