Conor
Conor

Reputation: 395

An issue with the logic in the code using the Scanner object

I'm getting frustrated with something really basic here.. scanner just isn't doing what I want and I don't know why, I'd be really grateful if someone could help me

so i'm trying to program a a basic inventory and I want a scanner to check if a valid builder name has been entered. A string variable stores the user entered word, if an invalid builder name was entered I just want that string variable to be equal to "ANY". What is happening is - the string comes out correct (in capitals as I wanted) AS WELL as "Any", which it shouldn't!, when something invalid is entered, nothing is printed (and it should print "Any")

i've made a simple SSCCE:

import java.util.Scanner;

public class SSCCE {

    public static void main(String[] args)
    {
        System.out.println("Enter a builder name: ");
        Scanner scan = new Scanner(System.in);
        //scan.useDelimiter("\\z"); // count a blank entry (end of input)

        String entry_1 = scan.next();


        if (entry_1.equalsIgnoreCase("FENDER")
                        || entry_1.equalsIgnoreCase("MARTIN")
                        || entry_1.equalsIgnoreCase("GIBSON")
                        || entry_1.equalsIgnoreCase("COLLINGS")
                        || entry_1.equalsIgnoreCase("OLSON")
                        || entry_1.equalsIgnoreCase("RYAN") 
                        || entry_1.equalsIgnoreCase("PRS")) 
        {
            entry_1 = entry_1.toUpperCase();

            System.out.println(entry_1);
        }


    //  if (entry_1.equals(entry_1.toLowerCase())
    //          || entry_1.equalsIgnoreCase(entry_1)
    //          && (entry_1.equalsIgnoreCase("FENDER")
    //                  || entry_1.equalsIgnoreCase("MARTIN")
    //                  || entry_1.equalsIgnoreCase("GIBSON")
    //                  || entry_1.equalsIgnoreCase("COLLINGS")
    //                  || entry_1.equalsIgnoreCase("OLSON")
    //                  || entry_1.equalsIgnoreCase("RYAN") || entry_1
    //                      .equalsIgnoreCase("PRS"))) 
    //  {
    //      entry_1 = entry_1.toUpperCase();
    //  }

        if (!entry_1.equalsIgnoreCase("FENDER")
                || !entry_1.equalsIgnoreCase("MARTIN")
                || !entry_1.equalsIgnoreCase("GIBSON")
                || !entry_1.equalsIgnoreCase("COLLINGS")
                || !entry_1.equalsIgnoreCase("OLSON")
                || !entry_1.equalsIgnoreCase("RYAN")
                || !entry_1.equalsIgnoreCase("PRS")) {

            entry_1 = "ANY";

            System.out.println(entry_1);
        }   
    }

}

edit: thanks for all responses, now i'm just having a problem with scan.useDelimiter("\z"); when nothing is entered it should say "ANY" and so it does, but now it will always say "ANY" even for correct entry

Upvotes: 0

Views: 101

Answers (5)

user2972135
user2972135

Reputation: 1461

In the second if () use && not ||. That's all ;)

Upvotes: 0

Robert H
Robert H

Reputation: 11730

You need to change your second if statement from using || to &&:

if (!entry_1.equalsIgnoreCase("FENDER")
                    && !entry_1.equalsIgnoreCase("MARTIN")
                    && !entry_1.equalsIgnoreCase("GIBSON")
                    && !entry_1.equalsIgnoreCase("COLLINGS")
                    && !entry_1.equalsIgnoreCase("OLSON")
                    && !entry_1.equalsIgnoreCase("RYAN")
                    && !entry_1.equalsIgnoreCase("PRS")) {

If you use || only one of the continues needs to be true for the if statement itself to be true and cause ANY to display as the output. With && all the statements must evaluate for true before the if statement will execute and display ANY

Upvotes: 0

rgettman
rgettman

Reputation: 178313

In the second if, it's guaranteed that entry_1 isn't one case OR it isn't another case OR... The logic is incorrect. You want "and", with &&:

if (!entry_1.equalsIgnoreCase("FENDER")
        && !entry_1.equalsIgnoreCase("MARTIN")
        && !entry_1.equalsIgnoreCase("GIBSON")
        && !entry_1.equalsIgnoreCase("COLLINGS")
        && !entry_1.equalsIgnoreCase("OLSON")
        && !entry_1.equalsIgnoreCase("RYAN")
        && !entry_1.equalsIgnoreCase("PRS")) {

    entry_1 = "ANY";

    System.out.println(entry_1);
}   

However, it would have been simpler to attach it to the first if condition as an else:

if (entry_1.equalsIgnoreCase("FENDER")
                    || entry_1.equalsIgnoreCase("MARTIN")
                    || entry_1.equalsIgnoreCase("GIBSON")
                    || entry_1.equalsIgnoreCase("COLLINGS")
                    || entry_1.equalsIgnoreCase("OLSON")
                    || entry_1.equalsIgnoreCase("RYAN") 
                    || entry_1.equalsIgnoreCase("PRS")) 
{
    entry_1 = entry_1.toUpperCase();

    System.out.println(entry_1);
}
else
{
    entry_1 = "ANY";

    System.out.println(entry_1);
}

Upvotes: 0

Just change whole of second if condition with else:

if (entry_1.equalsIgnoreCase("FENDER")
                            || entry_1.equalsIgnoreCase("MARTIN")
                            || entry_1.equalsIgnoreCase("GIBSON")
                            || entry_1.equalsIgnoreCase("COLLINGS")
                            || entry_1.equalsIgnoreCase("OLSON")
                            || entry_1.equalsIgnoreCase("RYAN") 
                            || entry_1.equalsIgnoreCase("PRS")) 
            {
                entry_1 = entry_1.toUpperCase();

                System.out.println(entry_1);
            }

            else {

                entry_1 = "ANY";

                System.out.println(entry_1);
            } 

Upvotes: 0

nhgrif
nhgrif

Reputation: 62062

Your second if statement, where you're checking !entry_1, etc. should use && rather than ||.

"FENDER" will return true for !entry_1.equalsIgnoreCase("MARTIN"); etc, and in a list of || comparisons, only one of them has to be true to make the whole thing return true.

Upvotes: 3

Related Questions