Michael
boolean help
3 Answer(s)      2 years and 7 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");
    }
}

}

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 Pages:
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
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
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
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
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
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 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 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
objective c boolean example
objective c boolean example  Hi, Can anyone tell me how to use boolean variable in Objective c? Thanks
please help
, new String [] { "name", "num" } ) { boolean[] canEdit = new boolean [] { false, false }; public boolean isCellEditable(int rowIndex, int
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
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
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
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
PLZ Need some help JAVA...HELP !!
PLZ Need some help JAVA...HELP !!  Create a class names Purchase Each purchase contains an invoice number, amount of sale and amount of sales tax...){ Scanner input=new Scanner(System.in); boolean check1
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
Load function ***PLEASE HELP
Load function ***PLEASE HELP   Hi I am writing a program that tracks... LOAD implementations. Thank you so much for your help. I really need help to get...;(); // the boolean is created boolean start = true; // The while loop will only work
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
java coding help plz ?
java coding help plz ?  Given two integers N and M (N ââ?°Â¤ M), output all the prime numbers between N and M inclusive, one per line. N and M...{ static boolean isPrime(long number) { boolean isPrime = false
Java help needed
Java help needed  Create a web application that will allow a user... options: One for accessing help using the program, one for adding new words, one... another word. 7. If the help button is pressed, show a help screen that gives
Help Required - Swing AWT
Help Required  How to bring password field in JDialogueBox??? ... static String HELP = "help"; private JFrame controllingFrame; //needed..."); JButton helpButton = new JButton("Help
java help - Java Beginners
java help  Write the code for a method named calculateTax that accepts a double parameter named subtotal and a boolean parameter named isFresno and calculates and returns the sales tax. If isFresno is false, the sales tax should
java help - Java Beginners
that accepts a double parameter named subtotal and a boolean parameter named isFresno... subtotal = Double.parseDouble(bufReader.readLine()); boolean isFresno...) { double subtotal= 10,salestax; boolean isFrensco=true; salestax=calculate
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
Plz Help Me
Plz Help Me  Write a program for traffic light tool to manage time... traffic light on the frame. We have used java swing. It may help you. import...{ Color on; int radius = 40; int border = 10; boolean change; Signal
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
Can someone help me with this?
Can someone help me with this?  I have this project and i dont know how to do it. Can someone help me? please? Write a java class named "PAMA..., Multiply, Divide) Help me please! Thanks in advance!   import
help in java please
help in java please  Define a class named Money whose objects represent amounts of U.S. money. The class will have two instance variables of type... dollars) â?? void setCents(int cents) â?? boolean equals(Money amount) â
java help - Java Beginners
; char c; boolean done =false; System.out.println("Enter the number
Please help with this code
Please help with this code  I need some help getting the Search method below to work with the menu, and I also cannot figure out how to get my bubble...; } } return result; } private static boolean
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
help
help   how i can send a pitcture on url in java
Need help in java programming
Need help in java programming  Hello. Can someone please help me with writig the following programm. Assignment 20% Presentation 10% Mini Test...(); System.out.println("1. Add a student"); System.out.println("2. Exit"); boolean quit = false
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
program help - Java Beginners
"); choice=console.nextInt(); do { boolean found=true
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
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
Logic error? HELP PLEASE! :(
Logic error? HELP PLEASE! :(  Hello Guys! i have a huge problem. What... = Calendar.getInstance(); private boolean filled = false; private String nric... = "01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random r = new Random(); public boolean isFilled
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  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

Ask Questions?

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.