Adam Calcanes Adam Calcanes - 1 month ago 11
Java Question

How to replace an integer element in a 2d array with a string type, using a Random

I have this table (2d array) and I'm using a Random utility from numbers 0-4 to select a row and column to replace a number with the letter "P" and I have everything but I get this.

- Type mismatch: cannot convert from String
to int
- Type mismatch: cannot convert from
Random to int
- Type mismatch: cannot convert from
Random to int


Meanwhile this question How to change value of array element in 2D arrays? says you can just do

someArray[row][column] = "x";


Here is what I have (I assume my problem is with using the random)

import java.util.Random;
public class Server{
private int number;
private boolean pennyLanded;
public Server()
{
int n = 0;
number = n;
pennyLanded = false;
}
public boolean pennyLanded()
{
return pennyLanded;
}
public void setPennyLanded()
{
pennyLanded = true;
}
public int getNumber()
{
return number;
}
public String toString()
{
if (pennyLanded)
return "P";
else
return "" + number;
}
public static int[][] tableMaker(){
int[][] table = new int[5][5];
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
if(i==2 && j==2){
table[i][j] =3;
}
else if(i==0 || i==4){
table[i][j] = 1;
}
else if(j==4 || j==0){
table[i][j] = 1;
}
else if((i==1 || i==3) && (j>0 || j<4)){
table[i][j] = 2;
}
else if((i==2 && j==1) || (i==2 && j==3)){
table[i][j] = 2;
}
}
}
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
System.out.print(table[i][j] + " ");
}
System.out.println();
}
return table;
}



public static int[][] tossPenny(){
Random row = new Random();
Random column = new Random();
int[][] table = Server.tableMaker();
for(int i=0; i<5; i++){
table[row.nextInt(4)][column.nextInt(4)] = -1;
}
return table;
}

}


The table is printed in a Client class

public class Client{
public static void main(String[] args){

System.out.println(Server.tableMaker());
}
}


I Don't know how simple of a fix this is, and I have searched I just have a particular problem

The table is printed as so

1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1


And I would like something like this say the random puts out 1 and 2

1 1 1 1 1
1 2 P 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

Answer

Was able to figure it out with help of a IRL friend and this avoids the memory store errors as seen before, Server is below

 import java.util.Random;
    public class Server{
    private static int[][] table;
    public static int[][] tableMaker(){
            table = new int[5][5];
            for(int i=0; i<table.length; i++){
                for(int j=0; j<table.length; j++){
                    if(i==2 && j==2){
                        table[i][j] =3;
                    }
                    else if(i==0 || i==4){
                        table[i][j] = 1;
                    }
                    else if(j==4 || j==0){
                        table[i][j] = 1;
                    }
                    else if((i==1 || i==3) && (j>0 || j<4)){
                        table[i][j] = 2;
                    }
                    else if((i==2 && j==1) || (i==2 && j==3)){
                        table[i][j] = 2;
                    }
                }
            }
            return table;
        }

        public static void printTable(){
            for(int i=0; i<table.length; i++){
                for(int j=0; j<table.length; j++){
                     if(table[i][j] == -1) System.out.print("P ");
                     else System.out.print(table[i][j] + " "); 
                }
                System.out.println();
            }
        }



        public static int[][] tossPenny(){
            Random row = new Random();
            Random column = new Random();
            for(int i=0; i<5; i++){
                table[row.nextInt(4)][column.nextInt(4)] = -1;
            }
            return table;
        }

    }  

Client is here

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

        Server.tableMaker();
        Server.printTable();
        System.out.println();
        Server.tossPenny();
        Server.printTable();
        input.close();
    }
}  

Thanks a lot everyone especially @DavidWallace You helped me get around most of my problems and I was able to fix everything using the private static int[][] table;

Comments