Search code examples
javacurly-braces

Missing braces but I never missed it (Java)


It happens when I put "if conditional". Sorry to bring full of code, but I have tried to find what is wrong whole day, but I'm still looking for problem.

import javax.swing.JOptionPane;

class Accounting2 {
    public static double VOS;
    public static double vatRate = 0.1;
    public static double expenseRate = 0.3;
    
    public static double person1, person2, person3;
    public static double[] dividend = new double[3];
    
    public static void printReport(double person1, double person2, double person3) {
        System.out.println("Value of supply : " + VOS);
        System.out.println("VAT : " + getVAT());
        System.out.println("Price : " + getPrice());
        System.out.println("Income : " + getIncome());
        System.out.println("Person1 : " + person1 +
                "\nPerson2 : " + person2 +
                "\nPerson3 : " + person3);
    }
        
    public static double getIncome() {
        return VOS - getExpense();
    }
    public static double getExpense() {
        return VOS * expenseRate;
    }
    public static double getPrice() {
        return VOS + getVAT();
    }
    public static double getVAT() {
        return VOS * vatRate;
    }
    
    public static double toPutGetIncome = getIncome(); // error hapeens here (Syntax error on token ";", { expected after this token)
    
    // The "if conditional" starts from here
    if( toPutGetIncome >= 10000 ) {
        for (int n = 0; n < 3; n++) {
            dividend[n] = Double.parseDouble(JOptionPane.showInputDialog("Enter a Dividend of Person" + (n+1)));
            if(dividend[0] + dividend[1] + dividend[2] > 1) { System.out.println("Dividend over"); return; }
        }
        if(dividend[0] + dividend[1] + dividend[2] < 1) { System.out.println("Dividend short"); return; }
    }
    
    
    if( toPutGetIncome < 10000 ) { person1 = getIncome(); person2 = 0; person3 = 0; }
    else{ person1 = getIncome()*dividend[0]; person2 = getIncome()*dividend[1]; person3 = getIncome()*dividend[2]; }
    // ends
        
}

public class AccountingApp2 {

    public static void main(String[] args) {
        
        Accounting2.VOS = Double.parseDouble(JOptionPane.showInputDialog("Enter a VOS"));
        if( Accounting2.VOS <= 0 ) { System.out.println("Wrong VOS"); return; }
        
        Accounting2.printReport(Accounting2.person1, Accounting2.person2, Accounting2.person3);
        
    }

} // the other error happens here (Syntax error, insert "}" to complete ClassBody)

I'm not sure it helps you I was studying outer class and inner class. All of the codes were in the public class AccountngApp2. I made another class Accounting2 to move methods from AccountingApp2 and the error happened while I move methods.


Solution

  •     // The "if conditional" starts from here
        if( toPutGetIncome >= 10000 ) {
            for (int n = 0; n < 3; n++) {
                dividend[n] = Double.parseDouble(JOptionPane.showInputDialog("Enter a Dividend of Person" + (n+1)));
                if(dividend[0] + dividend[1] + dividend[2] > 1) { System.out.println("Dividend over"); return; }
            }
            if(dividend[0] + dividend[1] + dividend[2] < 1) { System.out.println("Dividend short"); return; }
        }
        
        
        if( toPutGetIncome < 10000 ) { person1 = getIncome(); person2 = 0; person3 = 0; }
        else{ person1 = getIncome()*dividend[0]; person2 = getIncome()*dividend[1]; person3 = getIncome()*dividend[2]; }
        // ends
    

    This is invalid. An if statement must be inside a method, constructor, or static initializer.