I am doing a Bingo program. I need to save the bingoCard (bidimensional array), because every time I execute a new move the card is different. And it need to be the same.
So I need to know how to save the card. I have a global variable (private static int[][]bingoCard=new int[3][9];) this is the code
public static int[][] bingoCard(){
int numRows = 3;
int numCols = 9;
int randomNumbersPerRow = 5;
int randomNumberBound = 90;
Random random = new Random();
bingoCard = new int[numRows][numCols];
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
bingoCard[row][col] = 0;
}
for (int i = 0; i < randomNumbersPerRow; i++) {
int indexToAssign = random.nextInt(numCols);
while (bingoCard[row][indexToAssign] != 0) {
indexToAssign = random.nextInt(numCols);
}
int numToAssign = random.nextInt(randomNumberBound)+1;
bingoCard[row][indexToAssign] = numToAssign;
}
}
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
System.out.printf("%2d ", bingoCard[row][col]);
}
System.out.println();
}
return bingoCard;
}
Related
i have to check the validity of a sudoku as a homework. the rows and cols were no big deal, but i'm stuck at checking the boxes.
i want to loop through the 3x3 sub-boxes and check them for duplicates, with copying the numbers into a smaller 3x3 array, flattening it and then checking it for duplicates.
my while loop doesn't seem to make sense tho, the first iteration works but the second one writes wrong values into my new array. i just don't know where to add my boxcount.
i feel like a noob, if someone can help i'd be thankful
private static boolean isValidSudokuSolution(int[][] sudokuField) {
// TODO: Implementieren Sie hier Ihre Lösung für die Methode
//rows
int row = 0;
while (row < sudokuField.length) {
for (int i = 0; i < sudokuField[row].length; i++) {
for (int j = 0; j < i; j++) {
if (sudokuField[row][i] == sudokuField[row][j]) {
return false;
}
}
}
row++;
}
//cols
int col = 0;
while (col < sudokuField.length) {
for (int i = 0; i < sudokuField[col].length; i++) {
for (int j = 0; j < i; j++) {
if (sudokuField[i][col] == sudokuField[j][col]) {
return false;
}
}
}
col++;
}
//box
int boxCount = 0;
while (boxCount < sudokuField.length) {
//create box array
int[][] box = new int[3][3];
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
box[i][j] = sudokuField[i + boxCount][j];
}
}
//flatten box
int[] flattenedBox = new int[sSize];
int counter = 0;
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
int num = box[i][j];
flattenedBox[counter + j] = num;
}
counter += 3;
}
//look for duplicates
for (int i = 0; i < flattenedBox.length; i++) {
for (int j = 0; j < i; j++) {
if (flattenedBox[i] == flattenedBox[j]) {
return false;
}
}
}
boxCount += 3;
}
return true;
}
I was wondering if you guys could help me out with some of my code. I have been programming this puzzle creator, but when I attempt to generate values for my two-dimensional array, I get an OutOfBoundsException. I'm sure this solution is simple, but for whatever reason, I can't seem to find what's wrong. (Also, of course, there is a driver class that goes along with this, but I don't think that's necessary to include here.)
import java.util.*;
public class ThirtyWonderful
{
private int dimen;
public ThirtyWonderful (int dimensions)
{
dimen = dimensions;
}
public ThirtyWonderful()
{
dimen = 5;
}
Random gen = new Random();
int[][] nums = new int [dimen][dimen];
public void genPuzzle()
{
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
nums[count][col] = gen.nextInt(9) + 1;
}
}
checkAcc();
if(checkAcc() == true)
{
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
System.out.print(nums[count][col] + " ");
}
System.out.println();
}
}
}
public boolean checkAcc()
{
int tot = 0;
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
tot += nums[count][col];
}
if(tot != 31)
return false;
}
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
tot += nums[count][col];
}
if(tot != 31)
return false;
}
return true;
}
}
You are declaring nums with new int [dimen][dimen]; Before dimen even initialization, By default dimen value is 0, so nums is initialize with new int[0][0]
Here is the Fix Code
public class ThirtyWonderful {
private int dimen;
Random gen = new Random();
int[][] nums;
public ThirtyWonderful(int dimensions) {
this.dimen = dimensions;
nums = new int[dimen][dimen];
}
public ThirtyWonderful() {
this(5);
}
public void genPuzzle() {
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
nums[count][col] = gen.nextInt(9) + 1;
}
}
checkAcc();
if (checkAcc() == true) {
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
System.out.print(nums[count][col] + " ");
}
System.out.println();
}
}
}
public boolean checkAcc() {
int tot = 0;
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
tot += nums[count][col];
}
if (tot != 31) {
return false;
}
}
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
tot += nums[count][col];
}
if (tot != 31) {
return false;
}
}
return true;
}
}
you should initialize your array object in your constructors. you are initializing your array at class load time before your object is instantiated.
The first piece of code results in only one column and bunch of rows. The second piece of code results in 5x5 board. What is wrong with the first piece of code. It's probably something stupid and simple, but I couldn't find it.
int size = 5; // size of the board
for (int row = 1; row <= size; ++row) {
for (int col = 1; col <= size; ++col) {
System.out.println("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print("# ");
}
System.out.println();
}
int size = 5;
for(int row=1;row<=size;++row)
{
for (int col = 1; col <= size; ++col)
{
System.out.print("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for(int i=0;i<height;i++)
{
for (int j = 0; j < width; j++)
{
System.out.print("# ");
}
System.out.println();
}
I was wondering how it was possible to fill a ragged array with a loop in Java.
In my example I want to put Pascals Triangle in the Array. When I try to do it ragged, (
// int [][] hako = new int [n][]; -> as I understand it; it gives a java.lang.NullPointerException.
Thanks
int n = 12, r = 1;
int [][] hako = new int [n][];
for(int i = 0; i < n; i++){
for(int j = 0; j < r; j++){
hako[i][j] = newton(i, j);
}
r++;
}
static int factorial(int n){
int k = 1;
if(n == 0)
return 1;
while(n>1){
k*=n;
n--;
}
return k;
}
static int newton(int i, int j){
return factorial(i)/((factorial(j))*(factorial(i-j)));
}
You need to initialize hako[i] as an array before you can assign a variable to an index within it i.e. hako[i][j].
int n = 12, r = 1;
int [][] hako = new int [n][];
for(int i = 0; i < n; i++){
for(int j = 0; j < r; j++){
// need to initialize hako[i]
hako[i] = new int[r];
hako[i][j] = newton(i, j);
}
r++;
}
your hako, is a matrix, but you initialize only one dimension thus your NullPointerException
to fix it try
for(int i = 0; i < n; i++){
hako[i] = new int[r];
for(int j = 0; j < r; j++){
hako[i][j] = newton(i, j);
}
r++;
}
public static void main(String[] args) {
int y[][] = new int[4][];
int four =4;
for (int row = 0; row < y.length; row++) {
y[row] = new int[four--];
}
RaggedArray(y);
for (int row = 0; row < y.length; row++) {
for (int column = 0; column < y[row].length; column++) {
System.out.print(y[row][column] + " ");
}
System.out.println();
}
}
public static void RaggedArray(int x[][]) {
int j;
for (int i = 0; i < x.length; i++) {
int k=1;
for (j = 0;j<x[i].length ; j++) {
x[i][j] = k++;
}
}
}}
You can change the size and fill it with any data. I wish it will be useful for u and anyone see this code.
I tried to run this code as a score table where I have chars and ints as below for heading;
A B c
1
2 65 //this is where I'm stuck again!
3
In order to print the score like (65) above in a particular place (matrix) but as soon as I try to add the print statements the table falls apart. Any help would be appreciated;
public class Table3 {
static int[][] list = new int[4][4];
//private char column = 'A';
//private int row = 1;
private static int row = 1;
public Table3(){
//column = 'A';
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 4; j++)
list[i][j] = 0;
}
}
public static void table(char col, int row, int value) {
//System.out.printf("\n\n%s\n", "Table");
for (int i = 1; i < 4; i++) {
System.out.print(row + " ");
row++;
for (int j = 1; j < 4; j++)System.out.print(col + " ");
System.out.println("\n");
col++;
if (row >= 0 && row <= 4 && col >=0 && col <= 4)
System.out.print(list[col][row]=value);
System.out.println("\n");
}
}
}
Client
public class TableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Table3 t = new Table3();
t.table('A', 5, 5);
}
}
Learn how to use System.out.printf. The docs are here.
public class Table3
{
static int numRows = 4;
static int numCols = 4;
static int[][] list = new int[numRows][numCols];
public Table3()
{
//column = 'A';
for (int i = 0; i < 4; i++)
{
//this is the row number so you don't have to print it manually
//just print the array
list[i][0] = i;
//initialize the list to 0
for (int j = 1; j < 4; j++)
{
list[i][j] = 0;
}
}
}
public static void table(char col, int row, int value)
{
list[row][col] = value;
int columnWidth = 5; //in characters
//empty space before first column header
for (int i = 0; i < columnWidth; i++)
{
System.out.print(" ");
}
//print the column headers (A through C)
for (int i = 1; i < numCols; i++)
{
System.out.printf("%-" + columnWidth" + "c", (char)(64 + i));
}
System.out.println(); //get off of the column header row
//print the rest of the table
for (int i = 1; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (list[i][j] == 0)
{
System.out.printf("%" + columnWidth + "s", " ");
}
else
{
System.out.printf("%-" + columnWidth + "d", list[i][j]);
}
}
System.out.println("\n");
}
}
}