When I tried to compile this code, it has one error says

ThreadsDemo.java:37: 'else' without 'if'
else {
^

I don't know what's wrong with that exactly?

import java.util.*;

class A extends Thread {
    private static int counter=0;
    private final int id=++counter;
    A() { start(); }
 
    public void run() {
    synchronized (this) {
    Random randomNumbers = new Random();
    int face;
    for (int count = 1; count <= 20; count++) {
    face = 1 + randomNumbers.nextInt(500); }
        while(true) {
            System.out.print("["+id+"] ");
            try {
                wait(face);
            } catch(InterruptedException e) { 
               System.out.print("\nThread ["+id+"] interrrupted");
               break;
            }
        }
    }
}

class ThreadsDemo {
    public static void main(String[] args) {
        ArrayList list=new ArrayList();
        for(int i=0; i<5; i++) list.add(new A());
        try {Thread.sleep(3000);
        } catch(InterruptedException e) { };
        for(int i=0; i<list.size(); i++) 
            ((A) (list.get(i))).interrupt();
        for(int i=0; i<list.size(); i++) {
        if ( (A) (list.get(i)).isAlive()) {
            System.out.println("Continue."); } }
        else {
            System.out.println("---All threads now ended---"); }
        }
    }
}

Dani AI

Generated

As hinted, the Java compiler is complaining because the else token is not being seen as part of an if/else pair. That is normally caused by the code structure above the else — most often an extra or missing curly brace, or a stray semicolon that terminates the if before the else appears. Since already tried removing braces without success, a more methodical approach will find the real mismatch.

Try these steps in order:

  • Reformat the file with an editor/IDE that auto-indents or shows matching braces. Highlighting the brace beside the else and its match often makes the problem obvious.
  • Count braces up to the else: the number of { and } before that line should leave the if block still open for the else. A quick mental or editor-assisted count narrows where the extra/missing brace lives.
  • Check for a stray semicolon after the if header (for example if (cond);) — that makes the if a null statement and leaves the else orphaned.
  • Isolate the area: comment out the block immediately above the else and recompile. If the error moves or disappears, the commented section contains the mismatch. Re-introduce small pieces until the compiler complains again.
  • Confirm the else is inside the same method/block as its if and not after a premature } that closed the method or loop.

A cautionary note: randomly deleting braces can hide the true problem by changing how the compiler parses the file. The compiler’s reported line is where it noticed the orphan else; the real cause is usually one or more lines earlier. Extracting a minimal reproducer (only the surrounding method) and reformatting that snippet almost always makes the offending brace or stray semicolon visible.

Recommended Answers

All 3 Replies

Extra } at end of line 36?
Adopt a standard for where you put your { and } and stick to it to avoid this kind of problem.

Extra } at end of line 36?
Adopt a standard for where you put your { and } and stick to it to avoid this kind of problem.

I don't get it. I mean I removed the } and it still doesn't work. I even tried the other way like remove most of these { } etc and it doesn't solve the problem.

Get yourself a code editor or IDE that will indent your code for you - that's the quickest way to spot {} errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.