Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Michael
boolean help
3 Answer(s)      4 years and a month ago
Posted in : Java Beginners


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

}


Advertisement
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"); } } } elimence@gmail.com



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

elimence@gmail.com



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
Boolean DataType
Boolean DataType  What is the Size of the Boolean datatype in Java
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 operators in JavaScript
boolean operators in JavaScript  What boolean operators does JavaScript support
Boolean functions in JavaScript
Boolean functions in JavaScript  Explain about the Boolean functions present in Javascript
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
objective c boolean example
objective c boolean example  Hi, Can anyone tell me how to use boolean variable in Objective c? Thanks
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.   Query
boolean comparisons - tutorial
.style1 { text-align: center; } Boolean Data Type Boolean data types... Statement and Boolean The  control statements like if, for, while and do all requires boolean value to execute. Generally operators are used
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. 
MySQL Boolean Value
MySQL Boolean Value       MySQl Boolean Value is used to display the Boolean type status of the field. Understand with Example The Tutorial illustrate an example from 'MySQL Boolean
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 into a string.  Code Description: This program helps you in converting 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 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
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
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
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 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 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 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 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
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
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
Advertisements
 

 

 

DMCA.com