0

Am working on a problem, basic code is shown below, takes 4 possible inputs from the user, and produces a response based on which one is input. However I need to add a test to validate that the only 1 of the 4 possible answers has been input.

I know how I could compare 2 of the 4 inputs however not all 4 at once, can someone give me an idea? Would prefer to figure out the actual code myself but a pointer in the right direction would be beneficial.

So to clarify How to - If input is anything except "Bill, Circular, Postcard or Letter" produce Error Message X

    System.out.println("What type of Letter has been received?");
    System.out.println("Bill, Circular, Postcard or Letter");
    String Letter = kybd.nextLine();

        {
            if (Letter.equalsIgnoreCase("Bill"))  
            {
                System.out.println("Bills must be paid");
            }
            else if (Letter.equalsIgnoreCase("Circular"))
            {
                System.out.println("Circulars are thrown away");
            } 
            else if (Letter.equalsIgnoreCase("Postcard"))
            {
                System.out.println("Postcards are put on the wall");
            } 
            else if (Letter.equalsIgnoreCase("Letter"))
            {
               System.out.println("Personal letters are read and have replies written for them");
            }
        }
1
  • Huge hint: if(a && b && c) is valid, as well as if(a || b || c) Commented Oct 23, 2014 at 19:48

2 Answers 2

1
            if (Letter.equalsIgnoreCase("Bill"))  
            {
                System.out.println("Bills must be paid");
            }
            else if (Letter.equalsIgnoreCase("Circular"))
            {
                System.out.println("Circulars are thrown away");
            } 
            else if (Letter.equalsIgnoreCase("Postcard"))
            {
                System.out.println("Postcards are put on the wall");
            } 
            else if (Letter.equalsIgnoreCase("Letter"))
            {
               System.out.println("Personal letters are read and have replies written for them");
            }else{
            System.out.println("ERROR");
            }
Sign up to request clarification or add additional context in comments.

1 Comment

I'm such an idiot......*sigh* Looking at this too complicated and the answer is so simple.
1

You want to throw an 'else' conditional statement in there, and you may want to look at try and catch blocks, or simply 'throw' a NoSuchElement exception in your 'else' conditional.

Looking at this may be useful to you:

http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Throwing an exception in an else statement would be a lot like the answer before mine, whereas a try...catch block is pretty much the test you're talking about. So, it could look something like this:

try
{
  Letter != "Bill"  //not exactly how it'd look, but this is a general idea on what you'd do here 
}
catch (NoSuchElementException e)
{
   System.out.println("Not a valid input.");
}

1 Comment

Thanks, will look into those, am sure I'll end up using them soon enough.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.