0

Looking for some help here. I'm trying to make this method work. Whenever I run the method, it throws IllegalArgumentException even if I do type in A,B,C or D. I am using an inputReader class. Here is the code for my DriverExam class. Please let me know where I am going wrong. I'd like it so the only valid answers are a,b,c,or d. I have to use a while loop and everything I've tried won't help me match the driverAnswers.

public class DriverExam
{
    // instance variables
    public static final String[] ANSWERS = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"};

    private String [] driverAnswers;
    private InputReader inputReader;

    public DriverExam(){
        driverAnswers = new String[20];
        inputReader = new InputReader();
    }

    public void promptStudentAnswers(){
        int index = 0;

        while(index < driverAnswers.length){
            System.out.println("enter answer");
            String driverAnswers = inputReader.readString();
            if(driverAnswers != ANSWERS[index]){
                throw new IllegalArgumentException(" answers can only be A,B,C or D");
            } else{ 
                index++;
            }
        }
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • and why do you want to throw an Exception? – Scary Wombat Mar 10 '20 at 04:02
  • i have an understanding of what == is and .equals @ScaryWombat its just part of the criteria. I have to make a few DriverExam methods. Stuck on this first one. I'm pretty much stuck on making the method run properly. I took out the other new local variable, I was tinkering around with the code hoping to stumble on the right answer. If I do enter something like if{driverAnswers = "ABCD") { driverAnswers[index] = answer; index++; } else {S.O.P("") it gives me an error incomparable types java.lang.string[] and java.lang.String –  Mar 10 '20 at 04:41

1 Answers1

1

First, you want to test if the answer is one of A, B, C or D (not that the answer matches something in the correct answers array). Also, your driverAnswers is masked because you created another local variable with that name. Basically, I think you wanted something like

public void promptStudentAnswers() {
    int index = 0;

    while (index < driverAnswers.length) {
        System.out.println("enter answer");
        String answer = inputReader.readString().trim().toUpperCase();
        if (answer.length() == 1 && "ABCD".indexOf(answer) != -1) {
            driverAnswers[index] = answer;
            index++;
        } else {
            System.out.println("Answers can only be A,B,C or D");
        }
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • hey Elloit Is there a way to simplify the method? I am a first year student for programming and we just learned about arrays in Java. I trying to understand it with what I've learned. Is there a way to simplify this?(answer.length() == 1 && "ABCD".indexOf(answer) != -1) why is answer.length() ==1 ? –  Mar 10 '20 at 04:21
  • To make sure it is one character. And that one character appears in `ABCD`. That's about as simple as methods come (and is a `String` not an array). – Elliott Frisch Mar 10 '20 at 04:24
  • You are awesome for your help. I havent learned about .indexof. Can you further explain answer.length() ==1? and != -1? why 1 and -1? thank you –  Mar 10 '20 at 04:30
  • `answer.length() == 1` tests that **`answer` is one character** ([`String.length()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--) gets the length of the `String`). [**`String.indexOf(String)`**](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#indexOf-java.lang.String-) ***returns** the index of the first occurrence of the specified substring, or `-1` if there is no such occurrence.* These are standard methods of `String`. – Elliott Frisch Mar 10 '20 at 04:39
  • We haven't gone over this yet, I cant use what I haven't been taught yet unfortunately :(. Im just picking at your brain, I hope im not too annoying! I got a hint from my prof but I cant find the method to do it. Ask the user for an answer, check if the answer is a b c or d, if the answer is valid, put it in the array and increment the counter. else throw in an illegal Argument. I tried doing driverAnswers == "ABCD" but i get an incomparable type error which sucks cause theyre both String but ones String[] and the other is just String. Thanks for everything Elliot –  Mar 10 '20 at 04:50
  • A `String[]` is **not** a `String`. Here is your "simplified" way: `if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D"))` - if you haven't gone over `if` and `or` I can't really help you further; I don't know what you have been taught yet. – Elliott Frisch Mar 10 '20 at 04:54
  • that works! been taught that. Will give it a go, and once again thanks :)! –  Mar 10 '20 at 04:58