#Help
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @idle vigil! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Please format your code to make it more readable.
For java, it should look like this:
````java
public void foo() {
}
````
'''import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to your Bank Account");
System.out.println("Type in one of the options");
System.out.println("1: Deposit ");
System.out.println("2: Withdraw ");
System.out.println("3: Transaction History ");
System.out.println("4: Total Balance");
int option = scanner.nextInt();
int withdraw = 1;
int deposit = 2;
int transactionHistory = 3;
int totalBalance = 4;
int total = 0;
if(option == deposit);{
System.out.println("You have chosen to Deposit money. How much would you like to deposit?");
int x = scanner.nextInt();
System.out.println("Total balance:" + total + x);
}
if(option == withdraw); {
System.out.println("You have chosen to Withdraw money. How much would you like to withdraw? ");
int y = scanner.nextInt();
System.out.println("total balance:" + total + y);
}
}
}''' ```
This message has been formatted automatically. You can disable this using /preferences.
Hard to catch. This is because of these semicolons ; on the lines of your ifs. You must not do that.
Sadly Java doesn't treat it as a compiling error and sees it as you wanting to execute no instructions when your if verifies instead.
Of course, that's an error nobody does when they're used to Java
The problem here is operator precendence.
Operator - has not greater precedence than + instead they're equals. As a result, the leftmost operator is applied first on the two leftmost operands.
That's "Total balance " + total
and that produces a String.
You can't substract something from a String.
What you need is to increase the precedence of the - operation, before you concatenate the result with +
That's what parentheses are for
(Though, looking at the bigger picture, a better approach would be that a withdrawal decreases the current balance, and after you've done that, you present what's the balance now that this operation has completed)
I'm guessing you don't know that you can set a different value to an existing variable, rather than having to keep the first value you put in it