Results 1 to 6 of 6
- 11-24-2008, 11:59 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
[SOLVED] Need help with simple Java program
I am a newbie and I am trying to write a simple program that prompts a user to input their account number, account type (checking or savings)and current balance. Then depending on the type of account and balance, assess a penalty or apply the appropriate penalty amount or interest rate and calculate a new balance. I'm having a very hard time with this. I tried to write the code and got as far as the code for the checking and get errors. I would really appreciate it if anyone could help me sort this out. Here is my code so far:
I get a whole lot of errors saying mostly that I am using the else without if. I really don't know what to do.Java Code:import java.util.*; public class Statement { static Scanner console = new Scanner(System.in); public static void main(String[] args) { // Variable declarations int accountNum; char accountType; double currentBal; double minimumBal; double newBal; newBal = 0; System.out.println("This program computes a" + "new bank balance."); System.out.println("Enter the account number."); accountNum = console.nextInt(); System.out.println("enter the account type."); accountType = console.next().charAt(0); switch (accountType) { case 'c': System.out.println("Enter the current balance."); currentBal = console.nextDouble(); System.out.println("Enter the minimum balance required."); minimumBal = console.nextDouble(); if (minimumBal >currentBal) newBal = currentBal - 25.00; System.out.println("Your checking balance " + "for account number: " + accountNum + "\n"); System.out.println("has fallen below the minimum " + "amount required." + "\n"); System.out.println("You have been assessed " + "a penalty of 25 dollars. Your new balance is " + newBal); else if (minimumBal <= currentBal) newBal = currentBal * 1.04; System.out.println("Your new checking account balance" + "for account number " + accountNum + "\n"); System.out.println("is now " + newBal + "\n"); System.out.println("This figure reflects interest earned this period."); else if (currentBal > 5000) newBal = currentBal * 1.05; System.out.println("Your new checking account balance" + "for account number " + accountNum + "\n"); System.out.println("is now " + newBal + "\n"); System.out.println("This figure reflects interest earned this period."); } } }
- 11-25-2008, 12:36 AM #2
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
use open and close braces for if and else if statements
here goes your code check it out
import java.util.*;
public class Statement
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
// Variable declarations
int accountNum;
char accountType;
double currentBal;
double minimumBal;
double newBal;
newBal = 0;
System.out.println("This program computes a"
+ "new bank balance.");
System.out.println("Enter the account number.");
accountNum = console.nextInt();
System.out.println("enter the account type.");
accountType = console.next().charAt(0);
switch (accountType)
{
case 'c':
System.out.println("Enter the current balance.");
currentBal = console.nextDouble();
System.out.println("Enter the minimum balance required.");
minimumBal = console.nextDouble();
if (minimumBal >currentBal){
newBal = currentBal - 25.00;
System.out.println("Your checking balance "
+ "for account number: " + accountNum + "\n");
System.out.println("has fallen below the minimum "
+ "amount required." + "\n");
System.out.println("You have been assessed "
+ "a penalty of 25 dollars. Your new balance is " + newBal);
}
else if (minimumBal <= currentBal){
newBal = currentBal * 1.04;
System.out.println("Your new checking account balance"
+ "for account number " + accountNum + "\n");
System.out.println("is now " + newBal + "\n");
System.out.println("This figure reflects interest earned this period.");
}
else if (currentBal > 5000){
newBal = currentBal * 1.05;
System.out.println("Your new checking account balance"
+ "for account number " + accountNum + "\n");
System.out.println("is now " + newBal + "\n");
System.out.println("This figure reflects interest earned this period.");
}
}
}
}
- 11-25-2008, 01:15 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
Thanks for the help
I put braces around the if and else if statements but I think I also needed to use the or operators as I have them to determine which interest calculation to use. Is that correct?
I am definitely not sure how to do this but the way the program is supposed to work is that if the balance is more than the minimum but up to 5000 it earns interest at 4% and if it is above 5000 it earns interest at 5%. The same will happen for the savings account except the penalty and interest values will be different. Here is my revised code:
Java Code:import java.util.*; public class Statement { static Scanner console = new Scanner(System.in); public static void main(String[] args) { // Variable declarations int accountNum; char accountType; double currentBal; double interestRate1; double interestRate2; double penaltyC; double penaltyS; double minimumBal; double newBal; newBal = 0; penaltyC = 25.00; penaltyS = 10.00; interestRate1 = 1.04; interestRate2 = 1.05; System.out.println("This program computes a" + "new bank balance."); System.out.println("Enter the account number."); accountNum = console.nextInt(); System.out.println("enter the account type."); accountType = console.next().charAt(0); switch (accountType) { case 'c': System.out.println("Enter the current balance."); currentBal = console.nextDouble(); System.out.println("Enter the minimum balance required."); minimumBal = console.nextDouble(); if (currentBal < minimumBal){ newBal = currentBal - penaltyC; System.out.println("Your checking balance " + "for account number: " + accountNum + "\n"); System.out.println("has fallen below the minimum " + "amount required." + "\n"); System.out.println("You have been assessed " + "a penalty of 25 dollars. Your new balance is " + newBal); } else if (currentBal > minimumBal | currentBal > 5000){ newBal = currentBal * interestRate1; System.out.println("Your new checking account balance " + "for account number " + accountNum + "\n"); System.out.println("is now " + newBal + "\n"); System.out.println("This figure reflects interest earned this period."); } else if (currentBal > 5000){ newBal = currentBal * interestRate2; System.out.println("Your new checking account balance " + "for account number " + accountNum + "\n"); System.out.println("is now " + newBal + "\n"); System.out.println("This figure reflects interest earned this period."); } } } }
- 11-25-2008, 05:15 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you get any error message in your code? If so please copy and paste here to see. Compiling and running your code can be mess up.
- 11-25-2008, 04:57 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 32
- Rep Power
- 0
Use of braces worked
Thanks for all your help. I placed the braces for the else if statements and now the program compiles without errors and runs like it should. Now I will try to write the same program using dialog boxes for input and output. Thanks again.:)
- 11-26-2008, 03:17 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's good practice use braces even in a single statement of if-else condition.
Something like this working fine, but not good to use.
I prefer this way,Java Code:if(2 > 4) System.out.println("Yes");
Java Code:if(2 > 4) { System.out.println("Yes"); }
Similar Threads
-
Stuck - simple program
By dirtycash in forum New To JavaReplies: 4Last Post: 11-24-2008, 07:44 PM -
Simple Java probablitity program?
By nothing4me in forum New To JavaReplies: 6Last Post: 11-12-2008, 06:16 AM -
the explanation of output of simple java program
By amol84 in forum New To JavaReplies: 1Last Post: 11-06-2008, 05:06 PM -
help with simple program in java
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:03 PM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks