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

26th Aug 2021, 6:09 PM
William Davis
William Davis - avatar
21 odpowiedzi
+ 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.
26th Aug 2021, 7:31 PM
Ipang
+ 1
@iPang Added the code, and gave detail at the bottom
26th Aug 2021, 7:47 PM
William Davis
William Davis - avatar
+ 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.
26th Aug 2021, 8:08 PM
William Davis
William Davis - avatar
+ 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();
26th Aug 2021, 8:35 PM
zemiak
+ 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
27th Aug 2021, 9:45 AM
Ipang
+ 1
@zemiak That worked!!!!!!!! Why and how???????
27th Aug 2021, 12:41 PM
William Davis
William Davis - avatar
+ 1
I try improve my first answer now, look there
27th Aug 2021, 12:44 PM
zemiak
+ 1
nextLine() reads every characters and \n as the end, but returns String without \n
27th Aug 2021, 1:07 PM
zemiak
+ 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.
27th Aug 2021, 9:02 PM
William Davis
William Davis - avatar
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.
26th Aug 2021, 8:01 PM
Ipang
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...
26th Aug 2021, 8:55 PM
William Davis
William Davis - avatar
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(); }
27th Aug 2021, 9:09 AM
zemiak
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.
27th Aug 2021, 10:05 AM
William Davis
William Davis - avatar
0
Ipang How do I know what to put in other classes and what to have in main?
27th Aug 2021, 10:06 AM
William Davis
William Davis - avatar
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
27th Aug 2021, 11:06 AM
zemiak
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
27th Aug 2021, 12:10 PM
William Davis
William Davis - avatar
0
add this second line acNumCheck = input.nextInt(); input.nextLine();
27th Aug 2021, 12:17 PM
zemiak
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"?
27th Aug 2021, 12:51 PM
William Davis
William Davis - avatar
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
27th Aug 2021, 1:35 PM
William Davis
William Davis - avatar
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"; } }
27th Aug 2021, 8:48 PM
zemiak