Results 1 to 6 of 6
  1. #1
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Question [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:
    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.");						
    }
    }
    }
    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.

  2. #2
    sai
    sai is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    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.");
    }
    }
    }
    }

  3. #3
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default 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.");						
    }
    }
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  5. #5
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default 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.:)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

    Java Code:
    if(2 > 4)
        System.out.println("Yes");
    I prefer this way,

    Java Code:
    if(2 > 4) {
        System.out.println("Yes");
    }

Similar Threads

  1. Stuck - simple program
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-24-2008, 07:44 PM
  2. Simple Java probablitity program?
    By nothing4me in forum New To Java
    Replies: 6
    Last Post: 11-12-2008, 06:16 AM
  3. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 05:06 PM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •