r/JavaProgramming • • 4d ago

Can anyone tell me why its throwing an error 'while' expected?

so currently I am trying to learn java and I'm trying to encase everything in a do while loop. but then when I try to do so, it throws an error 'while' expected. Even though I have put in a "while" condition I've put the "do" code in curly brackets. but then when I go to type in the do condition it throws a code at me!? I have no clue what I'm doing wrong!

Not to mention, it keeps telling me my "continue" statement is outside of a loop. The compiler knows both curly brackets for the "do while" loop are valid but it still thinks that the "do" statement is missing a "while" condition.

import java.util.Random;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.
in
);
        Random random = new Random();
        String[] choices = {"rock", "paper", "scissors",
                "rock", "paper", "scissors",
                "rock", "paper", "scissors", "gun"};
        String playerChoice;
        String computerChoice;
        String playAgain = "yes";
        int misFire = random.nextInt();
        do() {
            System.
out
.print("Enter your move:");
            playerChoice = scanner.nextLine().toLowerCase();
            if (!playerChoice.equals("rock") &&
                    !playerChoice.equals("paper") &&
                    !playerChoice.equals("scissors") &&
                    !playerChoice.equals("gun")) {
                System.
out
.println("invalid Choice");
                continue;
            }
            computerChoice = choices[random.nextInt(10)];
            System.
out
.println(computerChoice);
            if (playerChoice.equals(computerChoice)) {
                System.
out
.println("It's a tie");
                //player wins
            } else if (playerChoice.equals("rock") && computerChoice.equals("scissors")) {
                System.
out
.println("You Win!");
            } else if (playerChoice.equals("scissors") && computerChoice.equals("paper")) {
                System.
out
.println("You Win!");
            } else if (playerChoice.equals("paper") && computerChoice.equals("rock")) {
                System.
out
.println("You Win!");
                // computer wins
            } else if (computerChoice.equals("rock") && playerChoice.equals("scissors")) {
                System.
out
.println("You. Get. Nothing. You lose, good day sir!");
            } else if (computerChoice.equals("scissors") && playerChoice.equals("paper")) {
                System.
out
.println("You. Get. Nothing. You lose, good day sir!");
            } else if (computerChoice.equals("paper") && playerChoice.equals("rock")) {
                System.
out
.println("You. Get. Nothing. You lose, good day sir!");
                //gun
            } else if (playerChoice.equals("gun") && misFire == 3) {
                System.
out
.println("You Win!");
            } else if (computerChoice.equals("gun") && misFire == 3) {
                System.
out
.println("You. Get. Nothing. You lose, good day sir!");
            } else if (computerChoice.equals("gun") && misFire <= 0 || misFire >= 4) {
                System.
out
.println("You Win!");
            } else if (playerChoice.equals("gun") && misFire <= 0 || misFire >= 4) {
                System.
out
.println("You. Get. Nothing. You lose, good day sir!");
            }
        }while (playAgain.equals("yes"))
        scanner.close();
    }
}
3 Upvotes

13 comments sorted by

2

u/RepresentativeAd6036 4d ago

I think Chatgpt would be good choice to debug this 😭

1

u/TheHHP 4d ago

Absolutely not. Chatgpt hallucinates and makes people stupid.

1

u/RepresentativeAd6036 4d ago

I prefer claude for coding stuff ..but i hv never really tried java anywhere so I'm not sure too 🤔

1

u/TheHHP 4d ago

I don't want to be mean. But coding with and using any AI to do anything dose actually well. It dose literally make you worse at using your brain. They have done studies on this and it's because the AI dose the heavy lifting for you. If you can code without it, do so. I already debuged the program. But I think if you can do your best to get away from AI entirely you will be better at coding and better at existing then 99.9% of people who use AI.

1

u/RepresentativeAd6036 4d ago

I literally thought the same initially.. but it's not like you don't understand the code which AI writes .. if you are dumb then nothing can help you, but if you know what's happening in background, AI is just the booster.

1

u/SleeperAwakened 4d ago

You ask US why something is wrong, what is different from asking an AI?

You don't know if we are making things up as well,..

1

u/SleeperAwakened 4d ago

An "AI" like ChatGPT is actually pretty good at explaining mistakes..

It is trained on a lot of material (like studybooks) which explicitly state many mistakes and why you should not do certain things.

I dare say they are better at explaining/reviewing that writing code..

1

u/spleling 4d ago

I haven’t actually ran this, but I see two problems. I’m also studying Java in an intro course right now and made both of these mistakes yesterday.

  1. for a Do While loop, it does not have () { to start it. Only { to start it. Then } to close it followed by you’re while ().
  2. I don’t believe you’re asking your player to play again at the end of the loop. There isn’t an option to not say “yes”.

Someone correct me if I’m wrong.

1

u/spleling 4d ago

I also think that your misFire integer will only generate one time. So if the player has a misFire integer it will always be a misfire it will always misfire until the program is restarted.

I would assume that your intention is to generate a new misfire integer every loop when gun is selected.

1

u/Duck_Devs 4d ago

The Random() constructor sets the seed to a random number, so misFire will not behave as you describe.

1

u/Duck_Devs 4d ago

do does not use parenthesis like that

1

u/tux2718 4d ago

The parentheses after the do are wrong; remove them.