Part_Time_Nerd
Part_Time_Nerd

Reputation: 1014

How do I convert an integer method into a string method in Java?

I am trying to create a dice program and I am need the output to be a string of 'One', 'Two', 'Three', etc. It is currently printing an output of 0 but that is because my OutputDice method is incorrect. When I take it out it passes the arguments as integers, but I need them as strings. How do I do that?

My code is as follows:

import java.util.Random;

public class Dice {

    private int Value;

    public void setValue(int diceValue)
    {
           Value = diceValue;
    }
    public int getValue()
    {
           return Value;
    }
    public void roll()
    {
         Random rand = new Random();
            Value = rand.nextInt(6) + 1;
    }
    public void OutputDice()
    {
        switch (Value)
        {
        case 1:
            System.out.println("One");
        case 2:
            System.out.println("Two");
        case 3:
            System.out.println("Three");
        case 4:
            System.out.println("Four");
        case 5:
            System.out.println("Five");
        case 6:
            System.out.println("Six");
        }   
    }
}

and

public class DiceRoll {

    public static void main(String[]args) {

        Dice firstDie = new Dice();
        Dice secondDie = new Dice();

        firstDie.OutputDice();
        secondDie.OutputDice();

        System.out.println("Dice 1: "+ firstDie.getValue());
        System.out.println("Dice 2: "+ secondDie.getValue());
    }
}

Upvotes: 0

Views: 72

Answers (2)

Part_Time_Nerd
Part_Time_Nerd

Reputation: 1014

Thanks for the help. It was a simple oversight. Here is what I got for the answer:

import java.util.Random;

    public class Dice {

        private int Value;

        public void setValue(int diceValue){
               Value = diceValue;
        }
        public int getValue(){
               return Value;
        }
        public void roll(){
             Random rand = new Random();
                Value = rand.nextInt(6) + 1;
        }
        public void OutputDice(){
            switch (Value)
            {
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            case 4:
                System.out.println("Four");
                break;
            case 5:
                System.out.println("Five");
                break;
            case 6:
                System.out.println("Six");
                break;
            }   
        }
    }

and

public class BrandonAssignment4 {

    public static void main(String[]args) {

        Dice firstDie = new Dice();
        Dice secondDie = new Dice();

        firstDie.roll();
        secondDie.roll();

        firstDie.OutputDice();
        secondDie.OutputDice();
    }
}

Upvotes: 0

Ryan
Ryan

Reputation: 1974

You also never assign a value to your dice. You need to call the roll() method before displaying the values. Also, With switch case statements you need to include breaks after your cases like so

public void OutputDice()
{
    switch (Value)
    {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
        break;
    case 3:
        System.out.println("Three");
        break;
    case 4:
        System.out.println("Four");
        break;
    case 5:
        System.out.println("Five");
        break;
    case 6:
        System.out.println("Six");
        break;
    }   
}

Upvotes: 3

Related Questions