nbhuiya
nbhuiya

Reputation: 39

Passing input from scanner to a method

I am writing a Craps dice program for Java. I can't figure out how to pass the Scanner "playerName" from the main method into the playgame method and rolldice method. I want to be able to have the program print the player name in both of these methods. (Example "Jake rolled a 4 and 5 for a total of 9" instead of "Player rolled a 4 and 5 for a total of 9". I also get the warning Resouce Leak: 'sc' is never closed. I am new to the programming scene so if you could explain in simple terms i would greatly appreciate it. Any suggestions to improve the program are also welcome.

import java.util.Scanner;
import java.util.Random;
public class crapsgame
{
    //generates random number to be used in method rollDice
    private Random randomNumbers = new Random();

    //enumeration of constants that represent game status
    private enum Status {WIN, LOSE, CONTINUE};

    //represents possible outcomes of rolling the dice
    private final static int two = 2;
    private final static int three = 3;
    private final static int seven = 7;
    private final static int eleven = 11;
    private final static int twelve = 12;

    public static void main (String[]args)
    {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter a player name: ");
        String playerName = sc.nextLine();
        crapsgame game = new crapsgame(); //created object of class "crapsgame"
        game.playgame(); //tells crapsgame object "game" to invoke"playgame" method
    }

    //method to play game
    public void playgame()
    {
        int currentPoint = 0; //holds point value for current roll
        Status gameResult; //contains one of enumeration values
        int sumofDice = rollDice(); //sum after first roll
        //determines if won, lost, or continue
        switch (sumofDice)
        {
            case seven:
            case eleven:
                gameResult = Status.WIN;
                break;
            case two:
            case three:
            case twelve:
                gameResult = Status.LOSE;
                break;
            //game continues if above conditions are not met
            default:
                gameResult = Status.CONTINUE;
                currentPoint = sumofDice;
                System.out.printf("Point is %d\n", currentPoint);
        }

        while (gameResult == Status.CONTINUE)
        {
            sumofDice = rollDice();
            if (sumofDice == currentPoint)
                gameResult = Status.WIN;
            else
                if (sumofDice == seven)
                    gameResult = Status.LOSE;
        }
        if (gameResult == Status.WIN)
            System.out.println("Player wins");
        else 
            System.out.println ("Player loses");
    }

    public int rollDice()
    {
        //choose a random number from 1-6
        int firstroll = 1 + randomNumbers.nextInt(6);
        int secondroll = 1 + randomNumbers.nextInt(6);
        int sum = firstroll + secondroll;
        System.out.printf("Player rolled %d and %d for a total of %d\n", firstroll, secondroll, sum);
        return sum;
    }
}

Upvotes: 0

Views: 4672

Answers (2)

Joseph James
Joseph James

Reputation: 168

After you get the name from the user you have to pass playerName as a String argument to any function where you want to use it. I also changed your class name to camel case.

import java.util.Scanner;
import java.util.Random;
public class CrapsGame {
    //generates random number to be used in method rollDice
    private Random randomNumbers = new Random();

    //enumeration of constants that represent game status
    private enum Status {WIN, LOSE, CONTINUE};

    //represents possible outcomes of rolling the dice
    private final static int two = 2;
    private final static int three = 3;
    private final static int seven = 7;
    private final static int eleven = 11;
    private final static int twelve = 12;

    public static void main (String[]args) {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter a player name: ");
        String playerName = sc.nextLine();
        CrapsGame game = new CrapsGame(); //created object of class "CrapsGame"
        game.playgame(playerName); //tells CrapsGame object "game" to invoke"playgame" method
    }
    //method to play game
    public void playgame(String playerName) {
        int currentPoint = 0; //holds point value for current roll
        Status gameResult; //contains one of enumeration values
        int sumofDice = rollDice(playerName); //sum after first roll
        //determines if won, lost, or continue
        switch (sumofDice)
        {
            case seven:
            case eleven:
            gameResult = Status.WIN;
            break;
            case two:
            case three:
            case twelve:
            gameResult = Status.LOSE;
            break;
            //game continues if above conditions are not met
            default:
            gameResult = Status.CONTINUE;
            currentPoint = sumofDice;
            System.out.printf("Point is %d\n", currentPoint);
        }

        while (gameResult == Status.CONTINUE)
        {
            sumofDice = rollDice(playerName);
            if (sumofDice == currentPoint)
                gameResult = Status.WIN;
            else if (sumofDice == seven)
                gameResult = Status.LOSE;
        }
        if (gameResult == Status.WIN)
            System.out.println(playerName + " wins");
        else 
            System.out.println (playerName + " loses");
    }
    public int rollDice(String playerName)
    {
        //choose a random number from 1-6
        int firstroll = 1 + randomNumbers.nextInt(6);
        int secondroll = 1 + randomNumbers.nextInt(6);
        int sum = firstroll + secondroll;
        System.out.printf("%s rolled %d and %d for a total of %d\n", playerName, firstroll, secondroll, sum);
        return sum;
    }
}

Upvotes: 1

Brandon Laidig
Brandon Laidig

Reputation: 420

In order to pass parameters into a method they need to be declared in the method declaration

public void playGame(String playerName) {
   ...
}

You then call into the method in the following way

String playerName = sc.nextLine();
playGame(playerName);

As far as the warning you're receiving, you need to close the scanner when you're done using it, i.e

sc.close();

I hope that helps and welcome to programming!

Upvotes: 0

Related Questions