"Null" is printed when I try to print out the array's value in java program

Habiballah Hezarehee :

My problem is that when I want to print out any element of my 2D array the Null is shown instead of the array's element value. How may I solve the problem? and why I get this output despite my array has members? (I am new to programming)

I tried to just print out one array member by writing the element number directly but still, there is some problem so the random numbers are not the problem. I have also tried to define a new array inside the pickCard() method in order to copy the cardList array to it but it did not help either.

I have 2 different classes, the first class is called Tutorial and it includes the main() method and the other is called Kortlek and all my codes are there.

This is the Tutorial class

package tutorial;

import java.util.Arrays;
import java.util.Scanner;

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

        // Using Scanner class to get in the input from user
        Scanner input = new Scanner(System.in);

        // We initialize our class Kortlek
        Kortlek newKortlek = new Kortlek();

        // Here we choose a nickname for user: 
        System.out.print("Please choose a name: ");
        String Username = input.next();
        String pcName = newKortlek.nickNamePC();


        String userAnswer;
        int userScore = 1;
        int pcScore = 2;
        do {
            System.out.println("You picked up: " + newKortlek.pickCard());
            System.out.println(pcName + " has picked up: "  + newKortlek.pickCard());
            System.out.println("Do you want to continue? write yes or no");
            userAnswer = input.next();

        } while (!userAnswer.equals("no"));
       System.out.println("Your score is: " + userScore);
       System.out.println(pcName + "'s score is: " + pcScore);
    }
}

This is the Kortlek class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tutorial;

import java.util.Random;

/**
 *
 * @author hezarehee
 */
public class Kortlek {

    Random r = new Random();
    /**
     * In this method we create 2D array in order to group each card color and its cards (1-13)
     */
    String cardList[][] = new String[3][12];

    public String[][] buildCardGame () {
        String[] farg = {"Spader", "Hjarter", "Ruter", "Klover"};
        String[] nummer = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "Ace"};


        for (int i=0; i < farg.length; i++) {
            for (int j=0; j < nummer.length; j++) {
                cardList[i][j] = farg[i] + " " + nummer[j];
            }

        }
        return cardList;
    }

    /**
     * Here we make a method that let computer to choose a name from given names in array
     */
    public String nickNamePC () {
        String[] nickName = {"Daivd", "Rahim", "Michael", "Sara", "Marie", "Jenny"};

        int low = 0; 
        int high = 5; 
        int result =  r.nextInt(high-low) + low;
        String chosenName = nickName[result];
        return chosenName;
    }

    /**
     * Here we each time pick up a card from our 2D Array cardList[][] 
     */
    public String pickCard() {
        int takeColor = r.nextInt(3-1) + 1;
        int takeNumber = r.nextInt(12-1) + 1;

        // we put our random numbers into the array carDList
        String pickedCard = cardList[takeColor][takeNumber];
        return pickedCard;
    }

}

I need to make a card game (Rank and colors), in this game the users play against the program, first in the array cardList, I tried to create 52 different cards for in 4 groups (Clubs, Diamonds, Hearts, Spades). I made the array inside method called buildCardGame().

By method pickCard(), I try to pick a random card by adding 2 random numbers from 0-3 for the color and 0-12 for the rank. But when I print it out so I get null.

Madplay :

First, You need to modify where cardlist is initialized.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tutorial;

import java.util.Random;

/**
 *
 * @author hezarehee
 */
public class Kortlek {

    Random r = new Random();
    /**
     * In this method we create 2D array in order to group each card color and its cards (1-13)
     */
    String cardList[][] = new String[4][13];

    // ArrayIndexOutOfBoundsException
    // String cardList[][] = new String[3][12];

    public String[][] buildCardGame () {
        String[] farg = {"Spader", "Hjarter", "Ruter", "Klover"};
        String[] nummer = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "Ace"};


        for (int i=0; i < farg.length; i++) {
            for (int j=0; j < nummer.length; j++) {
                cardList[i][j] = farg[i] + " " + nummer[j];
            }

        }
        return cardList;
    }

    /**
     * Here we make a method that let computer to choose a name from given names in array
     */
    public String nickNamePC () {
        String[] nickName = {"Daivd", "Rahim", "Michael", "Sara", "Marie", "Jenny"};

        int low = 0;
        int high = 5;
        int result =  r.nextInt(high-low) + low;
        String chosenName = nickName[result];
        return chosenName;
    }

    /**
     * Here we each time pick up a card from our 2D Array cardList[][] 
     */
    public String pickCard() {
        int takeColor = r.nextInt(3-1) + 1;
        int takeNumber = r.nextInt(12-1) + 1;

        // we put our random numbers into the array carDList
        String pickedCard = cardList[takeColor][takeNumber];
        return pickedCard;
    }

}


Second, buildCardGame method must be called

package tutorial;

// import java.util.Arrays; // not used
import java.util.Scanner;

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

        // Using Scanner class to get in the input from user
        Scanner input = new Scanner(System.in);

        // We initialize our class Kortlek
        Kortlek newKortlek = new Kortlek();

        // initialize the members
        newKortlek.buildCardGame(); 

        // Here we choose a nickname for user:
        System.out.print("Please choose a name: ");
        String Username = input.next();
        String pcName = newKortlek.nickNamePC();


        String userAnswer;
        int userScore = 1;
        int pcScore = 2;
        do {
            System.out.println("You picked up: " + newKortlek.pickCard());
            System.out.println(pcName + " has picked up: "  + newKortlek.pickCard());
            System.out.println("Do you want to continue? write yes or no");
            userAnswer = input.next();

        } while (!userAnswer.equals("no"));
        System.out.println("Your score is: " + userScore);
        System.out.println(pcName + "'s score is: " + pcScore);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=133814&siteId=1