Trying to create an array, "error: '.class' expected" - java

Here's my code:
public static String[][] displayPlay()
{
String[][] board = new String[10][10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
board[i][j] = ".";
}
}
return board[][];
}
(This is meant to create a 10x10 array of "."s)
The error message in the title points to the end of the line return board[][]. Why is this?

You want to just return board
Change return board[][]; to return board;

You must return board, not board [][].

Related

How to make sure that every 3x3 block contains values in Sudoku

I want to check a whole Sudoku table if all the blocks got the 9 values or not but I was able to check only the first block and I need to check the other 8 blocks how?
public static boolean checkSubs(int[][] p) {
int[] nums = new int[9];
int x=0, temp;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++) {
temp = p[i][j];
for ( int m=0; m<nums.length; m++)
if ( nums[m]==temp ) return false;
nums[x++]=temp; }
return true; }
You can modify your checkSubsMethod.
Add i and j of the top left corner of sudoku subblock (e.g (0,0), (0,3),... (3,0), (3,3)... (6,3),(6,6)).
Use set to check does the value already used or not. The add() method of Set class return true if value doesn't in the set and false if value already added to the set.
And when you generalise your method you can use it for fields of any size. In your case size is 9x9, here is the example
public static boolean checkSubs(int[][] p, int topI, int topJ) {
Set<Integer> nums = new HashSet<>();
for (int i = topI; i < topI + 3; i++) {
for (int j = topJ; j < topJ + 3; j++) {
if (!nums.add(p[i][j])) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int[][] sudoku = {
{1,2,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{7,8,9,7,8,9,7,8,9},
{1,2,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{7,8,9,7,8,9,7,8,9},
{1,2,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{7,8,9,7,8,9,7,8,9}};
for (int i = 0; i < sudoku.length;i += 3){
for (int j = 0; j<sudoku[0].length; j += 3){
if (!checkSubs(sudoku, i, j)){
System.out.println("DUPLICATED VALUES FOUND!");
return;
}
}
}
System.out.println("OK!!");
}
The output for this case will be OK!!
If you change the input like this
int[][] sudoku = {
{3,3,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{7,8,9,7,8,9,7,8,9},
{1,2,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{7,8,9,7,8,9,7,8,9},
{1,2,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{7,8,9,7,8,9,7,8,9}};
The output will be DUPLICATED VALUES FOUND!
You can modify this example for your purposes in future.

Why does this code in java print a reference instead the result of invoking the method?

I am supposed to write a method that accepts 3 2-D arrays of This method should determine whether one of the matrices is the result of matrix addition of the other two.
public class Matrix {
public static void main(String[]args){
int [][] a = {{5,2,3},{4,1,6},{0,7,2}};
int [][] b = {{1,2,3},{4,5,6},{0,1,2}};
int [][] t = {{6,4,6},{8,6,12},{0,8,4}};
System.out.println(add(a,b));
System.out.println(check(a,b,t));
}
public static int [][] add(int[][]a,int[][]b){
int i=0;
int j=0;
int[][] r = new int [3][3];
while (i<a.length){
r[i][j] = a[i][j] + b[i][j];
i++;
j++;
}
return r;
}
public static boolean check(int[][]a,int[][]b,int[][]t){
int i = 0;
int j = 0;
while(i<t.length){
if(t==add(a,b))
return true;
}
return false;
}
}
add returns an array. Arrays in Java are objects, but they do not override the toString() method. When printing, you'd print their default toString() call, which is implemented by Object as return getClass().getName() + "#" + Integer.toHexString(hashCode());.
Luckily, Java provides a utility in the form of java.util.Arrays.deepToString(Ojbect[]) to generate a more readable string output:
System.out.println(Arrays.deepToString(add(a,b)));
EDIT:
Your add method is also wrong. Your code iterates i and j together, so it only sums the elements along the matrix's diagonal instead of adding all of them. You should use a nested loop instead:
public static int [][] add(int[][]a, int[][]b) {
int[][] r = new int [3][3];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
r[i][j] = a[i][j] + b[i][j];
}
}
return r;
}
Your check method, by the way, is also wrong - it attempts to compare the array itself instead of is elements:
public static boolean check(int[][]a, int[][]b, int[][]t) {
int[][] r = add(a, b);
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (r[i][j] != t[i][j]) {
return false;
}
}
}
return true;
}

Logic check for a 10*10 game

I am doing a game called 1010! Probably some of you have heard of it. Bascially I encouter some trouble when writing the Algorithm for clearance.
The rule is such that if any row or any column is occupied, then clear row and column respectively.
The scoring is such that each move gains a+10*b points. a is the number of square in the input piece p and b is the total number of row&column cleared.
To start, I create a two dimensional Array board[10][10], poulate each elements in the board[][] with an empty square.
In the class of Square, it has public void method of unset()-> "empty the square" & boolean status() -> "judge if square is empty"In the class of piece, it has int numofSquare -> "return the number of square in each piece for score calculation"
In particular, I don't know how to write it if both row and column are occupied as they are inter-cross each other in an two dimensional array.
It fail the test under some condition, in which some of the squares are not cleared but they should have been cleared and I am pretty sure is the logic problem.
My thinking is that:
Loop through squares in first row and first column, record the number of square that are occupied (using c and r); if both are 10, clear row&column, otherwise clear row or column or do nothing.
reset the c &r to 0, loop through square in the second row, second column…
update score.
Basically the hard part is that if I seperate clear column and clear row algorithm ,I will either judge row or column first then clear them . However, as every column contains at least one square belong to the row, and every row contains at least one square belong to the column, there will be mistake when both row and column are full.
Thanks for help.
import java.util.ArrayList;
public class GameState{
public static final int noOfSquares = 10;
// the extent of the board in both directions
public static final int noOfBoxes = 3;
// the number of boxes in the game
private Square[][] board; // the current state of the board
private Box[] boxes; // the current state of the boxes
private int score; // the current score
// initialise the instance variables for board
// all squares and all boxes are initially empty
public GameState()
{
getboard();
score = 0;
board = new Square[10][10];
for(int i =0;i<board.length;i++){
for(int j =0;j<board[i].length;j++){
board[i][j] = new Square();
}
}
boxes = new Box[3];
for(int k =0;k<boxes.length;k++){
boxes[k] = new Box();
}
}
// return the current state of the board
public Square[][] getBoard()
{
return board;
}
// return the current score
public int getScore()
{
return score;
}
// place p on the board with its (notional) top-left corner at Square x,y
// clear columns and rows as appropriate
int r =0;
int c = 0;
int rowandcolumn = 0;
for (int row=0;row<10;row++){
for (int column=0;column<10;column++) {
if (board[row][column].status() == true){
c = c + 1;
if( c == 10 ) {
rowandcolumn = rowandcolumn + 1;
for(int z=0;z<10;z++){
board[row][z].unset(); //Clear column
}
}
}
if (board[column][row].status() == true){
r = r + 1;
if( r == 10) {
rowandcolumn = rowandcolumn + 1;
for(int q=0;q<10;q++){
board[q][row].unset(); //Clear row
}
}
}
}
r=0; //reset
c=0;
}
score = score + p.numberofBox()+10*rowandcolumn;
}
how about this
void Background::liquidate(int &score){
int arr_flag[2][10]; //0 is row,1 is column。
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 10; j++)
{
arr_flag[i][j] = 1;
}
}
//column
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (arr[i][j].type == 0)
{
arr_flag[0][i] = 0;
break;
}
}
}
//row
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (arr[j][i].type == 0)
{
arr_flag[1][i] = 0;
break;
}
}
}
//clear column
for (int i = 0; i < 10; i++)
{
if (arr_flag[0][i] == 1)
{
for (int j = 0; j < 10; j++)
{
arr[i][j].Clear();
}
}
}
//clear row
for (int i = 0; i < 10; i++)
{
if (arr_flag[1][i] == 1)
{
for (int j = 0; j < 10; j++)
{
arr[j][i].Clear();
}
}
}
}
I tried to write somme code for the idea I posted
// place p on the board with its (notional) top-left corner at Square x,y
// clear columns and rows as appropriate
int r =0;
int c = 0;
int rowandcolumn = 0;
int row=FindFirstRow();
int column=FindFirstColumn();
if(row!=-1 && column!=-1)
{
rowandcolumn++;
//actions here: row found and column found
//clear row and column
clearRow(row);
clearColumn(column);
}
else if(row!=-1)
{
//only row is found
//clear row
clearRow(row);
}
else if(column!=-1)
{
//only column is found
//clear column
clearColumn(column);
}
else
{
//nothing is found
}
public void clearRow(int row)
{
for(int i=0; i<10;i++)
{
board[row][i].unset();
}
}
public void clearColumn(int column)
{
for(int i=0; i<10;i++)
{
board[i][column].unset();
}
}
//this method returns the first matching row index. If nothing is found it returns -1;
public int FindFirstRow()
{
for (int row=0;row<10;row++)
{
int r=0;
for (int column=0;column<10;column++)
{
if (board[row][column].status() == true)
{
r = r + 1;
if( r == 10)
{
//row found
return row;
}
}
}
r=0; //reset
}
//nothing found
return -1;
}
//this method returns the first matching column index. If nothing is found it returns -1;
public int FindFirstColumn()
{
for (int column=0;column<10;column++)
{
int c=0;
for (int row=0;row<10;row++)
{
if (board[row][column].status() == true)
{
c = c + 1;
if( c == 10 )
{
//matching column found
return column;
}
}
}
c=0; //reset
}
//nothing found
return -1;
}

