Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I am trying to make a simple game and I don't know why but my array isn't printing. I'm still learning so I can't figure out why. I code using the NetBeans IDE. Here the code (When I run it, the array of players isn't printing)

TextBattle.java

Java
package textbattle;
import java.util.Arrays;
import java.util.Scanner;

public class TextBattle {
    
    public static void main(String[] args) {
        characterCreate();
    }
    
    public static void characterCreate(){
        
            String playerType;

            System.out.println("Create a character");
            Scanner beginCharScan = new Scanner(System.in);
            System.out.println("What is the name of your character: ");
            String addPlayer = beginCharScan.nextLine();
            System.out.println("What element is your character(water, fire, or grass)");
            playerType = beginCharScan.nextLine();
            Players player = new Players();
            switch(playerType){
                case "fire":
                    playerType = beginCharScan.nextLine();
                    player.Player(addPlayer, playerType);
                    break;
                case "water":
                    playerType = beginCharScan.nextLine();
                    player.Player(addPlayer, playerType);
                    break;
                case "grass":
                    playerType = beginCharScan.nextLine();
                    player.Player(addPlayer, playerType);
                    break;
                default:
                    System.out.println("That is not a valid type");
                    characterCreate();                        
            }
            
        }
    
        public static void Game(){
            
            System.out.println("What do you want to do?");
            Scanner choiceScan = new Scanner(System.in);
            System.out .println("Create another character (type 'Create')");
            System.out .println("Attack a different character (type 'Attack')");
            String choice = choiceScan.nextLine();
            
            switch(choice){
                case "Create":
                    characterCreate();
                    break;
                case "Attack":
                    break;
                default:
                    System.out.println("That is not a valid choice");
                    Game();
            }
        }
        
        public static void printPlayers(String playersArray[]){
            String printText = "Players exsisiting: " + Arrays.toString(playersArray);
        }
    
}


Players.java

Java
package textbattle;

import java.util.Arrays;

public class Players {
    
    int playerNum = 0;
    int setPlayerNum;
    
    public void Player(String addPlayer, String playerType){
        String playersArray[] = new String[4];
        String typeArray[] = new String[4];
        
        playersArray[playerNum] = addPlayer;
        typeArray[playerNum] = playerType;
        
        playerNum++;
        setPlayerNum = playerNum;
        
        TextBattle bat = new TextBattle();
        bat.printPlayers(playersArray);
        
    }
    
}


Thanks in advance!!

What I have tried:

I have tried looking up videos, "googling it", and as much as I could.
Posted
Updated 5-Sep-16 10:27am

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Ghostdragon777 5-Sep-16 17:55pm    
Thx for the help... the debugger really helped me
Patrice T 5-Sep-16 18:11pm    
Nice to hear it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900