Semicolon expected in java method [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
When I compile this, I get an error stating a semicolon is expected after (String[] args). I can't wrap my head around it. If I do add one, I get 10+ errors unsurprisingly
import java.util.;
import java.text.;
public class PayCheck
{
private String givenName;
private double totalWage;
private double totalHours;
private double gross, netPay, tax, ssnTax;
static String employer="PrismHR";
static double taxRate = 0.22;
static double ssRate = 0.06;
public static void main (String[] args) PayCheck()
{
Scanner std = new Scanner(System.in);
System.out.println("Enter employee name: ");
givenName = std.nextLine();
System.out.println("Enter pay rate and hours worked: ");
totalWage = std.nextDouble();
totalHours = std.nextDouble();
gross = totalWage*totalHours;
tax = gross*taxRate;
ssnTax = gross*ssRate;
netPay = gross-tax-ssnTax;
}
public PayCheck(String name, double wage, double hours)
{
this.givenName = name;
this.totalHours = hours;
this.totalWage = wage;
gross = totalWage * totalHours;
tax = gross * taxRate;
ssnTax = gross * ssRate;
netPay = gross - tax - ssnTax;
}
public String toString()
{
String emptyString = "";
NumberFormat formats = new DecimalFormat(".##");
emptyString += "Employer: " + employer + "\n";
emptyString += "Employee: " + givenName +"\n";
emptyString += "Gross income: $" + formats.format(gross)+"\n";
emptyString += "Federal Tax: $" + formats.format(tax) + "\n";
emptyString += "Social Security Tax: $" + formats.format(ssnTax) +"\n";
emptyString += "Net Pay: " + formats.format(netPay);
return emptyString;
}
}

Oftentimes, and this goes for all programming languages, the error itself may not be accurate. In your case, you have function call just kind of... there.
public static void main (String[] args) PayCheck()
{
...
}
Get rid of PayCheck(). I'm not sure what you're trying to do with it, but that is definitely causing the error.

Related

NetBeans : Trying to create a class file to work with another program

Having trouble compiling these two files to run together.
I get a "can't find or load main class" error or a "erroneous tree" error.
Never asked for help on here before, hope this works :)
package savingsaccount;
import java.util.Scanner;
public class SavingsAccount
{
public static void main(String[] args)
{
double begginingBalance, deposit, withdraw;
int months;
double monthlyRate;
double plus = 0.0;
double minus = 0.0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the balance at beggining of " +
"accounting period.");
begginingBalance = keyboard.nextDouble();
System.out.println("Please enter number of months in current " +
"accounting period.");
months = keyboard.nextInt();
System.out.println("Enter the annual interest rate.");
monthlyRate = keyboard.nextDouble();
a7main accounting = new a7main();
for(int month = 1; month<=months; month++)
{
System.out.println("Enter the amount of deposits for month " +
month + " : ");
plus = keyboard.nextDouble();
accounting.deposits(plus);
System.out.println("Enter the amount of withdrawals for" +
" month " + month + ": ");
minus = keyboard.nextDouble();
accounting.withdrawals(minus);
accounting.interest(monthlyRate);
}
System.out.println("The account balance is: " +
accounting.getBalance());
System.out.println("The total amount of deposits is:" + plus);
System.out.println("The total amount of withdrwals is: " + minus);
System.out.println("The earned interest is: " +
accounting.getRate());
}
}
HERE IS THE CLASS FILE
I am trying to use the methods in this file to calculate and hold the values from the other file.
public class a7main
{
private double totalBalance;
private double interestRate;
public a7main(double balance,double rate)
{
totalBalance = balance;
interestRate = rate;
}
public void deposits(double deposit)
{
totalBalance = totalBalance+deposit;
}
public void withdrawals(double withdraw)
{
totalBalance = totalBalance-withdraw;
}
public void interest(double rate)
{
interestRate = totalBalance*rate;
}
public double getBalance()
{
return totalBalance;
}
public double getRate()
{
return interestRate;
}
}
#Alex Goad -
Add package name in a7main class
You have created parameterized constructor and no default constructor.
a7main accounting = new a7main();
The above line will look for default constructor like
public a7main(){
}

UserInput Method keeps assigning a different value in java

