heroK
heroK

Reputation: 19

Program won't compile because of brackets

I'm writing a game in Java and after compiling it and debugging, the only error was that it reach the end of the file while parsing.

I looked and made sure all the brackets were balanced. I realized I was missing a closing bracket, so I added one where it should be. After compiling again, there were even more errors, as if there were misplaced brackets.

Below is the version without the bracket on line 94. I understand entire files are not ideal to post, but the issue is not specific to a few lines of the code that I am aware of.

import java.util.Scanner;
import java.util.Random;
public class NATO{

    public static void main(String[] args){

        float finalScore = 0;
        float numberCompleted = 0;
        float numberCorrect = 0;
        int gameLength = 10;
        boolean playing = true;

        String A = "Alpha";
        String B = "Beta";
        String C = "Charlie";
        String D = "Delta";
        String E = "Echo";
        String F = "Foxtrot";
        String G = "Golf";
        String H = "Hotel";
        String I = "India";
        String J = "Juliett";
        String K = "Kilo";
        String L = "Lima";
        String M = "Mike";
        String N = "November";
        String O = "Oscar";
        String P = "Papa";
        String Q = "Quebec";
        String R = "Romeo";
        String S = "Sierra";
        String T = "Tango";
        String U = "Uniform";
        String V = "Victor";
        String W = "Whiskey";
        String X = "Xylophone";
        String Y = "Yankee";
        String Z = "Zulu";

        while(playing == true) {

            try {
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);
                System.out.println("Welcome to the NATO speed game! Press [ENTER] to start...");
            }

            catch (IOEXCEPTION ioe) {
                System.out.println("Index out of bounds exception raised ^");
            }

            System.out.println("Game starting in 3...");
            System.out.println("Game starting in 2...");
            System.out.println("Game starting in 1...");

            while(numberCompleted <= gameLength){
                int rnd = Random().nextInt(args.length);
                String word = args[rnd];
                System.out.println(word.charAt(1));
                System.out.println(": ");
                String answerIncludingSpaces = System.in.nextLine;
                String answerUncapitalized = answerIncludingSpaces.replaceAll("\\s+", "");
                String answer = answerUncapitalized.charAt(1).toUpperCase;

                if(answer != word) {
                    float numberCompleted = numberCompleted + 1;
                }

                else if(answer == word){
                    float numberCorrect = numberCorrect + 1;
                }

                else {
                    System.out.println("Something went wrong. Please restart the game.");
                    break;
                }
            }

            float finalScore = (numberCorrect / numberCompleted);
            System.out.println("Game over.");
            System.out.println("Your score was " + finalScore + ". Would you like to play again? Y/N");

            if(System.nextLine.toUpperCase == "Y") {
                continue;
            }

            else {
                system.out.println("Thank you for playing!");
                break;
            }

        }

    }
}

The error message(s) it shows me when I do add the bracket on line 94:

https://pastebin.com/6E9DNdkA

Upvotes: 1

Views: 427

Answers (2)

Stephan Herrmann
Stephan Herrmann

Reputation: 8178

If the question is "why does addition of one bracket cause so many more compile errors to be reported?", then the answer is that presence of syntax errors (like missing bracket) will cause compilers to abort before even starting to detect more 'sophisticated' errors.

Think of it as a game in two (or more) rounds:

  • Can we (compiler & programmer) agree that this is syntactically correct Java?
    • if answer is "no", abort.
    • if answer is "yes", we proceed.
  • Can we agree that all names are resolvable?
    • if answer is "no" ...

Etc.

Motivation is: if we don't agree even on the syntax level, than any further errors are likely off the mark, i.e., more confusing than helping.

Upvotes: 1

mapeters
mapeters

Reputation: 1117

You are missing imports for InputStreamReader and BufferedReader. system and IOEXCEPTION are not capitalized correctly (and IOException will require an import when spelled correctly). You have some methods that you don't put parentheses after (e.g. nextLine on line 83). You need to put new before Random() on line 57.

Since finalScore is already declared as a float on line 7, you need to remove the float before it on line 79. You should only declare the type of a variable when you are creating it. With the second float declaration on line 79, it is trying to create a second finalScore variable which fails, because you can't have 2 variables with the same name. Removing the float declaration on that line will make it correctly modify the existing finalScore variable. This same idea applies to other places like numberCompleted on line 66 and numberCorrect on line 70.

Also, on line 63 you need to do Character.toUpperCase(answerUncapitalized.charAt(1)). Currently you are trying to call toUpperCase on a char, and primitives don't have methods, so that is incorrect.

But really, you should use an application that helps you with this, such as Eclipse. I copied and pasted this in Eclipse and it made it very easy to see where the issues are, and suggested appropriate fixes for some of them.

Upvotes: 5

Related Questions