Evening all,

My assignment is as follows:

Consider a program that administers multiple-choice quizzes. The student needs to provide a response such as A or D. Your job will be to check for input errors in the student response string. Check that the string has length 1, and that it is a letter between A and the last valid choice for the problem.

Complete the following code:

public class Inputs
{
   /**
      Gets the choice that the user provided, or null if the 
      user didn't provide a valid choice.
      @param input the user input
      @param maxChoice the maximum valid choice, e.g. "D" if there
      are four choices.
      @return the user input if it was a valid choice (i.e. length 1
      and between "A" and maxChoice), null otherwise
   */
   public String getChoice(String input, String maxChoice)
   {
      // Your work here


   }
}

The validation website it currently down, so here is what I did. Would some of you experts take a look and see if I did this correctly, please.

Thank you!

public class Inputs
{
   /**
      Gets the choice that the user provided, or null if the 
      user didn't provide a valid choice.
      @param input the user input
      @param maxChoice the maximum valid choice, e.g. "D" if there
      are four choices.
      @return the user input if it was a valid choice (i.e. length 1
      and between "A" and maxChoice), null otherwise
   */
   public String getChoice(String input, String maxChoice)
   {
      String aInput;
      String amaxChoice;
    
      aInput = input;
      amaxChoice = maxChoice;
      
      int choiceA = 1;
      int choiceB = 2;
      int choiceC = 3;
      int choiceD = 4;
      
      
   
      
      if (aInput.length() == 1)
          {
              if (aInput.equals("A"))
                  {
                      System.out.println("Valid answer: A");
                  }
              else if (aInput.equals("B"))
                  {
                      System.out.println("Valid answer: B");
                  }
              else if (aInput.equals("C"))
              
                  {
                      System.out.println("Valid answer: C");
                  }
              else if (aInput.equals("D"))
                  {
                      System.out.println("Valid answer: D");
                  }
              else
                  {
                      System.out.println("Invalid answer");
                  }
           }
       else
        {
            System.out.println("Please enter only one letter");
        }
        
           
        
                    
            
            return aInput;
              
      


   }
}

Recommended Answers

All 5 Replies

Is it required to use maxChoice or is it not really necessary cause you did not refer to it for the process?
plus you did not use these variables too

int choiceA = 1;
int choiceB = 2;
int choiceC = 3;
int choiceD = 4;

As @zeroliken said, there was no need for the choiceA, choiceB, choiceC, and choiceD variables. Other than what he said, I don't see anything wrong with your code. You should run it once and check it out! :)

there is one remark though:
you only should return the user input if it was valid, null otherwise, so:

if (aInput.length() == 1)
          {
              if (aInput.equals("A"))
                  {
                      System.out.println("Valid answer: A");
                  }
          //    ... your other if's
              else
                  {
                      System.out.println("Invalid answer");
                      return null; // since here it would be invalid
                  }
           }
       else
        {
            System.out.println("Please enter only one letter");
            return null; // here also, it would be invalid
        }
            // with invalid input, you won't reach this statement, so you would not
            // return the input if it was invalid
 
            return aInput;

Thank you all for the input.

there is one remark though:
you only should return the user input if it was valid, null

Im sure I need to utilize the getMaxChoice but I'm not sure how. Our book is vague and severely lacks examples "Big Java"

all books seem vague, but sometimes that's just because the example is not very extensive.
I never said you're not supposed to use getMaxChoice, I'm just pointing out that in your assignment, it says:

@return the user input if it was a valid choice (i.e. length 1
and between "A" and maxChoice), null otherwise

the return annotation tells you what your method is supposed to return and in which case. so, is the choice invalid, return null; else, return the String you used as input. that's what I did in the bit code I adapted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.