Can someone help me with this little bug I found? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone help me with this little bug I found?

So, I'm working on a simple project on Java. I'm creating basically a Banking program. It's really simple. I'm just practicing and trying to work my way to figure things out. For today, I think what I want to try to figure out is how to have the program keep asking the user for a valid name until the user enters a valid name. So, I tried doing this and I'll just put in an empty name, it'll put me back but when I do it again, it'll let me in. How do I prevent that from happening? I know there's tons of improvement I need to implement on this program, but I'm just wanting to deal with this one thing I found. I'll improve it some more tomorrow. Here is what I wrote: //Bank Account project // The purpose of this is to store account information like balance and to make simple transactions such as deposit and withdraws. /* Improvements that I'll add in: * Put balance number with randomized account number in a file and read file every time program runs * Add in a password and validate the password before allowing the user to perform transactions * Add support for more than one account */ import java.util.Scanner; public class Bank { public static void main(String[] args){ Scanner input = new Scanner(System.in); //scanner int input_num = -1; double deposit_num = 0; // number of how much money to deposit double withdraw_num = 0;// number of how much money to withdraw String account_name = " "; while (account_name == " "){ //This block of code will have the program ask the user to enter a name until it enters a valid name System.out.print("Enter your account name: "); account_name = input.nextLine(); Account a = new Account(account_name); System.out.println(" "); //This code needs to improvement. The program takes in invalid names as the account name. Needs tweaking. } Account a = new Account(account_name); System.out.println(" "); System.out.println("Welcome " + account_name + ". You have a total balance of

quot; + a.getBalance());

18th Jul 2017, 1:55 AM
Dishoungh White
Dishoungh White - avatar
31 Answers
+ 2
I implemented a separate method and put it into the do-while loop. Here: do { System.out.print("Enter your account name: "); account_name = input.nextLine(); } while (!a.validateName(account_name)); //Method to check if the name is valid. //If name is empty, returns false. //If name is null, returns false //If name includes invalid characters, returns false public boolean validateName(String input){ if (input.isEmpty() == true){ return false; }else if (input == null){ return false; }else if (input.isEmpty() != true && input != null){ input.toUpperCase(); for (int i = 0; i < input.length(); i++){ if (input.charAt(i) < 'A' || input.charAt(i) >'Z'){ return false; }else{ return true; } } } return true; }
18th Jul 2017, 3:38 AM
Dishoungh White
Dishoungh White - avatar
+ 5
.isEmpty checks if a string is empty string nm = ""; if(nm.isEmpty()) { System.out.println("The name is empty!"); }
18th Jul 2017, 2:18 AM
MrCoder
MrCoder - avatar
+ 5
@ChaoticDawg Thanks for clarifying the do-while too, I didn't know about much of it :D
18th Jul 2017, 2:29 AM
MrCoder
MrCoder - avatar
+ 5
do { //code } while(condition); //do-while structure
18th Jul 2017, 2:30 AM
MrCoder
MrCoder - avatar
+ 5
@ChaoticDawg Wow thats coincidental lol
18th Jul 2017, 2:59 AM
MrCoder
MrCoder - avatar
+ 4
a do-while loop ensures you that the block of code will compile atleast once even if the condition is false, basically a do-while loop iterates the block of code before it checks the condition a while loop will check the condition if it is true then it iterates the block of code Null means nothing, basically nothing
18th Jul 2017, 2:17 AM
MrCoder
MrCoder - avatar
+ 3
Try adding these while(true) { name = sc.nextLine(); if(name != valid) { // Change the 'valid' to a valid name continue; // Re-loop again } else { break; //break and iterate the next code } // Iterates the below code //some code
18th Jul 2017, 2:08 AM
MrCoder
MrCoder - avatar
+ 3
Oh you get the idea there
18th Jul 2017, 2:08 AM
MrCoder
MrCoder - avatar
+ 3
Remove the ! from your condition.
18th Jul 2017, 2:55 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
I copied and pasted my code on the code playground. Here is the link https://code.sololearn.com/cuXdSLOOe4No/#java
18th Jul 2017, 1:58 AM
Dishoungh White
Dishoungh White - avatar
+ 2
@MrCoder thanks for answering that. However it's not that it will compile at least once with a do-while, it's that it will run the code block at least once before checking the condition. Just to clarify. But yes, null means that the object has not been instantiated. So you would also need to do some checking in your code block before you try to access its value so as not to create an error. IE surround the code in an if block making sure that it is not equal to null if(account_name != null) { System.out.println(account_name); }
18th Jul 2017, 2:25 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Lol @MrCoder, posted basically the same answer at the same time
18th Jul 2017, 2:33 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Change your while to a do-while and make the comparison: account_name == null || account_name.isEmpty() (not null or equal to an empty string)
18th Jul 2017, 3:00 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
do { System.out.print("Enter your account name: "); account_name = input.nextLine(); } while (account_name == null || account_name.isEmpty()); The check below is only needed if you are attempting to access the value of account_name prior to it possibly being set. So if you were accessing it in the body of the do-while loop where it was being set or before entering the loop (don't know why you would do that, lol) then it could possibly be null if something went wrong or if it actually was accessed before the line in which it was set. After the loop exits it shouldn't be needed as that condition is checked to exit the loop. if (account_name == null){ System.out.println("Account name is null"); }
18th Jul 2017, 3:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
here is a String trim() example: public class Program { public static void main(String[] args) { String str = " "; // All white space string str = str.trim(); // trim the string and save it back into the variable if(str.isEmpty()) { System.out.println("Empty"); // should output Empty } else if (str == null) { System.out.println("Null"); } else { System.out.println("IDK"); } } }
18th Jul 2017, 3:38 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Looks good, but I would add the trim() method prior to the isEmpty() check and then change the for loop to a for each loop. This will run a bit faster. You could also flip all the returns so that you don't need to put a negation in front of your method call in the do-while conditional. You don't need to check isEmpty against being true as it is already a boolean type. And the last else if could just be an else. You also need to save the toUpperCase() back into the input variable. You should only exit the for loop if the char doesn't match what you want I.E. remove the else. This should work: public boolean validateName(String input){ input = input.trim(); if (input.isEmpty()){ return true; }else if (input == null){ return true; }else { input = input.toUpperCase(); for (char ch: input.toCharArray()) { if (ch < 'A' || ch >'Z'){ return true; } } } return false; }
18th Jul 2017, 3:55 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ChaoticDawg What does null really mean? I never learned this before. And I didn't know that I could do .isEmpty().
18th Jul 2017, 2:10 AM
Dishoungh White
Dishoungh White - avatar
+ 1
What does do-while do instead of a while statement?
18th Jul 2017, 2:15 AM
Dishoungh White
Dishoungh White - avatar
+ 1
The solutions aren't working. Let me try something.
18th Jul 2017, 2:16 AM
Dishoungh White
Dishoungh White - avatar
+ 1
@MrCoder Whenever I turn it into a do while, it has an error with one of brackets. It doesn't make any sense.
18th Jul 2017, 2:29 AM
Dishoungh White
Dishoungh White - avatar