Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

compilation error, java programming, private vs public functions?

import java.util.*;

public class LoanCalculator

{

public LoanCalculator()

{

private double loanAmount; //dollars and cents

private double interestRate; //percent

private int loanPeriod; //years

}

Scanner input = new Scanner(System.in);

loanAmount = input.nextDouble();

System.out.print("The current loan amount is: " + loanAmount);

interestRate = input.nextDouble();

System.out.print("The interest rate is: " + interestRate);

loanPeriod = input.nextInt();

System.out.print("The loan period is " + loanPeriod + " years");

}

the error comes up at the private variables

it says illegal start of expression

1 Answer

Relevance
  • ?
    Lv 7
    8 years ago
    Favorite Answer

    Of course it will; your code doesn't really make that much sense.

    If you're going to use private variables, you should put them outside the constructor, like so:

    public class LoanCalculator {

        private double loadAmount;

        private double interestRate;

        private int loadPeriod;

        // other code here

    }

    It makes no sense to have a public or private variable inside the constructor (or any method, for that matter), since access modifiers only really apply for class-level variables.

    Also, your code for getting input from the user should be in its own method.

Still have questions? Get your answers by asking now.