need help in this code with get set methods - java

import java.util.Scanner;
public class Matrix{
private int rowNumber;
private int colNumber;
private int val;
int [][] matrix;
public Matrix(){
rowNumber = 0;
colNumber = 0;
}
public Matrix(int row, int col){
rowNumber = row;
colNumber = col;
Matrix obj = new Matrix();
matrix = new int [rowNumber][colNumber];
Scanner input = new Scanner(System.in);
for (int i = 0; i < rowNumber; i++) {
for (int j = 0; j < colNumber; j++) {
System.out.println("Enter A" + (i+1) +""+ (j+1) + " : ");
matrix [i][j] = input.nextInt();
int val = matrix[i][j];
obj.setElement(rowNumber,colNumber,val);
}
}
obj.display();
}
public void setElement(int r, int c, int value){
matrix = new int [rowNumber][colNumber];
matrix[r][c] = value;
}
public int getElement(int r, int c){
matrix = new int [rowNumber][colNumber];
return matrix[r][c];
}
public void display(){
Matrix ex = new Matrix();
String str = "|\t";
for(int i=0; i<rowNumber; i++){
for(int j=0; j<colNumber ;j++){
**str += ex.getElement(i,j) + "\t";**
}
System.out.println(str + "|");
str = "|\t";
}
}
public static void main (String[] args) {
int rowNumber;
int colNumber;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of rows: ");
rowNumber = input.nextInt();
System.out.println ("Enter number of columns: ");
colNumber = input.nextInt();
Matrix obj1 = new Matrix(rowNumber,colNumber);
}
}
I cannot display the required matrix. having problem in get and set methods. the program input value into an array and that value is stored in a variable. then that variable is passed as argument to the set method the setmethod takes the vaalue and put it in the array with r row and c column, all the values are stored in an array and then the display method is called in the constructor which uses the get method to get the value.

No need to create array again in get and set and display methods.
You are creating lot of objects unnecessarily.I have changed your code to work properly
Please use this code
import java.util.Scanner;
class Matrix {
private int rowNumber;
private int colNumber;
int[][] matrix;
public Matrix() {
rowNumber = 0;
colNumber = 0;
}
public Matrix(int row, int col) {
rowNumber = row;
colNumber = col;
matrix = new int[rowNumber][colNumber];
Scanner input = new Scanner(System.in);
for (int i = 0; i < rowNumber; i++) {
for (int j = 0; j < colNumber; j++) {
System.out.println("Enter A" + (i + 1) + "" + (j + 1) + " : ");
setElement(i, j, input.nextInt());
}
}
display();
}
public void setElement(int r, int c, int value) {
matrix[r][c] = value;
}
public int getElement(int r, int c) {
return matrix[r][c];
}
public void display() {
String str = "|\t";
for (int i = 0; i < rowNumber; i++) {
for (int j = 0; j < colNumber; j++) {
str += getElement(i, j) + "\t";
}
System.out.println(str + "|");
str = "|\t";
}
}
public static void main(String[] args) {
int rowNumber;
int colNumber;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of rows: ");
rowNumber = input.nextInt();
System.out.println("Enter number of columns: ");
colNumber = input.nextInt();
Matrix obj1 = new Matrix(rowNumber, colNumber);
}
}

