java
Creating Login with Java, won't go to else statement?
I'm making a login program, but it is getting some errors. If I put in a name that is not in the list, I get an error. It doesn't seem to reach the else statement also, and I'm not sure why. import java.lang.*; import java.util.*; import java.io.*; import java.math.*; public class HelloWorld { public static void main(String[] args) { Scanner input = new Scanner(System.in); String userName; int n = 10; int i = 0; String[] array = new String[n]; array[0] = "John"; array[1] = "Johny"; array[2] = "ben"; System.out.println("Enter your user name(Note:**Case Sensative**)"); userName = input.nextLine(); while (i <= array.length) { if (array[i].equals(userName)) { System.out.println("Your UserName is valid"); break; } else if (!array[i].equals(userName)){ i++; } else { System.out.println("Your UserName is not valid"); break; } } } }
First, I'd consider using a for-loop as it'll manage the i value for you, but also consider the logic you have if element.equals(userName) {...} else if !element.equals(userName) {...} else {...} How is it possible to ever get into the else block, either element will be or won't be equal to the userName, there is no other state. I'd consider having a flag which indicates if a user was found or not and checking that at the other end of the loop As a general concept... //... userName = input.nextLine(); String validUser = null; for (int index = 0; index < array.length; index++) { if (array[i].equals(userName)) { validUser = userName; break; } } if (validUser != null) { System.out.println("UserName is valid"); } else { System.out.println("Your UserName is not valid"); } Because you probably don't care about the index so much, you can also do something like... for (String element : array) { if (array[i].equals(userName)) { validUser = userName; break; } } which is shorthand version. Have a look at The for statement for more details
First of all your if and else if are the only condition the java will run in your context and there are no other condition running afterwards. It is like you are doing TRUE and FALSE and expecting another condition. Make use of foreach in java much simpler way. boolean found = false; for(String name: array){ if (name.equals(userName)) { found = true; break; } } if(!found){ System.out.println("Your UserName is not valid"); }else{ System.out.println("Your UserName is valid"); } When you get mastered use the methods of Array , Collections and specially lambdas stream api for little bit lift up your java knowledge.
Your Statement will never go to the else part, because the the input(userName) will either EQUAL array[i], or not. Think about it... if (array[i].equals(userName)) { //It will do this if userName is array[i] System.out.println("Your UserName is valid"); break; } else if (!array[i].equals(userName)) { //It will do this if userName is NOT array[i] i++; } else { //this is impossible, unreachable code System.out.println("Your UserName is not valid"); break; } The reason your else is not reachable is because it is an impossible scenario. I edited your program, and it works now. Here's the whole thing: import java.util.Scanner; public class Experiments { public static void main(String[] args) { Scanner input = new Scanner(System.in); String userName; boolean found = false; String[] array = new String[3]; array[0] = "John"; array[1] = "Johny"; array[2] = "ben"; System.out.println("Enter your user name:"); userName = input.nextLine(); for(int i=0; i<array.length; i++) { if (userName.equalsIgnoreCase(array[i])) { System.out.println("Your UserName is valid"); found = true; break; } if (!found && i == array.length-1) { System.out.println("Your UserName is not valid"); } } input.close(); } } If the input (userName) is in the array at any spot, it will say "Your UserName is valid" If it isn't in the array, it will say "Your UserName is not valid". Here is an example of a valid userName: Enter your user name: John Your UserName is valid And if the UserName is invalid: Enter your user name: Jimmy Your UserName is not valid Please comment if you have any questions about this, and I will explain.
Related Links
Data Structure of a Serialized ArrayList<object> Binary File
Disadvantages of using Spring's #Configuration class
SWIG C function pointer and JAVA
IOException can not be resolved to a type error
Debugging Game of Life, Java
How to call PHP page through ajax call on java page
ClassNotFoundException for JSTL jar when I try to migrate JBoss 7.1 application to JSF 2.2
How to write binary file in remote machine using j-interop?
Spring MVC view & update
routing captured packets using Jpcap
How to add Restrictions to OneToMany mapping in hibernate
Initialize an Item Array
Making Struts2 redirectAction use relative paths instead of absolute paths
Read/write /proc/<pid>/mem from java code on Android platform
Swing Card Layout change panel displayed on action by user
How do I enumerate an object based on nodes in Java?