Hello I'm new to coding and in my class, we have jumped straight into coding with zero experience and or lessons, this is my first assignment and i am completely lost.

Write a program that reads text from a file. Create a 2-dimensional character array that is
6 * 7. Store the characters read in your array in row major order (fill row 0 first, then row
1, etc.). Fill any unused spaces in the 2-D array with the ‘*’ character. If you have more
characters than space, ignore the excess characters.
Extract the characters from your array in column-major order (pull from column 0 first,
then column 1, etc.). Build a new string as you extract the characters. Display the new
string.
Here is an example for the input string
"It was a bright cold day in April, and the clocks were striking thirteen."
Output string: IatdAat apn bcyrdwro i aililtsgdn,h h e


Respuesta :

Answer:

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class CharTwoDArray {

   public static void main(String[] args) {

       String file = "word.txt";

       final int MAX_CHARS = 1000;

       int count = 0;

       char array[] = new char[MAX_CHARS];

       char array2D[][] = new char[6][7];

       try {

           int t;

           FileReader fr = new FileReader(file);

           BufferedReader br = new BufferedReader(fr);

           while ((t = br.read()) != -1) {

               if ((char) t == '\n') continue;

               array[count++] = (char) t;

           }

       } catch (IOException e) {

           System.out.println("Error in opening file!");

           System.exit(0);

       }

       int t = 0;

       for (int x = 0; x < 6; x++) {

           for (int y = 0; y < 7; y++) {

               // if no char then adding *

               if (t >= count) {

                   array2D[x][y] = '*';

               } else

                   array2D[x][y] = array[t++];

           }

       }

       for (int i = 0; i < 6; i++) {

           for (int j = 0; j < 7; j++) {

               System.out.print(array2D[i][j]);

           }

           System.out.println();

       }

   }

}

Explanation:

  • Use buffer reader for reading file .
  • Add char to 1D array.
  • Add 1D array  to 2D array.
  • Display the 2D array .