#Yahtzee Game

1 messages · Page 1 of 1 (latest)

robust steppe
#

Hello!

I'm trying to make a small Yahtzee game in Java, however when I am checking to see which number the user wants to have as their 3 of a kind or 4 of a kind score taken, I get an input mismatch error and I don't quite know why. It points to this area as the error. There is a lot of code so I didn't paste all of it, but if more would help let me know! Thank you for reading!

else
            {
                System.out.println("Where would you like to allocate your points in the lower section?");
                String lowerSec = keyboard.next();
                
                if (lowerSec.equals("3") || lowerSec.equals("4"))
                {
                    System.out.println("For which number?");
                    int val = keyboard.nextInt();

                    p1Cup.threeOrFours(val);
                    updated = true;
                }
bright vaporBOT
#

<@&987246883653156906> please have a look, thanks.

bright vaporBOT
#

@robust steppe

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.

robust steppe
#

Ahhh okay, thank you so much!

#

Oh dear, I used next quite a lot, so now I'm encountering some issues when I run everything else.

do {
            // allocatePoints(p1Cup);
            
            System.out.println("Would you like to update the upper section or lower section?");
            String answer = keyboard.nextLine();
            if (answer.equalsIgnoreCase("upper") || answer.equalsIgnoreCase("upper section"))
            {
                System.out.println("Which numbers would you like to update your tally with?");
                diceValue = keyboard.nextInt();
                
                if (p1Cup.isLegal(diceValue))
                {
                    updated = true;
                    p1Cup.updateTally(diceValue);
                }
            }
            
            else
            {
                System.out.println("Where would you like to allocate your points in the lower section?");
                String lowerSec = keyboard.nextLine();
                
                if (lowerSec.equals("3 of a kind") || lowerSec.equals("4 of a kind"))
                {
                    System.out.println("For which number?");
                    int val = Integer.parseInt(keyboard.nextLine());

                    p1Cup.threeOrFours(val);
                    updated = true;
                }
                
                else if (lowerSec.equalsIgnoreCase("chance"))
                {
                    p1Cup.chance();
                    updated = true;
                }
                
                else if (lowerSec.equalsIgnoreCase("full house"))
                {
                    System.out.println("For which two numbers? Separated by a space");
                }
            }
            }
        while(!updated);
bright vaporBOT
robust steppe
# trail owl like ?

It defaults to asking where in the lower section I want to allocate my numbers to.

Like :
Would you like to update the upper section or lower section?
Where would you like to allocate your points in the lower section?
4 of a kind
For which number?
4

ashen crane
#

scanner.nextLine() and Integer.parseInt(scanner.nextLine())

robust steppe
robust steppe
ashen crane
#

then lets re-evaluate

#

also if you are using java 25 you can skip the scanner entirely and use IO.readln()

robust steppe
robust steppe
# ashen crane just as a step 1 replace every `next**` method with `nextLine`

Now I am extremely confused, I had everything in a separate area to test but when I put everything together I am getting the same thing happening where it says:
Would you like to update the upper section or lower section?
Where would you like to allocate your points in the lower section?

Without asking for input

Here is how it is at the moment

do {
                System.out.println("Would you like to update the upper section or lower section?");
                String answer = keyboard.nextLine();
                if (answer.equalsIgnoreCase("upper") || answer.equalsIgnoreCase("upper section"))
                {
                    System.out.println("Which numbers would you like to update your tally with?");
                    int diceValue = Integer.parseInt(keyboard.nextLine());
                    
                    if (p1Cup.isLegal(diceValue))
                    {
                        updated = true;
                        p1Cup.updateTally(diceValue);
                    }
                }
                
                else
                {
                    System.out.println("Where would you like to allocate your points in the lower section?");
                    String lowerSec = keyboard.nextLine();
                    
                    if (lowerSec.equals("3 of a kind"))
                    {
                        System.out.println("For which number?");
                        int val = Integer.parseInt(keyboard.nextLine());
    
                        p1Cup.ofThrees(val);
                        updated = true;
                    }
                    
                    else if (lowerSec.equals("4 of a kind"))
                    {
                        System.out.println("For which number?");
                        int val = Integer.parseInt(keyboard.nextLine());
    
                        p1Cup.ofFours(val);
                        updated = true;
                    }
bright vaporBOT
ashen crane
#

when it prompts you just upload as a file

robust steppe
bright vaporBOT
ashen crane
#

ah here

#

choice = keyboard.next();

#

i meant replace everything with .nextLine()

robust steppe
#

Now it works completely fine, thank you for catching that!

ashen crane
#

now that i'm reading your code more

#
    private ArrayList<Integer> diceValues;  // momentary values of dice throw
    private ArrayList<Integer> tallyValues; // record of player selection of dice counts
    Scanner keyboard = new Scanner(System.in);

    public DiceCup() {
        diceValues = new ArrayList<Integer>(Arrays.asList(0, 0, 0, 0, 0));
        tallyValues = new ArrayList<Integer>(Arrays.asList(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1));
        // keep track of 1s  2s  3s  4s  5s 6s chance 3k 4k FH  LT  ST yahtzee
    }

#

it seems like you don't ever .add or .remove from diceValues or tallyValues

#

you just get, set, etc.

#

so you don't actually need an ArrayList - the list that Arrays.asList gives you will work fine

#
    private List<Integer> diceValues;  // momentary values of dice throw
    private List<Integer> tallyValues; // record of player selection of dice counts
    Scanner keyboard = new Scanner(System.in);

    public DiceCup() {
        diceValues = Arrays.asList(0, 0, 0, 0, 0);
        tallyValues = Arrays.asList(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
        // keep track of 1s  2s  3s  4s  5s 6s chance 3k 4k FH  LT  ST yahtzee
    }
robust steppe
ashen crane
#

accordingly, you'd need to update these methods to return List<Integer> instead of specifically ArrayList<Integer>

#

    public ArrayList<Integer> getTally() {
        // returns the array list of the tallyValues 

        return tallyValues;
    }

    public ArrayList<Integer> getDice() {
        // returns the array list of diceValues

        return diceValues;
    }
#

next

#

notice how you have a comment here that describes what getTally does?

#
    public List<Integer> getTally() {
        // returns the array list of the tallyValues 

        return tallyValues;
    }
#

if you move that comment to the top of the method and add one more slash

#
    /// returns the array list of the tallyValues
    public List<Integer> getTally() {
        return tallyValues;
    }
#

this is a "javadoc" comment

#

and you can run the javadoc tool on your code to produce documentation

#

also your editor will give hints

#

tl;dr, put that comment at the top with an extra slash

robust steppe
ashen crane
#

are you maybe using a really old java version?

#

oh here

#
            while(updated = false);
#

you want two equals signs

#
            while(updated == false);
#

or just

#
            while(!updated);
#

but remember that just one equals sign is for assignment, not checking if things are equal

#

similarly

#
p1Cup.emptySpaces() == true && p2Cup.emptySpaces() == true
#

you can just write

#
p1Cup.emptySpaces() && p2Cup.emptySpaces()
robust steppe
robust steppe
robust steppe
ashen crane
#

now as just a spoiler, once you are out of bluej and writing the newest java, this is your program

bright vaporBOT
# ashen crane

I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.

ashen crane
#

(in case you are starting to get annoyed with java - happens)

robust steppe
ashen crane
#

you run java src/Main.java or whatever the file is and it "just works"

#

its strangely harder to explain whats different to you than it would be if you knew nothing

#

but the most notable thing for you i think is that

#
  1. public static void main(String[] args) can just be void main()
  2. IO.println/IO.readln replace System.out.println and scanner
#

(all old code still works, just new stuff)

robust steppe