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

JAVA: Help with 2D array...

Options
  • 10-10-2007 7:42pm
    #1
    Closed Accounts Posts: 27,857 ✭✭✭✭


    Hey folks,

    having some trouble with a program I'm working on...

    I'm reading in a number of chars from a file in a particular pattern, and I want to take the chars and load them into a 2D array of INTS. So in order to do that I need to use a switch statement and then probably some other stuff...

    Could someone tell me how I'd manage that? So far I'm able to read in the file and using the switch statement, display a different output in the same pattern.

    I'm thinking something like...
                while (in.ready()) {
                    nextChar  = (char)in.read();
    		    int newChar;
    
    			switch(nextChar){
        		   	case 'B': newChar = -2;break; 
    			case '!': newChar = 0;break;
    			case '*': newChar = 0;break;
    
    .... etc
    

    ...and then use a for loop to read the new characters (ints) into the array.

    What ye think? Any merit in that?

    edit:

    Forgot to ask -- if the file being read in goes onto a new line ('\n'), do I need to specify that in the 2D array, and what would I use in the switch statement to signify that?

    eg. input file:

    15010
    10001
    11101

    Will I have to use some sort of function to make the array the same dimensions? (ie. myArray[3][5])


Comments

  • Registered Users Posts: 6,240 ✭✭✭hussey


    So in order to do that I need to use a switch statement and then probably some other stuff...

    You may want to explain this a bit better

    what is the purpose of this?

    You mentioned a for loop - where is your for loop? over what are you looping over ..

    If you want to store the characters somewhere then do work on them - like sorting or counting the letters etc
    best read them into a StringBufffer then do analysis on this

    you get the idea
    String fileString = "";
    while (more chars){
    fileString += char_just_read;
    }
    for (int i=0;i< string.length; i++){
    work on fileString.charAt(i);
    }

    *edit - actuallY I see your 2d array purpose
    Since you need to have a value for the length of the array at runtime (i.e. you can't expand an array)
    you could read the new line char into the String then split the strings based on the number of '\n'

    this will return an array of Strings
    String[] array1 = fileString .split("\n");
    then you could split each array1 into a charArray
    Char[] array = array1[X].toCharArray();

    then you could create an int array based on the lengths

    int[][] intArray = new int[array1.length][];

    then go through each array1 - and create a array[] based on this length
    for (int i=0; i< intArray.length; i++){
    intArray = new int[array1.toCharArray().length];
    }

    so you would have the correct demensions set etc - then just do analysis on the chars to set them into the array


Advertisement