Null Pointer Exception - Printing 2d array

I'm having an issue with attempting to print a 2 dimensional array, I continually get a nullpointer exception at the first for loop in printArray(), and at printArray(). I do know what a nullpointerexception means, I'm just having a hard time understanding why I'm getting it. I've ran into this problem before while trying to print an array and it'd be great for me to finally figure this out.
import java.lang.*;
import java.util.*;
public class Board {
int N = 3;
static int [][] unittest;
//construct a board from an N-by-N array of blocks
//(where blocks[i][j] = block in row i, column j)
public Board(int[][] blocks){
blocks = new int[N][N]; //creates array of size N
//generates random numbers 0 inclusive to # exclusive
//Random r = new Random(); uses blocks[i][j] = r.nextInt();
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randInt(0,9);
}
}
unittest = blocks.clone();
}
//generates random numbers in a range
private static int randInt( int min, int max){
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
//board size N
public int size(){
return 0; //placeholder
}
//number of blocks out of place
public int hamming(){
return 0; //placeholder
}
//sum of manhattan distances between blocks and goal
public int manhattan(){
return 0;
}
//is this board the goal board?
public boolean isGoal(){
return true; //placeholder
}
//is the board solvable?
public boolean isSolvable(){
return true; //placeholder
}
//does this board equal y?
//Object because can only take objects
//does it use Comparable?
public boolean equals(Object y){
return true; //placeholder
}
//all neighboring boards
public Iterable<Board> neighbors(){
Stack<Board> placeholder = new Stack<Board>();
return placeholder;
}
//string representation of the board
public String toString(){
return "placeholder";
}
//unit test
private static void printArray(){
for (int i = 0; i < unittest.length; i++){ //**NULL POINTER EXCEPTION
for (int j = 0; j < unittest[i].length; j++){
System.out.print(unittest[i][j]);
if (j < unittest[i].length - 1){
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args){
//prints out board
printArray(); //**NULL POINTER EXCEPTION
}
}
The problem lies in the test condition of printArray() function:
for (int i = 0; i < unittest.length; i++)
Here, unitest is a null object and when you try to apply length method on it, its throwing and exception.
Basically, you are not initializing the unittest object (2D array in your case). You can do something like this to avoid the exception:
private static void printArray(){
if(unittest == null)
System.out.println("its NULL");
else{
for (int i = 0; i < unittest.length; i++){ //**NULL POINTER EXCEPTION
for (int j = 0; j < unittest[i].length; j++){
System.out.print(unittest[i][j]);
if (j < unittest[i].length - 1){
System.out.print(" ");
}
}
System.out.println();
}
}
}
Hope it helps :)
static int [][] unittest;
is null when you call it
you have never initialized the array, nor put anything into it
Beyond initializing the array you have an off by one error
public Board(int[][] blocks){
blocks = new int[N][N]; //creates array of size N
//generates random numbers 0 inclusive to # exclusive
//Random r = new Random(); uses blocks[i][j] = r.nextInt();
for (int i = 0; i < blocks.length; i++){ <----------------- blocks length should be blocks.length-1
for (int j = 0; j < blocks[i].length; j++){ <---------------------also blocks [i].length - 1
blocks[i][j] = randInt(0,9);
}

Concatenating 2-dimensional ragged arrays (Java)

I am trying to concatenate two two-dimensional arrays of different sizes. I have no idea why my method doesn't work. Java tells me, when I mouse over the "return xx": "Type mismatch: cannot convert from int[][] to int[]". When I mouse over "concatenateArr2d..." I get: "Illegal modifier for parameter concatenateArr2d: only final is permitted".
I don't understand why I am getting this error.
public static int[][] concatenateArr2d(int[][] t, int[][] s)
{
int[][] xx = new int[t.length + s.length][];
for(i = 0; i < xx.length; i++)
{
xx[i] = new int[t[i].length + s[i].length];
}
return xx;
}
I still have to do the code to fill the entries but that should not be a problem.
Any help please? Thank you.
It looks to me that the code before the bit you pasted does not have aligned braces. You're probably inside a method when you try to declare this method.
public class HelloWorld{
public static int[][] concatenateArr2d(int[][] t, int[][] s)
{
int jLength=0;
// The below line of code is to determine second dimension size of new array
jLength=Math.max(s[0].length, t[0].length);
int sIterate=0;
//new array created using size of first+second array for First dimension and maximum size for second dimension
int[][] xx = new int[t.length + s.length][jLength];
//first array copy
for(int i = 0; i < t.length; i++)
{
for(int j = 0; j < t[0].length; j++)
{
xx[i][j] =t[i][j];
}
}
//second array copy
for(int i = t.length; i < xx.length; i++)
{
for(int j = 0; j < s[0].length; j++)
{
xx[i][j] =s[sIterate][j];
}
sIterate++;
}
return xx;
}
public static void main(String []args){
int a[][]={{1,2},{2,3},{1,1}};
int b[][]={{1,2,3},{2,3,4}};
int c[][]=HelloWorld.concatenateArr2d(a,b);
//Output iteration
for(int i = 0; i < c.length; i++)
{
for(int j = 0; j < c[0].length; j++)
{
System.out.print(c[i][j]);
}
System.out.println();
}
}
}
note : The blanks filled by 'primitive type default value' like, for int its 0.
Output :
120
230
110
123
234

Categories