Results 1 to 6 of 6
Thread: Simple Java program not working:
- 03-17-2014, 01:25 PM #1
Senior Member
- Join Date
- Jan 2014
- Posts
- 137
- Rep Power
- 0
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(); } }
- 03-17-2014, 01:35 PM #2
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 8
Re: Simple Java program not working:
don't compare String types with ==, rather with the equals method
- 03-17-2014, 01:40 PM #3
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Simple Java program not working:
What superhaNds says. Additionally:
Java Code:else if(acccType == "advanced" || acccType == "Advanced")
Java Code:else if(acccType.equalsIgnoreCase("advanced")){
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 03-17-2014, 01:41 PM #4
Senior Member
- Join Date
- Jan 2014
- Posts
- 137
- Rep Power
- 0
Re: Simple Java program not working:
Thank you.... I am coming from C++ and trying to learn Java.
- 03-17-2014, 01:52 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 03-17-2014, 02:14 PM #6
Senior Member
- Join Date
- Jan 2014
- Posts
- 137
- Rep Power
- 0
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
-
Prime Number Program in Java, simple program giving headache to me
By immohan in forum New To JavaReplies: 6Last Post: 12-15-2013, 07:40 AM -
Simple Graphics program not working.
By forms in forum New To JavaReplies: 7Last Post: 03-11-2012, 04:44 PM -
Simple draw line program not working
By forms in forum New To JavaReplies: 7Last Post: 01-24-2012, 05:51 PM -
Simple java program, need help
By cliffh in forum New To JavaReplies: 1Last Post: 10-21-2010, 04:32 AM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 10:40 AM
Bookmarks