
Hi I'm kinda new to java and I'm trying to make the following methods work sort of like a password. The problem is that watever input i give in, the boolean is always false and I don't know why. Maybe something to do with the for loop? Please help
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class random {
static boolean access=false;
public static String enterName() throws IOException{
System.out.println("Enter your first name");
BufferedReader brentryName=new BufferedReader(new InputStreamReader(System.in));
String entryName=brentryName.readLine();
return entryName;
}
public static void checkName() throws IOException{
String[] names={"Michael","Gavin","Morgan","Edward","Philippa","Brian","Colleen"};
String check=enterName();
System.out.println(check);
for(int i=0;i<names.length;i++){
if(check.equalsIgnoreCase(names[i])){
access=true;
} else {
access=false;
}
}
}
public static void main(String[] args) throws IOException {
checkName();
if(access==true){
System.out.println("Access Granted. Welcom");
} else {
System.out.println("Access denied");
}
}
}

The problem with ur code is that assuming u enter Edward, it checks out with the fourth item, but the loop is still running, hence when it is compared with the item after that "phillipa", and those after, access will be set to false since there is no match, u will only be granted access if u enter the last name in the array. What u have to do is to place a break statement at this point if (ch......) { access = true; break; } so that when a match is found, the loop is exited.
import java.io.*; class random {
static boolean access=false;
public static String enterName() throws IOException{ System.out.println("Enter your first name"); BufferedReader brentryName=new BufferedReader(new InputStreamReader(System.in)); String entryName=brentryName.readLine(); return entryName; } public static void checkName() throws IOException{ String[] names={"Michael","Gavin","Morgan","Edward","Philippa","Brian","Colleen"}; String check=enterName(); System.out.println(check);
for(int i=0; i<names.length;i++){
if(check.equals(names[i])) {
access=true;
}
else {
access=false;
}
}
}
public static void main(String[] args) throws IOException { checkName(); if(access==true){ System.out.println("Access Granted. Welcom"); } else { System.out.println("Access denied"); } } } elimence@gmail.com

Sorry for the messy answer. To solve ur problem, simply put a break statement within the if conditional, i.e, after access=true here is the code
import java.io.*;
class random {
static boolean access=false;
public static String enterName() throws IOException{
System.out.println("Enter your first name");
BufferedReader brentryName=new BufferedReader(new InputStreamReader(System.in));
String entryName=brentryName.readLine();
return entryName;
}
public static void checkName() throws IOException{
String[] names={"Michael","Gavin","Morgan","Edward","Philippa","Brian","Colleen"};
String check=enterName();
System.out.println(check);
for(int i=0; i<names.length;i++){
if(check.equals(names[i])) {
access=true;
break;
}
else {
access=false;
}
}
}
public static void main(String[] args) throws IOException {
checkName();
if(access==true){
System.out.println("Access Granted. Welcom");
}
else {
System.out.println("Access denied");
}
}
}
elimence@gmail.com

OOOOOOOH lol thanks a lot man, really helpful :)
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.