this is my first question on the site. I am a fresh CS student needing some help with something that is probably really simple. The code as is will compile. When I enter in the values as the program asks, it stores the values wrong. It will store the right values for gross pay and savings rate but the IRA rate comes back as 100% even when entered at 6.9 and it seems it stores the IRA rate in saveAmount. Please halp me figure out what I am doing wrong here.
import java.text.DecimalFormat;
import java.util.*;
public class CollinDunn_1_05 {
static Scanner console = new Scanner(System.in);
static DecimalFormat formatCash = new DecimalFormat("#,###.00");
static double iraTotal = 0.0;
static double saveAmount = 0.0;
static double totalSave = 0.0;
static String line = "";
public static void main (String [] args) {
// Input variables
double grossPay = 0.0; // The gross pay from a users paycheck
double saveRate = 0.0; // This is the user entered savings rate
double iraRate= 0.0; // The IRA investment rate
String whichOne = ""; // A temp variable to pass a string type into UserInput
printInfo();
grossPay = userInput("gross pay");
saveRate = userInput("savings rate");
iraRate = userInput("IRA rate");
iraTotal = iraAmount(grossPay, iraRate);
saveAmount = savingsAmount(grossPay, saveRate);
outputResults(grossPay, saveRate, saveAmount, iraRate, iraTotal);
return;
} // End Main
public static void printInfo() {
System.out.println ("This program uses methods to calculate \n"
+ "savings amounts and IRA investment amounts \n"
+ "from user input consisiting of their gross pay, \n"
+ "their desired savings rate and IRA rate, made by "
+ " Collin Dunn");
return;
} // End ProgramInfo
public static double userInput(String whichOne) {
double saveMe = 0.0;
System.out.print("Please enter your " + whichOne + ": ");
saveMe = console.nextDouble();
return saveMe;
} // End userInput
public static double iraAmount(double grossPay, double iraRate) {
iraTotal = grossPay * (iraRate / 100.0);
return iraTotal;
} // End iraAmount
public static double savingsAmount(double grossPay, double saveRate) {
saveAmount = grossPay * (saveRate / 100.0);
return saveAmount;
} // End savingsAmount
public static void outputResults(double grossPay, double saveRate, double iraRate,
double saveAmount, double iraTotal) {
totalSave = saveAmount + iraTotal;
System.out.print ("With a gross pay of $" + formatCash.format(grossPay)
+ ", a savings rate of %" + formatCash.format(saveRate)
+ " and a IRA rate of %" +formatCash.format(iraRate)
+ ".\n Your savings amount will be $" + formatCash.format(saveAmount)
+ ", with a investment amount of $" + formatCash.format(iraTotal)
+ ".\n Which leaves you with a total savings of $" +
+ totalSave + ". Way to go for paying yourself!" );
return;
} // End outputResults
} //End Class
Your only issue is the order of arguments you pass to or have set on the outputResults() method.
Change the signature of the method to:
public static void outputResults(double grossPay, double saveRate, double saveAmount, double iraRate, double iraTotal) {
Which now matches how you call the method:
outputResults(grossPay, saveRate, saveAmount, iraRate, iraTotal);
Let me make a couple of additonal suggestions:
1) You are consistently naming arguments in your method signatures the same names as global variables, which makes it confusing which is which when accessing the variable in the method. Either avoid using the same names for the method input variables, or use something like this.amount = amount to make it more obvious of your intention.
2) Avoid static unless you have a valid reason to use it (which is pretty rare).
Instead, take advantage of Java's Object oriented nature and create an instance of your class in the main method and call methods on that instance. This will make your code more readable, reliable, and reusable.
3) In a method that returns type 'void', you don't need to add the empty return; statement.
To correct your issue and also demonstrate the points I listed, I have refactored your code and provided it below. By the way, you have a lot of potential. Despite the fact there are a few details you can improve, for being a first year CS student, your code is well written and thought out. Good job!
import java.text.DecimalFormat;
import java.util.*;
public class CollinDunn_1_05 {
DecimalFormat formatCash = new DecimalFormat("#,###.00");
double iraTotal = 0.0;
double saveAmount = 0.0;
double totalSave = 0.0;
double grossPay = 0.0; // The gross pay from a users paycheck
double saveRate = 0.0; // This is the user entered savings rate
double iraRate= 0.0; // The IRA investment rate
public CollinDunn_1_05(double gross, double saveRt, double iraRt){
this.grossPay = gross;
this.saveRate = saveRt;
this.iraRate = iraRt;
}
public void calculate(){
calcIraAmount();
calcSavingsAmount();
}
public static void main (String [] args) {
Scanner scanner = new Scanner(System.in);
printInfo();
CollinDunn_1_05 program = new CollinDunn_1_05(
userInput("gross pay", scanner),
userInput("savings rate", scanner),
userInput("IRA rate", scanner)
);
program.calculate();
program.outputResults();
} // End Main
public static void printInfo() {
System.out.println ("This program uses methods to calculate \n"
+ "savings amounts and IRA investment amounts \n"
+ "from user input consisiting of their gross pay, \n"
+ "their desired savings rate and IRA rate, made by "
+ " Collin Dunn");
return;
} // End ProgramInfo
public static double userInput(String whichOne, Scanner console) {
double saveMe = 0.0;
System.out.print("Please enter your " + whichOne + ": ");
saveMe = console.nextDouble();
return saveMe;
} // End userInput
public void calcIraAmount() {
iraTotal = grossPay * (iraRate / 100.0);
} // End iraAmount
public void calcSavingsAmount() {
saveAmount = grossPay * (saveRate / 100.0);
} // End savingsAmount
public void outputResults() {
totalSave = saveAmount + iraTotal;
System.out.print ("With a gross pay of \$" + formatCash.format(grossPay)
+ ", a savings rate of %" + formatCash.format(saveRate)
+ " and a IRA rate of %" +formatCash.format(iraRate)
+ ".\n Your savings amount will be \$" + formatCash.format(saveAmount)
+ ", with a investment amount of \$" + formatCash.format(iraTotal)
+ ".\n Which leaves you with a total savings of \$" +
+ totalSave + ". Way to go for paying yourself!" );
} // End outputResults
} //End Class

