0

Is there an easy way to merge multiple, say char arrays to get a char matrix? I have 8 arrays below with 64 chars each and I want to merge to a matrix with 8 rows and 64 cols.

package august26;

import java.util.Scanner;

public class XBits {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    long n1 = input.nextInt();
    long n2 = input.nextInt();
    long n3 = input.nextInt();
    long n4 = input.nextInt();
    long n5 = input.nextInt();
    long n6 = input.nextInt();
    long n7 = input.nextInt();
    long n8 = input.nextInt();

    String s1 = String.format("%64s", Long.toBinaryString(n1)).replace(' ', '0');
    String s2 = String.format("%64s", Long.toBinaryString(n2)).replace(' ', '0');
    String s3 = String.format("%64s", Long.toBinaryString(n3)).replace(' ', '0');
    String s4 = String.format("%64s", Long.toBinaryString(n4)).replace(' ', '0');
    String s5 = String.format("%64s", Long.toBinaryString(n5)).replace(' ', '0');
    String s6 = String.format("%64s", Long.toBinaryString(n6)).replace(' ', '0');
    String s7 = String.format("%64s", Long.toBinaryString(n7)).replace(' ', '0');
    String s8 = String.format("%64s", Long.toBinaryString(n8)).replace(' ', '0');

    s1.toCharArray();
    s2.toCharArray();
    s3.toCharArray();
    s4.toCharArray();
    s5.toCharArray();
    s6.toCharArray();
    s7.toLowerCase();
    s8.toCharArray();
    input.close();
}

}

4
  • 4
    There are some problems with your code. toCharArray() returns an array, and you're not storing that returned array anywhere, making those method calls an expensive no-op. In addition, you should be using arrays to store the results of your input.nextInt() calls and your formatted strings, not individual variables. That would allow you to use for loops and avoid all that repetitive code. Commented Nov 7, 2014 at 23:13
  • 1
    You may want to look into multi-dimensional arrays Commented Nov 7, 2014 at 23:13
  • isn't this what varargs do? Commented Nov 7, 2014 at 23:15
  • Fairly certain a matrix in this regard would simply be a two dimensional array, eg String[][] Commented Nov 7, 2014 at 23:15

2 Answers 2

2
char[][] matrix = new char[8][];
matrix[0] = s1.toCharArray();
matrix[1] = s2.toCharArray();

etc...

Sign up to request clarification or add additional context in comments.

4 Comments

That's only a tiny part of the OP's problems.
Oh, well since this answer is accepted, I guess I can stop typing my answer now.
I agree @RobertHarvey, there is much to be fixed in this code. Include for loops, change the format string to get rid of replace(), for starters.
@RobertHarvey No way - contribute another answer. If it's better, it might get a lot of up-votes. I'm sure you can do better than I since I don't specialize in Java.
0

untested pseudocode:

package august26;

import java.util.Scanner;

public class XBits {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    ArrayList<Character[]> charArray = new ArrayList<Char[]>(); 
    while (input.hasNextInt()) {
        charArray.add(
            String
                .format("%64s", Long.toBinaryString(input.nextInt())
                .replace(' ', '0')
                .toCharArray()
        );
    }
    input.close();
}

2 Comments

You meant ArrayList<Character[]> or ArrayList<char[]>, right?
As I said, it's untested, so probably. I'm not exclusively a Java dev, so I might miss some of the finer details.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.