I have a small project to complete on my course but I'm a little stuck on solving this, basically I need to print the unicode characters in a multidimensional array to a table, 12 rows and 5 columns. So far I have this:
public class MultiArrTest {
public static void main(String[] args0) {
char[][] uc = new char[12][5];
int x = 64;
for (int i = 0; i < uc.length; i++) {
for (int j = 0; j < uc[i].length; j++) {
uc[i][j] = (char) x++;
System.out.print(uc[i][j] + " ");
System.out.println();
}
}
}
}
This prints the unicode but only in one column, I feel a bit silly here but could anyone give me a suggestion?
Thanks a lot.
Move the System.out.println(); outside the second for-loop and inside the first one, just after the for-loop:
for (int i = 0; i < uc.length; i++) {
for (int j = 0; j < uc[i].length; j++) {
uc[i][j] = (char) x++;
System.out.print(uc[i][j] + " ");
}
System.out.println();
}
Related
I'm trying to populate a 2D array with char's from a string I've read in. I'm having a problem with actually populating this 2D array. It keeps printing a 2D array bigger than what I've given it, and the number always seems to be 6 rather than the letters from the string.
I store the string in an ArrayList called tempArray.
Input strings:
WUBDLAIUWBD
LUBELUFBSLI
SLUEFLISUEB
I instantiate a 2D array with columnlength = 11, and rowcount 3
epidemicArray = new int[rowCount][columnCount];
Array before I try to populate it:
00000000000
00000000000
00000000000
My code:
public static void updateArray(){
//extract string from temp
for (int i = 0; i < tempArray.size(); i++){
String temp = tempArray.get(i);
char[] charz = temp.toCharArray();
for (int j = 0; j < charz.length; j++){
for (int k = 0; k < rowCount; k++){
for (int l = 0; l < columnCount; l++){
epidemicArray[k][l] = charz[j];
}
}
}
}
}
Output: Which I didn't expect
6666666666666666666666
6666666666666666666666
6666666666666666666666
Expected output: (2D array)
WUBDLAIUWBD
LUBELUFBSLI
SLUEFLISUEB
Thanks, this is really bugging me.
Change your code to this:
public static void updateArray(){
//extract string from temp
for (int i = 0; i < tempArray.size(); i++){
String temp = tempArray.get(i);
char[] charz = temp.toCharArray();
for (int j = 0; j < charz.length; j++){
epidemicArray[i][j] = charz[j];
}
}
}
This edit should work since the number of columns is the length of one of the string (same length for the 3 of them).
Here is my output
[EDIT]. #magna_nz, I used the following methods to print the array
public static void printRow(int rowNumber) {
for (int i = 0; i < 11; i++) {
System.out.print( epidemicArray[rowNumber][i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
updateArray();
for (int i = 0; i < 3; i++) {
printRow(i);
}
}
This will print the numbers, but if you want to print characters you can change the above printRow method to something like:
public static void printRow(int rowNumber) {
for (int i = 0; i < 11; i++) {
System.out.print( (char)epidemicArray[rowNumber][i] + " ");
}
System.out.println();
}
And this will give you the following result:
You're overwriting your entire epidemicArray with the last value that charz[j] gets. Which is apparently 66. Actually you're overwriting that entire array with every value from charz and the last one won.
I'm being told my formatting is off in this code, can anybody tell me what's wrong with it?
public class TwoDimArray {
public static void main(String[] args) {
int rows = 2;
Int columns = 2;
String[][] anArray = {{"Ireland", "Green"},{"England", "White"}};
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
System.out.println(anArray[i][j]);
}
}
}
}
Also, can anybody tell me how to get it to print as so:
Ireland Green
England White
As apposed to:
Ireland
Green
England
White
the code you have written has an error :
Int columns = 2;
this would be the correct code:
int columns = 2;
and if you want that your code have this output:
Ireland Green
England White
you can use this code:
public class TwoDimArray {
public static void main(String[] args) {
String[][] anArray = {{"Ireland", "Green"},{"England", "White"}};
for (int i = 0; i < anArray.length; i++){
for (int j = 0; j < anArray.length; j++){
System.out.print(anArray[i][j] + " ");
}
System.out.println();
}
}
}
I hope understand and help you
good lucky
public class TwoDimArray {
public static void main(String[] args) {
String[][] anArray = {{"Ireland", "Green"},{"England", "White"}}; // create your array 2 dimensions
for (int i = 0; i < anArray.length; i++){ // create for, .length is the array size,
for (int j = 0; j < anArray.length; j++){
System.out.print(anArray[i][j] + " "); // print the element of array and add one space for print with you want
}
System.out.println(); //when end the FOR with J variable, you has printed the row, and with println, add a one new line
}
}
}
with .length you get a variable size/length , in this case but your array is array[2][2]
and I use a println because when ends println, this method introduce a new line in the end line , a one line jump, and the next println write will be in a new line.
however the print , is same print that in C language, only write and the next print will be in the ends first print... so I enter +" " in the ends print
for (int i = 0; i < anArray.length; i++){
for (int j = 0; j < anArray.length; j++){
System.out.print(anArray[i][j] + " ");// First iteration of the FOR :Ireland" " second iteration of the FOR : Green" "
}
System.out.println(); // add a line jump for the next FOR iteration...
}
}
if you still don't understand, you can try a change de " " for " , " or you can try change the println for print and you will begin to understand
I hope help you now jajaja
Good lucky
I am working on a java Othello game and am using a 2D array with padding to build the board. I have the board printing just fine, the columns are labeled "a -h" but i need the rows to be numberd "1-8" and cannot figure out how to do this. my code is as follows:
void printBoard() {
String results = "";
OthelloOut.printComment(" a b c d e f g h");
int row = board.board.length;
int col = board.board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
}
the othelloOut class sort of extends the System.out print statements
public class OthelloOut {
static public void printMove(PieceColor color, Move amove){
System.out.printf("%s %s\n", color, amove);
}//printMove
static public void printComment(String str){
System.out.printf("C %s\n", str);
}//printComment
static public void printReady(PieceColor color){
System.out.printf("R %s\n", color);
}//printReady
}//OthelloOut
any help will be much appreciated. If this needs to be clarified more just let me know! thanks.
UPDATE: Numbers print but i prints 0 - 9 and i want it to skip the digits 0 and 9 to where its blank in the positions of those two numbers. Any suggestions? Thanks for the help guys!
Your best bet is doing it here:
for (int i = 0; i < row; i++) {
OthelloOut.printComment(i); // Obviously not exactly like this.
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
Remember that you're not using println, you're using print. You want all other text to be printed onto the same line as i.
And while I'm here..
I would be using a StringBuilder, instead of concatenating a String.
for (int i = 0; i < row; i++) {
StringBuilder results = new StringBuilder();
OthelloOut.printComment(i); // Obviously not exactly like this.
for (int j = 0; j < col; j++) {
results.append(pieces[board.board[i][j] + 2]);
}
OthelloOut.printComment(results.toString());
}
You can add the row number at each row iteration like this:
for (int i = 0; i < row; i++) {
results += i + 1; // add the row number
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
I did an exercise in one of my books to get this output. We are supposed to use nested loops and basic java. I could not format the output in here but the code below produces the correct output. I got it to print the correct output but I feel like it is very redundant, mainly with the loops regarding the * and spaces if you have a better method for doing this please share!
private static void printDesign(){
int astrickStopper = 1;
int slashStopper = 1;
for (int lines = 1; lines <= 7; lines++) {
for (int firstAstrick = 6; firstAstrick >= astrickStopper; firstAstrick--) {
System.out.print("*");
}
for (int spaces = 1; spaces <= slashStopper; spaces++) {
System.out.print(" ");
}
for (int forwardSlash = 6; forwardSlash >= slashStopper; forwardSlash--) {
System.out.print("//");
}
for (int backSlash = 1; backSlash < slashStopper ; backSlash++) {
System.out.print("\\\\");
}
for (int spaces = 1; spaces <= slashStopper; spaces++) {
System.out.print(" ");
}
for (int secondAstrick = 6; secondAstrick >= astrickStopper; secondAstrick--) {
System.out.print("*");
}
astrickStopper = astrickStopper + 1;
slashStopper = slashStopper + 1;
System.out.println();
}
}
The code you wrote seems like it meets the problem description. You could move the inner loop into a function that outputs a given sequence of characters a certain number of times, then you would just be calling the function 6 times instead of having 6 inner loops.
public void printChars(int count, String chars) {
for (int i = 0; i < count; i++) {
System.out.print(chars);
}
}
I'm struggling to finish a java exercise, it involves using 2d arrays to dynamically create and display a table based on a command line parameter.
Example:
java table 5
+-+-+-+-+-+
|1|2|3|4|5|
+-+-+-+-+-+
|2|3|4|5|1|
+-+-+-+-+-+
|3|4|5|1|2|
+-+-+-+-+-+
|4|5|1|2|3|
+-+-+-+-+-+
|5|1|2|3|4|
+-+-+-+-+-+
What i have done so far:
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
String[][] table = new String[num*2+1][num];
int[] numbers = new int[num];
int temp = 0;
for(int i=0; i<numbers.length; i++)
numbers[i] = i+1;
// wrong
for(int i=0; i<table.length; i++){
for(int j=0; j<num;j++){
if(i%2!=0){
temp=numbers[0];
for(int k=1; k<numbers.length; k++){
numbers[k-1]=numbers[k];
}
numbers[numbers.length-1]=temp;
for(int l=0; l<numbers.length; l++){
table[i][j] = "|"+numbers[l];
}
}
else
table[i][j] = "+-";
}
}
for(int i=0; i<table.length; i++){
for(int j=0; j<num; j++)
System.out.print(table[i][j]);
if(i%2==0)
System.out.print("+");
else
System.out.print("|");
System.out.println();}
}
This doesn't work, since it prints 1|2|3|4 in every row, which isn't what I need. I found the issue, and it's because the first for loop changes the array order more times than needed and basically it returns as it was at the beginning.
I know that probably there's a way to achieve this by writing more code, but I always tend to nest as much as possible to "optimize" the code while I write it, so that's why I tried solving this exercise by using less variables and loops as possible.
You are too complex. Hard to find your error. Straight code follows:
public static void main(String[] args) {
//int num = Integer.parseInt(args[0]);
int num = 5; // for test
// creating 2d array
int[][] figures = new int[num][num];
// filling the array
for(int row=0; row<figures.length; ++row) {
for(int col=0; col<figures[row].length; ++col) {
figures[row][col] = (row + col) % num + 1;
}
}
// printing the array
for(int row=0; row<figures.length; ++row) {
// printing border
for(int col=0; col<figures[row].length; ++col) {
System.out.print("+-");
}
System.out.println("+");
// printing data row
System.out.print("|");
for(int col=0; col<figures[row].length; ++col) {
System.out.print(figures[row][col]);
System.out.print("|");
}
System.out.println();
}
// printing final border
for(int col=0; col<figures[0].length; ++col) {
System.out.print("+-");
}
System.out.println("+");
}
public static void main(String args[]){
int dimension = Integer.valueOf(args[0]);
int[][] twoDimArray = new int[dimension][dimension];
for(int i=0;i<dimension;i++){
for(int j=0;j<dimension;j++){
System.out.print("|"+((i+1)%(dimension+1)));
} //end of j loop
} //end of i loop
} //end of main
The above is only the logic for printing the numbers in the specified sequence.
The other design pattern ( +-+ ) thing i guess u can manage.
the following codes will initialize a 2d int array for the data (1-5 in your example). and print the table. note that the table structure was not save in a String 2d-array. just print the table out. see comments in line.
public static void main(String[] args){
final int num = 5; //hardcoded 5, just for testing.
final int[][] data = new int[num][num];
for (int r = 0; r < data.length; r++) {
for (int c = 0; c < data[r].length; c++) {
final int t = r + c + 1;
data[r][c] = t <= num ? t : t - num;
}
}
// now we have all int data in data 2D-array
// here is the +-+- line
final StringBuilder sb = new StringBuilder("+");
for (int i = 0; i < data.length; i++)
sb.append("-+");
// now print the table
for (int r = 0; r < data.length; r++) {
System.out.println(sb.toString());
for (int c = 0; c < data.length; c++)
System.out.print("|" + data[r][c]);
System.out.println("|");
}
System.out.println(sb.toString());
}
output:
if you give num=9 as argument. the codes above will print:
+-+-+-+-+-+-+-+-+-+
|1|2|3|4|5|6|7|8|9|
+-+-+-+-+-+-+-+-+-+
|2|3|4|5|6|7|8|9|1|
+-+-+-+-+-+-+-+-+-+
|3|4|5|6|7|8|9|1|2|
+-+-+-+-+-+-+-+-+-+
|4|5|6|7|8|9|1|2|3|
+-+-+-+-+-+-+-+-+-+
|5|6|7|8|9|1|2|3|4|
+-+-+-+-+-+-+-+-+-+
|6|7|8|9|1|2|3|4|5|
+-+-+-+-+-+-+-+-+-+
|7|8|9|1|2|3|4|5|6|
+-+-+-+-+-+-+-+-+-+
|8|9|1|2|3|4|5|6|7|
+-+-+-+-+-+-+-+-+-+
|9|1|2|3|4|5|6|7|8|
+-+-+-+-+-+-+-+-+-+
you have make it more complicated try this Simple code :
enter code here
public static void main(String[] args)
{
int n = 5 ;
for(int i = 1 ; i <= n ;i++)
{
for(int l = 0 ; l < n;l++)
System.out.print("+-");
System.out.print("\n|");
for(int j = i ; j <=n;j++ )
{
System.out.print(j+"|");
}
for(int k = 1 ; i >= 2 && k <=i-1;k++)
{
System.out.print(k+"|");
}
System.out.println();
}
}