Why isn't my else statement working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
pretty straightforward as to what I am curious about. I dont understand why when I type in an amount that is 0 or less than for the withdrawal I do not get the error message that I have in the class.
Code is here:
Mainclass:
import java.text.NumberFormat;
public class Account {
private final double RATE = 0.03; // interest rate of 3%
private String customer;
private int id;
private long account_id;
public double balance;
// Creates new account with specified information
public Account(int id, String name, long acctNum, double acctBalance) {
customer = name;
account_id = acctNum;
balance = acctBalance;
}
public double deposit(double amount) {
// If deposit is larger than 0 increase by deposit amount
if (amount > 0)
balance = balance + amount;
else {
System.out.println("Invalid deposit amount");
}
return balance;
}
public double withdraw(double amount) {
// If withdrawal is larger than 0 reduce balance by amount
if (amount <= balance || amount > 0)
balance = balance - amount;
else {
System.out.println("Invalid withdrawal amount or insufficient funds");
}
return balance;
}
// Method to add interest to account
public double addInterest() {
balance += (balance * RATE);
return balance;
}
public double getBalance() {
return balance;
}
#Override
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (customer + "\t" + account_id + "\t" + fmt.format(balance));
}
}
Driver class:
import java.util.Scanner;
public class AccountManager {
public static void main(String[] args) {
// Create array space for 30 accounts
String customer;
long account_id;
double balance;
int id = 0;
int count = 0;
int amount = 0;
Account[] account_array = new Account[30];
Scanner scan = new Scanner(System.in);
String input = "";
System.out.println("Would you like to create an account?");
System.out.println("Type 'yes' to add an account");
input = scan.nextLine();
while (input.equals("yes")) {
System.out.println("To create an account please enter the following: ");
System.out.println("ID number (0,1,2,3..etc)");
id = scan.nextInt();
scan.nextLine();
System.out.println("Name: ");
customer = scan.nextLine();
System.out.println("Account Number: ");
account_id = scan.nextLong();
System.out.println("Current Balance: ");
balance = scan.nextDouble();
scan.nextLine();
account_array[count] = new Account(id, customer, account_id, balance);
count++;
System.out.println("Would you like to create another account?");
System.out.println("Type 'yes' to add another account");
input = "";
input = scan.nextLine();
}
System.out.println("Enter the ID of the account you would like to make changes to: ");
id = scan.nextInt();
scan.nextLine();
System.out.println("Would you like to deposit or withdraw cash? ");
input = scan.nextLine();
if (input.equals("withdraw")) {
System.out.println("How much would you like to withdraw?");
amount = scan.nextInt();
account_array[id].withdraw(amount);
System.out.println("Balance for account: " + id + " = " + account_array[id].balance);
}
if (input.equals("deposit")) {
System.out.println("How much would you like to deposit?");
amount = scan.nextInt();
account_array[id].deposit(amount);
System.out.println("Balance for account: " + id + " = " + account_array[id].balance);
}
}
}
Because amount <= balance holds true. Use && instead of ||.
if(amount <= balance && amount > 0){

Adding "$" to right aligned numbers and Scanner class not running, java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am having two problems with my code.
First: I can't seem to add "$" in the right location (I cant get it to look like $10.00 only 10.00$)
Second: Adding a Scanner class ends up with the program "running" but nothing happening. (if I set gross with a number it runs fine but not with using a scanner class)
import java.util.Scanner;
public class Payment
{
public static void main(String[] args)
{
Scanner Keyboard = new Scanner(System.in);
//double gross = Keyboard.nextDouble(); will not work
//double gross = 8000; will work
double fed = (0.15 * gross);
double state = (0.035 * gross);
double soc = (0.0575 * gross);
double med = (0.0275 * gross);
double pen = (0.05 * gross);
double hea = 75;
double net = (gross - (fed + state + soc + med + pen + hea));
System.out.println("Paycheck calculation by employee\n");
System.out.printf("Gross Amount:%28.2f%n", gross);
System.out.printf("Federal Tax:%29.2f%n", fed);
System.out.printf("State Tax:%31.2f%n", state);
System.out.printf("Social Security Tax:%21.2f%n", soc);
System.out.printf("Medicare/Medicaid Tax:%19.2f%n", med);
System.out.printf("Pension Plan %28.2f%n", pen);
System.out.printf("Health Insurance %24.2f%n%n", hea);
System.out.printf("Net Pay:%33.2f", net);
}
}
You probably want to print out an input prompt. Regarding currency formatting, you could use the DecimalFormat class.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Payment
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter gross amount: ");
double gross = keyboard.nextDouble();
//double gross = 800; //will work
double fed = (0.15 * gross);
double state = (0.035 * gross);
double soc = (0.0575 * gross);
double med = (0.0275 * gross);
double pen = (0.05 * gross);
double hea = 75;
double net = (gross - (fed + state + soc + med + pen + hea));
DecimalFormat currency = new DecimalFormat("$0.00");
System.out.println("Paycheck calculation by employee\n");
System.out.printf("Gross Amount: %27s%n", currency.format(gross));
System.out.printf("Federal Tax:%29s%n", currency.format(fed));
System.out.printf("State Tax:%31s%n", currency.format(state));
System.out.printf("Social Security Tax:%21s%n", currency.format(soc));
System.out.printf("Medicare/Medicaid Tax:%19s%n", currency.format(med));
System.out.printf("Pension Plan %28s%n", currency.format(pen));
System.out.printf("Health Insurance %24s%n%n", currency.format(hea));
System.out.printf("Net Pay:%33s", currency.format(net));
keyboard.close();
}
}
Answering your first question, you could it like this:
System.out.printf("Gross Amount %28c%.2f%n", '$', gross);
Your second question, i think your problem is the Locale. Depending on your Locale, the input format of a Double, in this case, may be different. You could do:
keyboard.useLocale(Locale.US);
This way, the input of a Double will be the integer part separated by a . from the decimal part. 8000 and 5.5 are valid examples of a Double input.

