Dear experts,

I am trying to code out my assignment but i made a problem with my

break;

within my

if (operator.equals(zero))

.
i have received compilation error stating the following:

HelloWorld.java: break outside switch or loop
Appreciate if someone can point out my mistake. Thank you.

import java.util.*;

public class HelloWorld {

    public static void main(String[] args) {

        int N; //declare variable to read the number of N lines in the input
        int type; // indicate the type
        String operator; // indicate if it's AND or OR
        int firstBit, secondBit; //indicate the Bits
        int zero = 0;

        Scanner sc = new Scanner (System.in);

        type = sc.nextInt(); // indicate the type 

        if (type ==1){
            N = sc.nextInt();
            for (int i=0;i <N; i++){
                operator = sc.nextLine(); // read the operator
                firstBit = sc.nextInt(); // read first bit
                secondBit = sc.nextInt(); // read second bit
            }
        }
        else if (type ==2){
            operator = sc.nextLine();
            if (operator.equals(zero))
                break;
            else {
                firstBit = sc.nextInt(); // read first bit
                secondBit = sc.nextInt(); // read second bit
            }
        }
        else if (type ==3){
            //read all the inputs till the end
            while(sc.hasNext()){
                operator = sc.nextLine(); // read the operator
                firstBit = sc.nextInt(); // read first bit
                secondBit = sc.nextInt(); // read second bit
            }
        }
        else{
            System.out.println("there are no such types");
        }

        System.out.println(operator + "" + firstBit + "" + secondBit);

    }
}

The purpose of using break:

To terminate a looping structure and jump to the line following the body of the loop or exit out of a switch-case construct.

Using break anywhere else would be totally illegal. You cant use it to exit an "if".

So think of a different mechanism to construct ur if-else construct.

Actually i am trying to exit the program

int zero = 0;
if operator.equals(zero);
//exit the program

I have tried using System.exit(1); but it throw an exception in thread.

What exception did it throw? Handle it...

why dont you do an while command over there?

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.