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




Have Programming Question? Ask it here!
 
 
 


Michael
boolean help
3 Answer(s)      3 years and 10 months 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
Java NotesBoolean The primitive type boolean has only two possible values: true and false. Boolean literals - true and false The two values... statements The if, for, while, and do statements all require boolean values
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 Expressions
Boolean Expressions Name ______________________ Assume the following: int k = 10; int j = 6; boolean b = true; Show what is printed by each of the following statements. __________ System.out.println( 1 > 2
Boolean Expressions
Boolean Expressions Name ______________________ Assume the following: int k = 10; int j = 6; boolean b = true; Show the value (true or false) of each of the following expressions. __________ 1 > 2 __________ b __________ k
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
Java: Boolean Expression Answers
Java NotesBoolean Expression Answers Name ______________________ Assume the following: int i, j, k; boolean b; k = 10; j = 6; b = true; Give the values of the following expressions, or illegal. 1trueb 2false!b 3true!!!!!!b 4trueb
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 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
help
help   how i can send a pitcture on url in java
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  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
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!  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
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
to the user) please tell me how could i design that. Please help me asap Thanks a lot
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
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
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 : <?php $age = true; //assign
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
How to send boolean array to servlet from java application?
How to send boolean array to servlet from java application?  Hi, I want to send randomly generated array of boolean values. I used Random function and boolean array length to generate values. My question is I want to send
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
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
please help
, new String [] { "name", "num" } ) { boolean[] canEdit = new boolean [] { false, false }; public boolean isCellEditable(int rowIndex, int
I'm getting an illgal start of expression error in my code for the public static boolean portion at the bottom... any ideas?
static boolean portion at the bottom... any ideas?  heres my code import...; boolean guess = true; System.out.print("The Number guessing game... static boolean playAgain(){ String answer
Help me
Help me  Hi, LWUIT is working in eclipse j2me for Symbian OS
Advertisements
 

 

 

DMCA.com