Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
First java project, if /else statement help
Options
-
13-05-2013 8:18pmHi all hoping someone out there might be able to help me on this.
___________________________________________________________________
break;
if (theaccount !=null)
System.out.println("\nWould you like to delete your account:" );
String = yes or no .(inputread.nextLine()); theaccount.answer
(y);
break;
____________________________________________________________
What I want the system to output is if "yes" is selected " your account has been deleted" , if "no" is selected then output will read " Please exit the system".
thanks in advance0
Comments
-
Here's the basis.
if (condition is true) print Your account... else print Please exit...
0 -
If you want more help than the above post, I think you need to post more code so people can see what is going on there, what you have posted is a little confusing. What are the break statements breaking out of, for example, and what does the theaccount class look like ?
Variable names should be camel case by convention in Java by the way, like theAccount and inputRead intead of theaccount and inputRead. Won't affect functionality, but helps readability.0 -
Thanks for the replies. The break is out of menu choice. the following is the menu code just before the code i posted earlier
switch (menu_choice) {
case 0:
System.exit(0);
break;
case 1:
theaccount = createaccount();
break;
case 2:
if (theaccount == null)
System.out
.print("\nSorry cannot withdraw no account created \n");
else // an account has been created so we can withdraw
{
System.out
.print("\nPlease enter amount you wish to withdraw: ");
amount = Double.parseDouble(inputread.nextLine());
theaccount.withdraw(amount);
}
break;
case 3:
if (theaccount == null)
System.out
.print("\nSorry cannot deposit no account created \n ");
else {
System.out
.print("\nPlease enter amount you wish to deposit: ");
amount = Double.parseDouble(inputread.nextLine());
theaccount.deposit(amount);
}
break;0 -
boolean yes
if(yes=true)
{
println.system.out ("you have deleted your account")
}
else { println.system.out "Please exit the system"
}
is this right?0 -
Senor Frog wrote: »boolean yes
if(yes=true)
{
println.system.out ("you have deleted your account")
}
else { println.system.out "Please exit the system"
}
is this right?
boolean mySuperCoolBool = true;
if (mySuperCoolBool) {
System.out.println("you have deleted your account");
} else {
System.out.println("Please exit the system");
}0 -
Advertisement
-
Senor Frog wrote: »boolean yes
if(yes=true)
{
println.system.out ("you have deleted your account")
}
else { println.system.out "Please exit the system"
}
is this right?
Just to expand on irishoob's correction — You can't say:yes = true
As that would be setting yes to be true. In Java, you'd use a double-equals to check (primative) equality, e.g.:yes == true
Also, with if statements, you can just pass them a boolean value directly, soif(yes==true)
andif(yes)
do the exact same thing.0 -
thanks for the reply bud.
been at it for hours, making silly mistakes, going to call it a night and look at it all again with fresh eyes tomorrow.0 -
String answer = inputread.nextLine();
if (answer.equalsIgnoreCase("yes"))
System.out.println("you have deleted your account");
} else {
System.out.println("Please exit the system");
}
what about this?0 -
Senor Frog wrote: »String answer = inputread.nextLine();
if (answer.equalsIgnoreCase("yes"))
System.out.println("you have deleted your account");
} else {
System.out.println("Please exit the system");
}
what about this?0 -
Senor Frog wrote: »String answer = inputread.nextLine();
if (answer.equalsIgnoreCase("yes"))
System.out.println("you have deleted your account");
} else {
System.out.println("Please exit the system");
}
what about this?
looks like you're missing a { after the if line?0 -
Advertisement
-
Hi
Been reading this thread. It's VERY confusing. You're posting snippets of code, and we have no idea of the context, which is why I assume the guys are giving you construct-type feedback.
If you'd like specific feedback on why stuff wont work, what does this do, is there a better way to do that etc, post your entire code (assume it's a single class?) and we can help you better.
Cheers0 -
Hi thanks for the reply , sorry its taken me a while to get back to you. Have been trying to get it all tied up, its due tomorrow . So this is where I am at, im trying to get a basic bank account system to work. I have five classes. 1) Bank System . 2) account (this is my base account from which the three types of account inherit from). 3) current account. 4) Savings.5) Mortgage.
I am happy with classes 1-4. The Bank system is were my confusion now is. I am trying to implement arrays so that when a new account is added it goes into an account list.
When I try run it im getting the following errors:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "}" to complete SwitchBlock
Syntax error, insert "}" to complete Block
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Syntax error, insert "}" to complete MethodBody
at BankingSystem.main(BankingSystem.java:2390 -
no matter how much I play around with { and ; im not resolving the isues even though this is what the prompter is telling me to do..
here is the bank system class in full:
import java.util.Scanner;
import java.util.ArrayList;
public class BankingSystem {
/**
* @param args
*/
static Scanner inputread = new Scanner(System.in);
static account theaccount = null;
static String name, address, phone, accountnum, sortcode;
static double open_bal,blance, int_rate, mortgageLimit, overdraftlimit; // put balance in here
static boolean paperworkinplace, overdraft; //because its a static variable
static ArrayList<account> list = new ArrayList<account>();// array list to hold account accounts created within the system//
//doesnt seem to be working linked to errors in line217 /231
public static void displayHeader() {
System.out.println("\t
");
System.out.println("\tWelcome to Bank System ");
System.out.println("\t
");
}
public static int displayMainMenu() {
System.out.println("1. Create an account");
System.out.println("2. Make a withdrawal");
System.out.println("3. Make a deposit");
//System.out.println("4. Close an account");
System.out.println("0. Exit");
System.out.print("\nPlease enter a choice and hit Return: ");
return Integer.parseInt(inputread.nextLine());
}
public static void getCustomerDetails() {
System.out.print("\nPlease enter the Customer name: ");
name = inputread.nextLine();
System.out.print("\nPlease enter the Customer address: ");
address = inputread.nextLine();
System.out.print("\nPlease enter the telephone number: ");
phone = inputread.nextLine();
System.out.print("\nPlease enter the Account number: ");
accountnum= inputread.nextLine();
System.out.print("\nPlease enter the sort code: ");
sortcode = inputread.nextLine();
System.out.print("\nPlease enter the opening balance for the account : ");
open_bal = Double.parseDouble(inputread.nextLine());
}
public static void getCurrentAccountDetails() {
System.out.print("\nPlease enter the interest rate: ");
int_rate = Double.parseDouble(inputread.nextLine());
System.out.print("\nPlease enter the overdraft limit: ");
overdraftlimit = Double.parseDouble(inputread.nextLine());
System.out.print("\nWould you like an overdraft facility? (Yes or NO): ");
String answer = inputread.nextLine();
if (answer.equalsIgnoreCase("yes")) {
overdraft = true;
}
else if (answer.equalsIgnoreCase("no")) {
overdraft = false;
} else
System.out.println("invalid output Selection Enter (true or false)");
}
public static void getDepositAccountDetails() {
System.out.print("\nPlease enter the interest rate: ");
int_rate = Double.parseDouble(inputread.nextLine());
}
public static void getMortgageAccountDetails() {
System.out.print("\nPlease enter the interest rate charged: ");
int_rate = Double.parseDouble(inputread.nextLine());
System.out.print("\nPlease enter the mortgage limit : ");
mortgageLimit = Double.parseDouble(inputread.nextLine());
System.out.print("\nIs the paper work in place? (Yes or NO): ");
String answer = inputread.nextLine();
{if (answer.equalsIgnoreCase("yes")) {
paperworkinplace = true;
}
else if (answer.equalsIgnoreCase("no")) {
paperworkinplace = false;
System.out.println("invalid output Selection Enter (true or false)");
}
}
}
public static account createaccount() {
boolean pass;
account newAcc = null;
do {
pass = true;
System.out.print("\nPlease enter the account type: ");
String choice = inputread.nextLine();
if (choice.equalsIgnoreCase("Current")) {
getCustomerDetails();
getCurrentAccountDetails();
newAcc = new CurrentAccount(name, address, balance, accountnum, // need to set value for balance above( or change to open_bal, ask where to put dont want to effect array at top
sortcode, phone, int_rate, overdraft,overdraftlimit );
System.out.println("New Curent account created");
} else if (choice.equalsIgnoreCase("Deposit")) {
getDepositAccountDetails();
getCustomerDetails();
newAcc = new Savings(name,address,blance accountnum, sortcode, phone, int_rate, open_bal , );// by putting these in the same order as class will this fix error?
System.out.println("New Savings account created");
} else if (choice.equalsIgnoreCase("Mortgage")) {
getMortgageaccDetails();
getCustomerDetails();
newAcc = new Morgage(accountnum, sortcode, open_bal, phone, address, // error becuase of mispelling of morgage in class
name, int_rate, paperworkinplace, mortgageLimit);
System.out.println("New Mortgage account created");
}
else {
System.out
.println("\nUnknown Account type. Valid values are [Deposit, Mortgage or Current ...]\n");
pass = false;
}
} while (!pass);
return newAcc;
}
private static void getMortgageaccDetails() {
// TODO Auto-generated method stub
}
public static void withdrawAccount(double a) {
//method below in switch
System.out.print("\nPlease enter a choice and hit Return: ");
}
public static void deleteAccount(int accountNum) {
System.out.print("\nPlease enter a choice and hit Return: ");
}
public static account findaccount(int accountnumtofind)
{
for (account element : list) {
if (element.accountnum == accountnumtofind)
return (element);
else return (null);}
/// find acount method
public static void main(String[] args) {
// TODO Auto-generated method stub
int menu_choice;
double amount;
account theaccount = null;
do {
int accountNum;
displayHeader();
menu_choice = displayMainMenu();
switch (menu_choice) {
case 0:
System.exit(0);
break;
case 1:
theaccount = createaccount();
list.add(theaccount);
break;
case 2:
if (list.isEmpty())
System.out
.print("\nSorry cannot withdraw no account created \n");
else {// an account has been created so we can withdraw
System.out.print("Please enter account number");
int acnumber = Integer.parseInt(inputread.nextLine());
account found_ac = findaccount(acnumber);// if i change from acnumber to accountnum will this fix error?
if (found_ac != null){
System.out.print("\nPlease enter the amount you wish to withdraw: ");
amount = Double.parseDouble(inputread.nextLine());
theaccount.withdraw(amount);
}
else
System.out.println("Account not found");
break; }
case 3:
if (theaccount == null)
System.out
.print("\nSorry cannot deposit no account created \n ");
else {
System.out.print("Please enter account number");
int acnumber = Integer.parseInt(inputread.nextLine());
account found_ac = findaccount(acnumber);
if (found_ac != null) {
System.out
.print("\nPlease enter amount you wish to deposit: ");
amount = Double.parseDouble(inputread.nextLine());
theaccount.deposit(amount);
break;
while (menu_choice != 0);
}}
/* case 4:
/* if (theaccount == null)
System.out.print("\nCannot delete. No account specified\n");
else
{// an account has been created so we can delete
System.out.print("Please enter account number");
int acnumber = Integer.parseInt(inputread.nextLine());
Account found_ac = findAccount(acnumber);
if (found_ac != null) {
if (found_ac.delete()) {
list.remove(found_ac);
System.out.println("The account has been deleted");
}
}
} */0 -
i have commented out case 4 for in the do statement, because im badly caught for time and don't think its a necessity....0
-
/** * @param args */ static Scanner inputread = new Scanner(System.in); static account theaccount = null; static String name, address, phone, accountnum, sortcode; static double open_bal,blance, int_rate, mortgageLimit, overdraftlimit; // put balance in here static boolean paperworkinplace, overdraft; //because its a static variable static ArrayList<account> list = new ArrayList<account>();// array list to hold account accounts created within the system// //doesnt seem to be working linked to errors in line217 /231 public static void displayHeader() { System.out.println("\t-------------------------------------------"); System.out.println("\tWelcome to Bank System "); System.out.println("\t-------------------------------------------"); } public static int displayMainMenu() { System.out.println("1. Create an account"); System.out.println("2. Make a withdrawal"); System.out.println("3. Make a deposit"); //System.out.println("4. Close an account"); System.out.println("0. Exit"); System.out.print("\nPlease enter a choice and hit Return: "); return Integer.parseInt(inputread.nextLine()); } public static void getCustomerDetails() { System.out.print("\nPlease enter the Customer name: "); name = inputread.nextLine(); System.out.print("\nPlease enter the Customer address: "); address = inputread.nextLine(); System.out.print("\nPlease enter the telephone number: "); phone = inputread.nextLine(); System.out.print("\nPlease enter the Account number: "); accountnum= inputread.nextLine(); System.out.print("\nPlease enter the sort code: "); sortcode = inputread.nextLine(); System.out.print("\nPlease enter the opening balance for the account : "); open_bal = Double.parseDouble(inputread.nextLine()); } public static void getCurrentAccountDetails() { System.out.print("\nPlease enter the interest rate: "); int_rate = Double.parseDouble(inputread.nextLine()); System.out.print("\nPlease enter the overdraft limit: "); overdraftlimit = Double.parseDouble(inputread.nextLine()); System.out.print("\nWould you like an overdraft facility? (Yes or NO): "); String answer = inputread.nextLine(); if (answer.equalsIgnoreCase("yes")) { overdraft = true; } else if (answer.equalsIgnoreCase("no")) { overdraft = false; } else System.out.println("invalid output Selection Enter (true or false)"); } public static void getDepositAccountDetails() { System.out.print("\nPlease enter the interest rate: "); int_rate = Double.parseDouble(inputread.nextLine()); } public static void getMortgageAccountDetails() { System.out.print("\nPlease enter the interest rate charged: "); int_rate = Double.parseDouble(inputread.nextLine()); System.out.print("\nPlease enter the mortgage limit : "); mortgageLimit = Double.parseDouble(inputread.nextLine()); System.out.print("\nIs the paper work in place? (Yes or NO): "); String answer = inputread.nextLine(); {if (answer.equalsIgnoreCase("yes")) { paperworkinplace = true; } else if (answer.equalsIgnoreCase("no")) { paperworkinplace = false; System.out.println("invalid output Selection Enter (true or false)"); } } } public static account createaccount() { boolean pass; account newAcc = null; do { pass = true; System.out.print("\nPlease enter the account type: "); String choice = inputread.nextLine(); if (choice.equalsIgnoreCase("Current")) { getCustomerDetails(); getCurrentAccountDetails(); newAcc = new CurrentAccount(name, address, balance, accountnum, // need to set value for balance above( or change to open_bal, ask where to put dont want to effect array at top sortcode, phone, int_rate, overdraft,overdraftlimit ); System.out.println("New Curent account created"); } else if (choice.equalsIgnoreCase("Deposit")) { getDepositAccountDetails(); getCustomerDetails(); newAcc = new Savings(name,address,blance accountnum, sortcode, phone, int_rate, open_bal , );// by putting these in the same order as class will this fix error? System.out.println("New Savings account created"); } else if (choice.equalsIgnoreCase("Mortgage")) { getMortgageaccDetails(); getCustomerDetails(); newAcc = new Morgage(accountnum, sortcode, open_bal, phone, address, // error becuase of mispelling of morgage in class name, int_rate, paperworkinplace, mortgageLimit); System.out.println("New Mortgage account created"); } else { System.out .println("\nUnknown Account type. Valid values are [Deposit, Mortgage or Current ...]\n"); pass = false; } } while (!pass); return newAcc; } private static void getMortgageaccDetails() { // TODO Auto-generated method stub } public static void withdrawAccount(double a) { //method below in switch System.out.print("\nPlease enter a choice and hit Return: "); } public static void deleteAccount(int accountNum) { System.out.print("\nPlease enter a choice and hit Return: "); } public static account findaccount(int accountnumtofind) { for (account element : list) { if (element.accountnum == accountnumtofind) return (element); else return (null);} /// find acount method public static void main(String[] args) { // TODO Auto-generated method stub int menu_choice; double amount; account theaccount = null; do { int accountNum; displayHeader(); menu_choice = displayMainMenu(); switch (menu_choice) { case 0: System.exit(0); break; case 1: theaccount = createaccount(); list.add(theaccount); break; case 2: if (list.isEmpty()) System.out .print("\nSorry cannot withdraw no account created \n"); else {// an account has been created so we can withdraw System.out.print("Please enter account number"); int acnumber = Integer.parseInt(inputread.nextLine()); account found_ac = findaccount(acnumber);// if i change from acnumber to accountnum will this fix error? if (found_ac != null){ System.out.print("\nPlease enter the amount you wish to withdraw: "); amount = Double.parseDouble(inputread.nextLine()); theaccount.withdraw(amount); } else System.out.println("Account not found"); break; } case 3: if (theaccount == null) System.out .print("\nSorry cannot deposit no account created \n "); else { System.out.print("Please enter account number"); int acnumber = Integer.parseInt(inputread.nextLine()); account found_ac = findaccount(acnumber); if (found_ac != null) { System.out .print("\nPlease enter amount you wish to deposit: "); amount = Double.parseDouble(inputread.nextLine()); theaccount.deposit(amount); break; while (menu_choice != 0); }} /* case 4: /* if (theaccount == null) System.out.print("\nCannot delete. No account specified\n"); else {// an account has been created so we can delete System.out.print("Please enter account number"); int acnumber = Integer.parseInt(inputread.nextLine()); Account found_ac = findAccount(acnumber); if (found_ac != null) { if (found_ac.delete()) { list.remove(found_ac); System.out.println("The account has been deleted"); } } } */
0 -
just put your code into code blocks to make it easier toreadException in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "}" to complete SwitchBlock
Syntax error, insert "}" to complete Block
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
Syntax error, insert "}" to complete MethodBody
at BankingSystem.main(BankingSystem.java:239
looks like you are missing some curly brackets and another syntax error.
Also get into naming variables and methods this way:int myVariable; someMethod();
again just makes it easier to read0 -
Why are you using all static methods and variables?0
-
because these variables are for the bank system solely , i have variables that are non static in my base account class which the three other class inherit form....
I know it says im missing curly brackets in the error report but when i put them in where i have been prompted to another error appears on the bracket i just placed0 -
You curly brackets don't match up...
we cannot tell because you do not post code within the code tags but make sure your indentation is correct to help you match up the brackets
Maybe take a break then look again
Look closely to your findAccount method and in the main at the do while.
The problem is simply one of bracket placement.
EDIT: Whoops - it seems I had not reached the end of the thread before I responded.0 -
if (answer.equalsIgnoreCase("yes")) { overdraft = true; } else if (answer.equalsIgnoreCase("no")) { overdraft = false; } else System.out.println("invalid output Selection Enter (true or false)"); }
Your missing the } after the elsepublic static void getMortgageAccountDetails() { System.out.print("\nPlease enter the interest rate charged: "); int_rate = Double.parseDouble(inputread.nextLine()); System.out.print("\nPlease enter the mortgage limit : "); mortgageLimit = Double.parseDouble(inputread.nextLine()); System.out.print("\nIs the paper work in place? (Yes or NO): "); String answer = inputread.nextLine(); [COLOR="Red"]{[/COLOR] if (answer.equalsIgnoreCase("yes")) { paperworkinplace = true; } else if (answer.equalsIgnoreCase("no")) { paperworkinplace = false; System.out.println("invalid output Selection Enter (true or false)"); } [COLOR="red"]}[/COLOR] }
There may be more but your code is too hard to read0 -
Advertisement
-
I attempted to refactor this and reformat it into something a lot more readable but got bogged down, the code's all over the place.
For future, make sure you adopt a strict and consistent formatting style, it makes your code neat and readable.
This also has the effect of making problems that you're having (errors with brackets and things being out of place with regards to scope) very unlikely to pop up as they stick out like sore thumbs and are easy to notice. Secondly, it allows others to help you more, they're actually able to read and understand what you're trying to do.
fundamentally, there's nothing really wrong with your methodogy, you seem to be bogged down with syntax and scope
(there are examples of where you have if statements, but your else and else ifs that should follow it are actually at the class level just below where the method ends!).
Alot of the work could be done in a constructor too which would clean things up.
Like another poster mentioned, try and use camelCase for your variable names, as a rule of thumb for Java at least, methods and classes start an uppercase letter, such as "BankAccount" whereas variables will start lowercase, like "int myVal". These are good coding habits to adopt.
I would suggest going through your code line by line and reformatting everything!
Here's a sample of what I changed in your class before I stopped.
import java.util.Scanner;
import java.util.ArrayList;
public class BankingSystem
{
/**
* @param args
*/
static Scanner inputRead = new Scanner(System.in);
static account account = null;
static String name, address, phone, accountnum, sortcode;
static double open_bal,blance, int_rate, mortgageLimit, overdraftlimit; // put balance in here
static boolean paperworkinplace, overdraft; //because its a static variable
static ArrayList<account> list = new ArrayList<account>();// array list to hold account accounts created within the system//
//doesnt seem to be working linked to errors in line217 /231
public static void displayHeader()
{
System.out.println("\t
"+\n+
"\tWelcome to Bank System "+\n+
"\t
"+\n);
}
public static int displayMainMenu()
{
System.out.println(
"1. Create an account"+\n+
"2. Make a withdrawal"+\n+
"3. Make a deposit"+\n+
"4. Close an account"+\n+
"0. Exit"+\n+
"Please enter a choice and hit Return: ");
return Integer.parseInt(inputread.nextLine());
}
public static void getCustomerDetails()
{
System.out.print("\nPlease enter the Customer name: ");
name = inputread.nextLine();
System.out.print("\nPlease enter the Customer address: ");
address = inputread.nextLine();
System.out.print("\nPlease enter the telephone number: ");
phone = inputread.nextLine();
System.out.print("\nPlease enter the Account number: ");
accountnum= inputread.nextLine();
System.out.print("\nPlease enter the sort code: ");
sortcode = inputread.nextLine();
System.out.print("\nPlease enter the opening balance for the account : ");
open_bal = Double.parseDouble(inputread.nextLine());
}
public static void getCurrentAccountDetails()
{
System.out.print("\nPlease enter the interest rate: ");
int_rate = Double.parseDouble(inputread.nextLine());
System.out.print("\nPlease enter the overdraft limit: ");
overdraftlimit = Double.parseDouble(inputread.nextLine());
System.out.print("\nWould you like an overdraft facility? (Yes or No): ");
String answer = inputread.nextLine();
if (answer.toLowerCase.equalsIgnoreCase("yes"))
{
overdraft = true;
}
else if (answer.toLowerCase.equalsIgnoreCase("no")
{
overdraft = false;
}
else
{
System.out.println("invalid output Selection Enter (yes or no)");
}
}
public static void getDepositAccountDetails()
{
System.out.print("\nPlease enter the interest rate: ");
int_rate = Double.parseDouble(inputread.nextLine());
}
ETC.
EDIT
The formatting doesnt translate exactly here, so here's a screenshot
0 -
Thanks for the replies guys appreciate it. Points taken regarding the formatting. Won't make the same mistakes next time....0
Advertisement