Results 1 to 6 of 6
  1. #1
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Simple Java program not working:

    This is a very simple program but when I run it my "miniumBalance" method returns my else statement "ERROR-Account Type was not found."



    Java Code:
    import java.util.Scanner;
    
    
    public class BankAccount 
    {
    	private Double balance;
    	
    	public Double minimunBalance(String acccType)
    	{
    		if(acccType == "student" || acccType == "Student")
    		{
    			set_balance(100.00);
    		}
    		else if(acccType == "basic" || acccType == "Basic")
    		{
    			set_balance(500.00);
    		}
    		else if(acccType == "advanced" || acccType == "Advanced")
    		{
    			set_balance(1000.00);
    		}
    		else
    		{
    			System.out.println("ERROR-Account Type was not found.");
    			set_balance(000.00);
    		}
    		
    		return get_balance();
    	}
    	
    	//setter function
    	public void set_balance(Double amount)
    	{
    		balance=amount;
    	}
    	//getter function
    	public Double get_balance()
    	{		
    		return balance;
    	}
    	
    	public void FindInterest(String a)
    	{
    		double min_balance = minimunBalance(a);
    		double interest = 0;
    		double total = 0;
    		
    		if(min_balance == 100)
    		{
    			interest = 0.03; // % 3.0 quarterly
    			total = min_balance * interest * (.25); // 1st quarter
    			
    			//calculate quarters 2-4
    			for(int i=0;i<3;i++){total = total * interest * (.25);};
    			
    			System.out.println("The is a minimun requirement of $100 balance for this account.\n");
    			System.out.println("If you leave 100 dollars in your account\n"
    							+ "you will have of "+total+" in your account after 12 montns.\n"
    							+ " because we give you a amazing interst rate of % 3.0.");
    		}
    		else if(min_balance == 500)
    		{
    			interest = 0.032; // % 3.2 quarterly
    			
    total = min_balance * interest * (.25); // 1st quarter
    			
    			//calculate quarters 2-4
    			for(int i=0;i<3;i++){total = total * interest * (.25);};
    			
    			System.out.println("The is a minimun requirement of $500 balance for this account.\n");
    			System.out.println("If you leave 500 dollars in your account\n"
    							+ "you will have of "+total+" in your account after 12 montns.\n"
    							+ " because we give you a amazing interst rate of % 3.2.");
    		}
    		else if(min_balance == 1000)
    		{
    			interest = 0.041; // % 4.1 quarterly
    			
    total = min_balance * interest * (.25); // 1st quarter
    			
    			//calculate quarters 2-4
    			for(int i=0;i<3;i++){total = total * interest * (.25);};
    			
    			System.out.println("The is a minimun requirement of $1000 balance for this account.\n");
    			System.out.println("If you leave 1000 dollars in your account\n"
    							+ "you will have of "+total+" in your account after 12 montns.\n"
    							+ " because we give you a amazing interst rate of % 4.1.");
    		}
    		else
    		{
    			System.out.println("Sorry but an Error occured");
    		}
    		
    		
    	}
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner(System.in);
    
    		System.out.println("Enter the account type you are interested in:\n"
    							+ "Student\n"
    							+ "Basic\n"
    							+ "Advanced");
    		
    		String status = input.nextLine();
    		
    		BankAccount account = new BankAccount();
    		
    		account.FindInterest(status);
    		
    		input.close();
    	}
    
    }

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Simple Java program not working:

    don't compare String types with ==, rather with the equals method

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: Simple Java program not working:

    What superhaNds says. Additionally:

    Java Code:
    else if(acccType == "advanced" || acccType == "Advanced")
    its less error-prone to do:

    Java Code:
    else if(acccType.equalsIgnoreCase("advanced")){
    or to just toLowerCase() the input value right from the start.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: Simple Java program not working:

    Thank you.... I am coming from C++ and trying to learn Java.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: Simple Java program not working:

    String is an object, if you use == to compare two String references you're only checking if it is the exact same object instance and not if the String objects contain the exact same sequence of characters. Much in the same way that in C++ you would be checking if two pointers point to exactly the same memory address and thus the same array of characters / the same STL string object.

    But in C++ the truth can be muddied because you are able to overload the standard behavior of the == operator. Perhaps that is what you are used to.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: Simple Java program not working:

    I got it working, thanks for the help everyone.

    Any good recommendations of places to find fun programs besides like tic tac toe, etc.

    I learn the best when I run into a problem. I have only been learning Java for 3 weeks but I am getting the hang of it pretty fast.

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  2. Simple Graphics program not working.
    By forms in forum New To Java
    Replies: 7
    Last Post: 03-11-2012, 03:44 PM
  3. Simple draw line program not working
    By forms in forum New To Java
    Replies: 7
    Last Post: 01-24-2012, 04:51 PM
  4. Simple java program, need help
    By cliffh in forum New To Java
    Replies: 1
    Last Post: 10-21-2010, 03:32 AM
  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
  •