return issue for method

I am having an issue with a method returning to the main method. It is saying that amount in "return amount" cannot be resolved to a variable. Where am I off on this??
This is the message I get:
Multiple markers at this line
- Void methods cannot return a
value
- amount cannot be resolved to a
variable
Here is the code:
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
amount = temp;
System.out.print((count + 1) + " " + temp);
}
{
return amount;
}
}
You curly braces are not correct. The compiler - and me - was confused about that.
This should work (at least syntactically):
import java.util.Scanner;
public class Investment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years));
}
public static double futureInvestmentValue(
double amount, double interest, int years) {
double monthlyInterest = interest / 1200;
double temp = 0;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest, years * 12));
amount = temp;
System.out.print((count + 1) + " " + temp);
return amount;
}
}
Remove amount from its own scope As a start. Also from the method futureInvestmentValue, you take in amount as an argument but the value is never modified so you're returning the same value being passed which is most likely not the desired outcome.
remove return amount from its own scope
the method futureInvestmentValue... You can't modify any of the parameters inside the method so you have to declare another variable besides amount inside the method (maybe it's the temp variable you keep using) and return that instead
when you return something, the return statement is always inside the method. Never outside it while inside its own braces (never seen this before...)
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years) {
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
System.out.print((count + 1) + " " + temp);
}
return amount;
}
}

Categories

Resources