Your code had several NullPointerExceptions, basically because you were creating new Matrix objects using an empty constructor and then trying to set the matrix' properties from there, which means there was no matrix created inside your empty constructor, thus causing it to point to null.
Try this instead (I also used Prabhaker's answer here):
public Matrix(int row, int col){
rowNumber = row;
colNumber = col;
matrix = new int[rowNumber][colNumber];
Scanner input = new Scanner(System.in);
for (int i = 0; i < rowNumber; i++) {
for (int j = 0; j < colNumber; j++) {
System.out.println("Enter A" + (i + 1) + "" + (j + 1) + " : ");
matrix[i][j] = input.nextInt();
int val = matrix[i][j];
setElement(rowNumber - 1, colNumber - 1, val);
}
}
display();
}
public void setElement(int r, int c, int value) {
matrix[r][c] = value;
}
public int getElement(int r, int c) {
return matrix[r][c];
}
public void display() {
String str = "|\t";
for (int i = 0; i < rowNumber; i++) {
for (int j = 0; j < colNumber; j++) {
str += getElement(i, j) + "\t";
}
System.out.println(str + "|");
str = "|\t";
}
}`

Related

Using for loop to find an element that appears multiple times in a random, unsorted array

My goal is to print out a user input value (and its corresponding index) if it appears 1 or more times in a random generated array of 50 integers. If I search the array for a value, and it happens to appear more than once, however, only one location of the element is printed, and then the if-else statement is executed. If I remove the break at the end of the third for loop, the whole thing falls apart. I've attached an image but here is the part of it that is giving me an issue. Apologies if the code is not clean, I'm very new.
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate 50 random integer unsorted list.\n");
Scanner stdin = new Scanner(System.in);
int[] randomNumbers = new int[50];
for(int index = 0; index < randomNumbers.length; index++) {
randomNumbers[index] = (int) (Math.random()*100);
}//end for
int count = 0;
for(int i = 0; i < randomNumbers.length; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}//end for
System.out.println("\nSearch value?");
int x = stdin.nextInt();
int i;
for(i = 0; i < randomNumbers.length; i++) {
if(randomNumbers[i] == x)
break;}
if (i != randomNumbers.length) {
System.out.println("\nFound " + x + " in array [" + i + "]");}
else {
System.out.println(x + " is not in the list");}
{int temp;
int size = randomNumbers.length;
for(i = 0; i<size; i++ ){
for(int j = i+1; j<size; j++){
if(randomNumbers[i]>randomNumbers[j]){
temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.println("\nSmallest element of the array is: " + randomNumbers[0]);}
System.out.println("\n3. Sort the list:");
int size = randomNumbers.length;
for(i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
if(randomNumbers[i] > randomNumbers[j])
{
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.print("Now the Array after Sorting is :\n\n");
int count1 = 0;
for(i=0; i<size; i++)
{
System.out.print(randomNumbers[i]+ ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}
}
}
if statement in for loop
Implementing the comments to your question:
import java.util.Scanner;
public class NumCount {
private static final int RANDOM_NUMBER_COUNT = 50;
private static void display(int[] randomNumbers) {
int count = 0;
for (int i = 0; i < RANDOM_NUMBER_COUNT; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
private static int[] generateRandomNUmbers() {
int[] randomNumbers = new int[RANDOM_NUMBER_COUNT];
for (int index = 0; index < RANDOM_NUMBER_COUNT; index++) {
randomNumbers[index] = (int) (Math.random() * 100);
}
display(randomNumbers);
return randomNumbers;
}
private static int search(int[] randomNumbers, int x) {
int i;
int count = 0;
for (i = 0; i < randomNumbers.length; i++) {
if (randomNumbers[i] == x) {
System.out.println("\nFound " + x + " in array [" + i + "]");
count++;
}
}
return count;
}
private static int[] sort(int[] randomNumbers) {
int size = randomNumbers.length;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (randomNumbers[i] > randomNumbers[j]) {
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
return randomNumbers;
}
/**
* Start here.
*/
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate " + RANDOM_NUMBER_COUNT + " random integer unsorted list.\n");
int[] randomNumbers = generateRandomNUmbers();
System.out.print("\n2. Search value? ");
Scanner stdin = new Scanner(System.in);
int x = stdin.nextInt();
int count = search(randomNumbers, x);
if (count == 0) {
System.out.println(x + " is not in the list");
}
System.out.println("\n3. Sort the list:");
sort(randomNumbers);
System.out.print("Now the Array after Sorting is :\n\n");
display(randomNumbers);
}
}
Of-course there is no need to search for the smallest number because it will be the first element in the sorted array. Hence I removed that part of your code.

How can I read numbers from a .txt file and put them into a 2-D array?

This is for a school project. I need to read numbers from a .txt file and put them into an array. After they're in an array, I need to pass it into a different class to do the math and compare the numbers. The only problem is, I can't get the code to read the .txt file or make it into an array.
I need to use
if (sq.isMagicSquare())
to pass the array to class Square, but it gives the error:
required: int[][]
found: no arguments
reason: actual and formal lists differ in length
public class MagicSquareTester
{
public static void main() throws IOException
{
Square sq = null;
System.out.println("Enter the name of your data file (magicData.txt):");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.nextLine(); // input data file name from keyboard
Scanner inFile = new Scanner(new File (fileName));
int sqSize = inFile.nextInt(); // read the size
while (sqSize != -1)
{
sq = new Square(sqSize, inFile);
if (sq.isMagicSquare()) //will return true or false
System.out.println("\tWe have a Magic Square!");
else
System.out.println("\tThis is NOT a Magic Square.");
System.out.println(sq);
System.out.println();
sqSize = inFile.nextInt();
}
System.out.println("Of the " + sq.getTotalTested() + " squares tested " + sq.getMagicCount() + " were magic square(s)" );
}
}
public class Square
{
Scanner scan = new Scanner(System.in); //has been imported correctly, btw
int tested = 0, areMagic = 0, sqSize;
boolean magic;
int[][] Square;
public Square(int sqSize, Scanner inFile)
{
Square = new int [sqSize] [sqSize];
}
public void readSquare(Scanner inFile)
{
for(int row = 0; row < sqSize; row++)
for(int col = 0; col < sqSize; col++)
{
Square[row][col] = inFile.nextInt();
tested++;
}
}
public boolean isMagicSquare(int[][] array)
{
Sums testMagic = new Sums();
int rows = testMagic.sumRows(array);
int cols = testMagic.sumCol(array);
int diagonals = testMagic.sumDiagonal(array);
if((rows == cols) && (cols == diagonals) && (diagonals == rows))
{
magic = true;
areMagic++;
}
else
magic = false;
return magic;
}
public int getMagicCount()
{
return areMagic;
}
public int getTotalTested()
{
return tested;
}
}
public class Sums
{
int sum = 0, lastSum = 0, counter1, counter2, counter3;
boolean magic = true;
public int sumRows(int [][] array)
{
for (int row = 0; row < array.length; row++)
{
sum = 0;
for (int col = 0; col < array.length; col++)
{
sum += array[row][col];
System.out.print(sum + " ");
if (lastSum == sum)
{
lastSum = sum;
counter1++;
}
else if (lastSum != sum)
{
magic = false;
System.out.println("This is not a magic square");
row = array.length;
col = array.length;
}
}
}
return counter1;
}
public int sumCol(int [][] array)
{
for (int col = 0; col < array.length; col++)
{
sum = 0;
for (int row = 0; row < array.length; row++)
{
sum += array[row][col];
System.out.print(sum + " ");
if (lastSum == sum)
{
lastSum = sum;
counter2++;
}
else if (lastSum != sum)
{
magic = false;
System.out.println("This is not a magic square");
row = array.length;
col = array.length;
}
}
}
return counter2;
}
public int sumDiagonal(int [][] array)
{
int diagonal1 = 0, diagonal2 = 0;
for (int col = 0; col < array.length; col++)
{
sum = 0;
for (int row = 0; row < array.length; row++)
{
if(row == col)
{
sum += array[row][col];
System.out.print(sum + " ");
diagonal1 = sum;
}
}
}
for (int col = 0; col < array.length; col--)
{
sum = 0;
for (int row = 0; row < array.length; row++)
{
if((row + col) == array.length - 1)
{
sum += array[row][col];
System.out.print(sum + " ");
diagonal2 = sum;
}
}
}
if(diagonal1 == diagonal2)
{
magic = true;
counter2 = counter3;
}
else
counter3 = 0;
return counter3;
}
}
Also, apologies if my code looks weirdly formatted. I've never posted here before and I'm doing my best.
I suggest you use java.io.file and separate the squaring and parsing operations from the file reading operations.
Possibly like follows:
public class MathSquareTester {
public static void main (String[] args) throws IOException {
List<Integer> squaredNums = new ArrayList<Integer>();
FileInputStream in = new FileInputStream(file_name);
BufferedReader reader = new BufferedReader(new FileReader(in));
String line = reader.readLine();
while (line != null) {
//Assuming each line represents a separate integer
squaredNums.add((int)Math.pow(Integer.parseInt(line)), 2);
line = reader.readLine();
}
int[] numsArray = new int[squaredNums.size];
squaredNums.toArray(numsArray);
//All of your numbers are now stored in numsArray
}
}

Showing Multiple Variables in Console with Scanner

The problem is when you entry an input with scanner ,it shows on console. I want them to shown in an order. I want them shown like a matris. But with nextInt method all shows bottom of each other.
I want a console output like this:
But with nextInt() method your new int shows on nextLine like this:
How can i show multiple variables in same line with scanner?
import java.util.Scanner;
public class ProbilityMatrixTest {
static int M;
static int N;
static float[][] matrixX;
static float[][] matrixY;
static boolean isProbilityMatrix;
public static void main(String[] args) {
initiate();
testMatrix(matrixX);
System.out.println();
multiplyMatrix();
testMatrix(matrixY);
}
public static void initiate() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the row and column size of matrix : ");
M = sc.nextInt();
N = sc.nextInt();
System.out.println();
matrixX = new float[M][N];
System.out.println("Enter values of " + M + "x" + N + " matrix :");
for (int j = 0; j < N; j++) {
for (int i = 0; i < M; i++) {
matrixX[i][j] = sc.nextFloat();
}
}
}
public static void testMatrix(float[][] givenMatrix) {
isProbilityMatrix = true;
if (M != N) {
isProbilityMatrix = false;
}
for (int j = 0; j < N; j++) {
float rowVariablesTotal = 0;
for (int i = 0; i < M; i++) {
rowVariablesTotal += givenMatrix[i][j];
if (givenMatrix[i][j] < 0) {
isProbilityMatrix = false;
}
}
if (rowVariablesTotal != 1.0f) {
isProbilityMatrix = false;
}
}
System.out.print("TEST RESULT : ");
if (isProbilityMatrix) {
System.out.println("Probility matrix");
} else {
System.out.println("not Probility matrix");
}
}
public static void multiplyMatrix() {
matrixY = new float[M][N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
float newMatrixVariable = 0;
for (int a = 0; a < M; a++) {
newMatrixVariable += (matrixX[i][a] * matrixX[a][j]);
}
matrixY[i][j] = newMatrixVariable;
}
}
System.out.println("The square of given matrix:");
for (int j = 0; j < M; j++) {
for (int i = 0; i < N; i++) {
System.out.print(matrixY[i][j] + " ");
}
System.out.println();
}
}
}
You need to scan entire lines at a time. Otherwise, you're always pressing the enter key, causing it to look like you're entering one value before the other on previous lines
For example, type 3 3, then enter, then you can type three space separated decimal values, enter, then repeat that twice
System.out.print("Enter the row and column size of matrix : ");
String[] mn = sc.nextLine().split("\\s+");
int M = Integer.parseInt(mn[0]);
int N = Integer.parseInt(mn[1]);
System.out.println();
double[][] matrixX = new double[N][];
for (int i = 0; i < N; i++) {
matrixX[i] = new double[M];
String[] row = sc.nextLine().split("\\s+");
for (int j = 0: j < M: j++) {
matrix[i][j] = Double.parseDouble(row[j]);
//...
}
}

Java Array Method

I am going to include just the necessary code here. I have indicated the problem area, about 8 lines in. I am wanting to use the containSameElements method with these inputs, k and n. I know that the problem is that it is not in the right data type (array) but i'm not sure where to change it/ what to change. I've been messing with this for some time now and I just can't seem to figure it out
import java.util.Scanner;
class Lottery
{
public static double jackpotChance(int k, int n, int m)
{
double jackpotChance = 0;
System.out.println(factorial(n)); //factorial
return jackpotChance;
}
public static double factorial(int n)
{
double result = 1;
for (int i = 1; i <= n; i++) //counter i
{
result *= i;
}
return result;
}
public static int[] enterNumbers(int k, int n)
{
System.out.println();
Scanner s = new Scanner(System.in);
System.out.println("Enter k.");
k = s.nextInt();
System.out.println("Enter n.");
n = s.nextInt();
final int SIZE = k;
int enterNumbers[]= new int[SIZE];
System.out.println("Enter " + k + " integers between 1 and " + n + ".");
int i=SIZE;
for (i = 0; i < enterNumbers.length; i++) //counter i
{
System.out.println("Enter the next number");
Scanner ss = new Scanner(System.in);
int p = ss.nextInt();
if (p >= 1 && p <= n)
{
enterNumbers[i] = p;
}
else
{
System.out.println("Invalid input, please try again.");
i--;
}
}
System.out.println();
System.out.println("Your chosen numbers are: ");
for (i = 0; i < enterNumbers.length; i++)
{
System.out.print(enterNumbers[i] + " ");
}
System.out.println();
System.out.println("Enter m.");
int m = s.nextInt();
jackpotChance(k,n,m);//jackpot
drawNumbers(k,n);
return enterNumbers;
}
public static int[] drawNumbers(int k, int n)
{
int drawNumbers[]= new int[k];
int randNumber;
int i=k;
for (i = 0; i < drawNumbers.length; i++)
{
randNumber = (int) (Math.random()*n + 1); // random draw number
drawNumbers[i] = randNumber; }
System.out.println();
System.out.println("Your drawn numbers are: ");
for (i = 0; i < drawNumbers.length; i++) //counter i
{
System.out.print(drawNumbers[i] + " ");
}
return drawNumbers;
}
public static void main (String[] args)
{
System.out.println("Welcome to the Tennessee Lottery");
System.out.println("----------------------------");
int k = 0;
int n = 0;
enterNumbers(k,n);
if(containSameElements(a,b)==true)
System.out.println("Winner!");
else
System.out.println("Loser.");
}
public static boolean containSameElements(int[] a, int[] b)
{
int len1 = a.length;
int len2 = b.length;
if(len1!=len2)
return false;
else
{
int flag = 0;
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
if(a[i]==b[j])
{
flag = 1;
break;
}
}
if(flag==0)
return false;
else
flag = 0;
}
}
return true;
}
}
Simplify your testing code.
public class ArrayOps {
static private final int SIZE = 10;
public static void main(final String[] args) {
System.out.println("Welcome to the Tennessee Lottery");
System.out.println("----------------------------");
final int[] k = new int[SIZE];
final int[] n = new int[SIZE];
enterNumbers(k, n);
if (containSameElements(k, n) == true) //PROBLEM AREA
System.out.println("Winner!");
else System.out.println("Loser.");
}
private static void enterNumbers(final int[] pK, final int[] pN) {
for (int i = 0; i < SIZE; i++) {
pK[i] = (int) (Math.random() * SIZE);
pN[i] = (int) (Math.random() * SIZE);
}
}
public static boolean containSameElements(final int[] pK, final int[] pN) {
if (pK.length != pN.length) return false;
// mandatory check
for (final int k : pK) {
if (!arrayContains(pN, k)) return false;
}
// optional check, only if arrays have to contain EXACTLY the same elements
for (final int n : pN) {
if (!arrayContains(pK, n)) return false;
}
return true;
}
static private boolean arrayContains(final int[] pArray, final int pLookForNumber) {
for (final int i : pArray) {
if (i == pLookForNumber) return true;
}
return false;
}
public static int[] drawNumbers(final int k, final int n) {
final int drawNumbers[] = new int[k];
int randNumber;
int i = k;
for (i = 0; i < drawNumbers.length; i++) {
randNumber = (int) (Math.random() * n + 1); // random draw number
drawNumbers[i] = randNumber;
}
System.out.println();
System.out.println("Your drawn numbers are: ");
for (i = 0; i < drawNumbers.length; i++) //counter i
{
System.out.print(drawNumbers[i] + " ");
}
return drawNumbers;
}
}

How to change an array of ints to an array of strings?

Ok, the title might be deceiving. All i want to do is take my Bingo program and when the second bingo card is printed, i want to replace all the "0"'s with "X"'s. I was thinking i would have to go and change the array to an string, but i'm not surer where to start.
Here is the Bingo program:
import java.util.*;
import java.io.*;
import java.util.Arrays;
public class Bingo
{
public static final int ROWS = 5;
public static final int COLS = 5;
public static final int VERTICAL = 1;
public static final int DIAGONAL = 2;
public static final int HORIZONTAL = 3;
public static int winFound;
public static int currPick = 0;
public static int randomPick = 0;
public static int WinFound;
public static void main(String[] args)
{
int Totcards;
int[][] card = new int[ROWS][COLS];
int[] picks = new int[25];
fillCard (card);
printCard(card);
playGame(card);
printCard(card);
finalCard(card);
}
private static void fillCard (int[][] card)
{
// FileReader fileIn = new FileReader("Bingo.in");
// Bufferreader in = new Bufferreader(fileIn);
try {
Scanner scan = new Scanner(new File("bingo.in"));
for (int i=0; i<card.length; i++){
for (int j=0; j<card[0].length; j++){
card[i][j] = scan.nextInt();
}
}
} catch(FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
}
}
private static void printCard (int[][] card)
{
System.out.println("\n\tYOUR BINGO CARD : ");
System.out.println("\n\tB I N G O");
System.out.println("\t----------------------");
for (int i=0; i<card.length; i++){
for (int j=0; j<card[0].length; j++){
System.out.print("\t" + card[i][j]);
}
System.out.print("\n");
}
}
private static void playGame (int[][] card)
{
int numPicks = 0;
System.out.println("\n\tBINGO NUMBERS PICKED AT RANDOM FROM BIN: ");
while (true)
{
markCard (card); // Generate a random num & zero-it out
winFound = checkForWin(card); // Look for zero sums
numPicks++;
if (winFound != 0)
{
if (winFound == 1)
{
System.out.print("\n\n\tYOU WIN WITH A VERTICAL WIN AFTER " + numPicks + " PICKS\n");
}
else if (winFound == 2){
System.out.print("\n\n\tYOU WIN WITH A DIAGONAL WIN AFTER " + numPicks + " PICKS\n");
}
else if (winFound == 3){
System.out.print("\n\n\tYOU WIN WITH A HORIZONTAL WIN AFTER " + numPicks + " PICKS\n");
}
announceWin (numPicks);
return;
}
}
}
private static void markCard (int[][] card)
{
int randomPick = (int) (Math.random() * 74) + 1;
for (int j = 0; j < ROWS; j++){
for (int k = 0; k < COLS; k++){
if (card[j][k]==randomPick)
card[j][k] = 0;}
}
System.out.print("\t " + randomPick + " ");
System.out.print("");
}
private static int checkForWin(int[][] card)
{
int sum=0;
for (int i = 0; i < ROWS; i++)
{
sum = 0;
for (int j = 0; j < COLS; j++)
sum += card[i][j];
if (sum == 0)
return HORIZONTAL;
}
for (int j = 0; j < COLS; j++)
{
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][j];
if (sum == 0)
return VERTICAL;
}
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][ROWS-i-1];
if (sum == 0)
return DIAGONAL;
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][i];
if (sum == 0)
return DIAGONAL;
return WinFound;
}
private static void makeCard(int[][] card, int[] picks)
{
int count = 100;
int currPick = 0;
for (int i=0; i<count; i++){
currPick = (int)(Math.random() * 74) + 1;
System.out.print(" " + currPick + "\n");
picks[i] = currPick;
}
}
private static void announceWin(int numPicks)
{
}
private static boolean duplicate (int currPick, int[] picks, int numPicks)
{
for (int i = 0; i < numPicks; i++){
if (picks[i] == currPick){
return true;}
}
return false;
}
private static void finalCard (int[][] card)
{
Arrays.sort(card);
final String stringRep = Arrays.toString(card);
final String[] out =
stringRep.substring(1, stringRep.length() - 1).split("\\s*,\\s*");
System.out.println(Arrays.toString(out));
}
}
Try this:
System.out.print("\t" + (card[i][j] == 0 ? "X" : card[i][j]))
Why don't you just use a String[][] for the fields from the beginning? You can still compare String values with ints (with Integer.valueOf for instance) and this way you don't have to switch types runtime...

Categories

Resources