0
Program Ending after If Statement
https://code.sololearn.com/cj66rJmM3szN It ends as soon as it outputs the option to choose which account: Checking or Savings
21 Respostas
+ 3
Sorry, but I'm afraid you can't expect people to debug a partial code. I would rather suggest you to edit your thread and attach a link to a code bit instead, for better view of the actual situation
https://www.sololearn.com/post/75089/?ref=app
Also, it wasn't really clear what//Ends here// means. Does it mean code crashes and stop there or something else? because you wrote "code is ending after the `if` statement" but that comment was there before an `if` block.
+ 1
@iPang Added the code, and gave detail at the bottom
+ 1
@Ipang I barely understand the language and am getting used to the site, did not understand what you meant earlier, it has been updated to your liking.
+ 1
You read the pin as an int number,
but the user sends along with the number
the invisible character '\n' meaning 'end of line' (because he pressed enter)
3597\n
Another attempt to read the answer
(to the question "Checking or Saving"),
reads only this old character \n instead of the new answer.
The solution is to read this \n by the program
acNumCheck = input.nextInt ();
input.nextLine();
+ 1
I think you need to break down the code into specific methods rather than bloating all the code in main() method. It's hard to analyse a code with everything pushed into main() method.
Also you have this, but I can't find CheckingAccount class definition.
CheckingAccount info = new CheckingAccount(); // Calling a class
+ 1
@zemiak That worked!!!!!!!! Why and how???????
+ 1
I try improve my first answer now, look there
+ 1
nextLine() reads every characters and \n as the end, but returns String without \n
+ 1
@zemiak That looks so much cleaner!!! So that's what you use classes and methods for! I'm at a confusing part of Java for me and this helps a lot! I'll have to play around with this and learn. Is there a way we can pm each other and would you be willing to help sometimes? Promise not to bother you too much.
0
William,
The code is too big to fit in, and it got truncated. You need to save the code in SoloLearn, and share its link as I had suggested earlier.
0
@zemiak No idea what you're saying? It continues after the code, meaning that it works. The problem is it ends after asking what you would like to access. So it either ends on the System.out.println(**) or the String account...
0
for repeating menu you need loop
while(true) {
System.out.println("What would you like to access:");
System.out.println("Checking Savings Quit");
String account = input.nextLine();
if ("Checking".equals(account)) {
//...
} else
if("Savings".equals(account)) {
//...
} else
break;
}
input.close();
}
0
Ipang I have 2 classes. 1 checking and 1 savings. They only hold the variables ints for the 2 balances. Also, the program ends when asked which account to access. But it works fine when I don't have the input pin section.
0
Ipang How do I know what to put in other classes and what to have in main?
0
>"Also, the program ends when asked which account
to access. But it works fine when I don't have the input pin section."
look at my first answer, it solve stop after account access by pin
0
@zemiak I've looked at your first answer and tried to figure it out. Your English is a bit hard to read and I don't understand what you are telling me
0
add this second line
acNumCheck = input.nextInt();
input.nextLine();
0
@zemiak So the input.nextLine(); reads the /n but doesn't mean anything? How come it doesn't do this when inputting something like: String action = input.nextLine();
When I input the string, I still hit enter, if I'm checking for "Savings" wouldn't that be wrong because I still hit enter and therefore input "Savings/n"?
0
@zemiak I have completed my code for now, will probably work on it more later but I'm ready to continue my java lesson. You're more than welcome to check the code out and let me know what you think.
https://code.sololearn.com/c9p0lmtuOjxO/#java
0
there is very similar redundant code for actions "Inquire, Withdrawal, or Deposit" for both accounts types. It can be unified in one method actions() with output eg.
System.out.println("Your " +typeName +"balance is: " +balance);
then it is better to write classes CheckingAccount and SavingAccount which extends BankAccount like
class CheckingAccount extends BankAccount {
CheckingAccount( int balance) {
super(balance);
typeName = "checking";
}
}