0

I want my program to output this in a certain way, how would I make this possible? So far, my code is giving me the wrong thing.

Here's my .txt file:

ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO

Here's my java file:

import java.io.*;

public class EncryptDecrypt {

    public static void encrypt() throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("cryptographyTextFile.txt"));
        String line = in.readLine();

        char[][] table = new char[6][5];

        // fill array
        for(int i = 0; i < 6; i++) {
            for(int j = 0; j < 5; j++) {
                while(table[i][j] < 6) {
                    table[i][j] = line.charAt(j);
                }
            }
        }

        // print array
        for(int i = 0; i < 6; i++) {
            for(int j = 0; j < 5; j++) {
                System.out.println(table[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException {
        encrypt();
    }
}

How would I print this .txt file like so:

ABCDE
GHIJK
MNOPQ
STUVW
XYZOO
OOOOO
yabva89
  • 59
  • 4

1 Answers1

2

A couple of problems

see

    String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO";

    char[][] table = new char[6][5];
    int counter = 0;
    // fill array
    for(int i = 0; i < 6; i++) {
        for(int j = 0; j < 5; j++) {
            table[i][j] = line.charAt(counter++);  // need to increment through the String
        }
    }

    // print array
    for(int i = 0; i < 6; i++) {
        for(int j = 0; j < 5; j++) {
            System.out.print(table[i][j] + " ");  // not println
        }
        System.out.println();
    }

output

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 O O O O 

Although a more extensible way would be link

    String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO";
    String lines [] = line.split("(?<=\\G.....)");
    for (String l : lines) {
        System.out.println(l);
    }
Community
  • 1
  • 1
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64