boolean help

boolean help

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");
    }
}

}

View Answers

October 26, 2010 at 11:27 PM

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"); } } } [email protected]


October 27, 2010 at 6:26 AM

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");
    }
}
}

[email protected]


October 27, 2010 at 6:26 PM

OOOOOOOH lol thanks a lot man, really helpful :)









Related Tutorials/Questions & Answers:
boolean help
boolean help  Hi I'm kinda new to java and I'm trying to make... 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
Need coding help - two dimensional matrix and it returns back a boolean.
Need coding help - two dimensional matrix and it returns back a boolean.  I need to write a code method that accepts a two dimensional integer arrays... it is a triangular matrix if false it is not. any help with this coding
Advertisements
PHP Boolean Data Types
PHP Boolean Data Type: Boolean is one of the simplest data type supported by PHP. A boolean is used to express a truth value either true or false. For conversion to boolean data type, we need to use (bool) or (boolean) casts. Though
Boolean DataType
Boolean DataType  What is the Size of the Boolean datatype in Java
Boolean functions in JavaScript
Boolean functions in JavaScript  Explain about the Boolean functions present in Javascript
boolean operators in JavaScript
boolean operators in JavaScript  What boolean operators does JavaScript support
Boolean functions present in Javascript
Boolean functions present in Javascript   Explain about the Boolean functions present in Javascript
boolean method - Java Beginners
boolean method  I have a Noteook class. And I have one requirement... don't know how to throw in the boolean method in my main class. Notebook... memory; private double cost; private int quantity; private boolean
ModuleNotFoundError: No module named 'boolean'
ModuleNotFoundError: No module named 'boolean'  Hi, My Python... 'boolean' How to remove the ModuleNotFoundError: No module named 'boolean... to install padas library. You can install boolean python with following command
objective c boolean example
objective c boolean example  Hi, Can anyone tell me how to use boolean variable in Objective c? Thanks
help
help   how i can send a pitcture on url in java
help
help  pls help me to get the code of a java program a program to perform different shapes in a menu using javaapplet
help
help  i need help with this code. write a java code for a method named addSevenToKthElement that takes an integer array, and an integer k as its arguments and returns the kth element plus 7. any help would be greatly
boolean comparisons - tutorial
.style1 { text-align: center; } Boolean Data Type Boolean data types... if, for, while and do all requires boolean value to execute. Generally operators are used to evaluate a condition, which return boolean value. According
MySQL Boolean
MySQL Boolean This example illustrates how to find the boolean value. In this example we create a select query to show if we pass 0 then it will return false but when we set 1 then it will return true.  ADS_TO_REPLACE_1
The boolean Keyword
The boolean Keyword       The boolean Keyword in java avails one of the two values that are true and false. Java have the boolean type so literal values true and false. 
HELP
HELP  I need this code to open in a new web browser. but no matter what i do it wont please help?? Directive 055 Storage Requirements for the Upstream Petroleum Industry
Help...
Help...  Write a class that displays your first name vertically down the screen â?? where each letter uses up to 5 rows by 5 columns of a character input by you with a blank line between each letter
Help
Help   i have a html file.. i have take input and store in database .. Tell me the steps to perform.. i have xampp it includes mysql,tomacat,apache. and how display the data from datbase .. please explain
Help!
Help!  thanks for ur code, I'd like to ask a question: after press "c" or "C", timer starts and after every delay ms, it'll print "This line is printed only once." or just print once. I'm writting a timer but it seems to perform
help
verification mail to the user) please tell me how could i design that. Please help me
Help
Help  Hi; Can anyone help me to solve this errors. I use J2ME(wirlesee Toolkit 2.5.2. for CLDC Project "SlideImageMIDlet" loaded Project settings saved Building "SlideImageMIDlet" C:\Documents and Settings\???? ?????\j2mewtk
help
based on the percentage of cheating. please help me
help!!!!!!!!!!!!!!!!!!
help!!!!!!!!!!!!!!!!!!  import java.awt.; import java.sql.*; import javax.swing.*; import java.awt.event.* public class NewJFrame extends javax.swing.JFrame { JTextField1
MySQL Boolean Value
MySQL Boolean Value       MySQl Boolean Value is used to display the Boolean type status of the field... 'MySQL Boolean Value'. To understand the example execute a query 'SHOW TABLE
ModuleNotFoundError: No module named 'Boolean-Solver'
ModuleNotFoundError: No module named 'Boolean-Solver'  Hi, My... named 'Boolean-Solver' How to remove the ModuleNotFoundError: No module named 'Boolean-Solver' error? Thanks   Hi, In your python
Converting Boolean to String
Converting Boolean to String In this section we will discuss about how to convert boolean to String type. This example show you how to convert boolean type... BooleanToString { public static void main(String args[]) { Boolean b
changing final Boolean value in SwingUtilities.invokeLater()
changing final Boolean value in SwingUtilities.invokeLater()  Hi, I have a problem with setting final Boolean value in second time... static boolean doCommit() { final boolean success = true
MySQL Boolean Data Type
MySQL Boolean Data Type This example illustrates how create a boolean data type. In this example we create a table 't' where define four field that is 'a' which is BOOL type, 'b' which is FLOAT type, 'c' which is LONG VARCHAR type
Convert Boolean to String
Convert Boolean to String       In this section, we are going to convert a Boolean type data... in converting the Boolean type data into a string. The toString() method reads the Boolean
Conversion from short to boolean
Conversion from short to boolean: In this tutorial we will learn how to convert a short type data to boolean type. Description: This program will take a short type value from console and provides a conversion to boolean type
MySQL Boolean Value
MySQL Boolean Value This example illustrates how to find the boolean type status of the table. In this example execute a query 'SHOW TABLE STATUS LIKE "%bool%"', it display the boolean type status of the field which is display
What is the difference between the Boolean & operator and the && operator?
What is the difference between the Boolean & operator and the && operator?   Hello, What is the difference between the Boolean & operator and the && operator? thanks
Conversion from String to boolean
Conversion from String to boolean: In this tutorial we will learn how to convert a string type data to boolean type data. Description: This program will take a String value from mystring variable. The line boolean mybool
MySQL Boolean Operator
MySQL Boolean Operator This example illustrates how to use the boolean operator 'NOT' in the MySQL query. In this example we create a PROCEDURE 'boolproc' to find whether a=b or not. The procedure call by [call boolproc
ModuleNotFoundError: No module named 'SQLAlchemy-boolean-search'
ModuleNotFoundError: No module named 'SQLAlchemy-boolean-search'  Hi...: No module named 'SQLAlchemy-boolean-search' How to remove the ModuleNotFoundError: No module named 'SQLAlchemy-boolean-search' error? Thanks  
ModuleNotFoundError: No module named 'SQLAlchemy-boolean-search'
ModuleNotFoundError: No module named 'SQLAlchemy-boolean-search'  Hi...: No module named 'SQLAlchemy-boolean-search' How to remove the ModuleNotFoundError: No module named 'SQLAlchemy-boolean-search' error? Thanks  
ModuleNotFoundError: No module named 'boolean-cayley-graphs'
ModuleNotFoundError: No module named 'boolean-cayley-graphs'  Hi...: No module named 'boolean-cayley-graphs' How to remove the ModuleNotFoundError: No module named 'boolean-cayley-graphs' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-boolean-sum'
ModuleNotFoundError: No module named 'django-boolean-sum'  Hi, My... named 'django-boolean-sum' How to remove the ModuleNotFoundError: No module named 'django-boolean-sum' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-boolean-switch'
ModuleNotFoundError: No module named 'django-boolean-switch'  Hi...: No module named 'django-boolean-switch' How to remove the ModuleNotFoundError: No module named 'django-boolean-switch' error? Thanks   Hi
Conversion from int to boolean
Conversion from int to boolean: In this tutorial we will learn how to convert an int type value to boolean type data. Description: This program will take an int value from console and provide a conversion to boolean type data(true
Conversion from byte to boolean
Conversion from byte to boolean: In this tutorial we will learn how to convert a byte type value to boolean type value. Description: This program will take a byte value from console and provides a conversion to boolean type data
Conversion from float to boolean
Conversion from float to boolean: In this tutorial we will learn how to convert a float type value to boolean type value. Description: This program will take a float value from console and provides a conversion to boolean type data
Conversion from double to boolean
Conversion from double to boolean: In this tutorial we will learn how to convert a double type value to boolean type value. Description: This program will take a double value from console and provide the conversion to boolean type
Conversion from long to boolean
Conversion from long to boolean: In this tutorial we will learn how to convert a long type value to boolean type value. Description: This program will take a long type value from console and provide the conversion to boolean type
Regular Expression Help Help HELP!!!!
Regular Expression Help Help HELP!!!!  HI all I am very new to java... could help me to give a solution of how to retrieve the name and email add... the quotes ""07001", "MR Harish Ram","M.RHarish @ hotmail.com"" Help is greatly
Java - Boolean Expression and Operation in Java
Java - Boolean Expression and Operation in Java       Boolean expression performs the relational or logical operations and returns the boolean value (True/False
help in java
help in java  Define a class named Money whose objects represent amounts of U.S. money. The class will have two instance variables of type int... cents) â?? boolean equals(Money amount) â?? static Money add(Money amount1, Money
How to do Null validation for a boolean variable - Java Beginners
How to do Null validation for a boolean variable  How to do Null validation for a boolean variable in JAVA?  Hi friend, For solving the problem visit to : http://www.roseindia.net/java/master-java/variables
PHP Boolean Variables
PHP Variables Boolean In PHP we have different kinds of data types... which is called Boolean data type. It is a primitive data type having either true...; Boolean in different way. For example : <?phpADS_TO_REPLACE_1 $age

Ads