1

I've posted my code below. I am having a problem on the line declaring the array wrongAnswers. I've been able to get my code working before, but the problem is that some person took it upon themselves to delete all my files. I was able to get it working without using List or ArrayList. I just want to understand how I can get this working now before I try using either of those other methods. I understand that Java arrays are immutable. However, I was still somehow able to get it to work before. If someone could help me figure out what I did previously, I would be most greatful.

private Scanner keyboard = new Scanner(System.in);

private final String[] testAnswers = {
        "B","D","A","A","C",
        "A","B","A","C","D",
        "B","C","D","A","D",
        "C","C","B","D","A"};
private String[] studentAnswers = new String[20];
/*
private String[] studentAnswers = {
        "B","D","A","A","C",
        "A","B","A","C","D",
        "B","C","D","A","D",
        "C","C","B","D","A"};
*/
private int[] wrongAnswers;
private int answeredCorrectly = 0;

public void getStudentAnswers() {
    for(int x = 0; x < 20; x++) {
        do {
            System.out.print("Enter answer for #" + (x + 1) + " : ");
            this.studentAnswers[x] = keyboard.next().toUpperCase();
            if (!"A".equals(this.studentAnswers[x]) && 
                    !"B".equals(this.studentAnswers[x]) &&
                    !"C".equals(this.studentAnswers[x]) && 
                    !"D".equals(this.studentAnswers[x])) {
                System.out.println("Invalid input.");
            }
        } while(!"A".equals(this.studentAnswers[x]) && 
                !"B".equals(this.studentAnswers[x]) &&
                !"C".equals(this.studentAnswers[x]) && 
                !"D".equals(this.studentAnswers[x]));
    }
}

public int totalCorrect() {
    int arrayLocation = 0;

    for(int x = 0; x < 20; x++) {
        if (this.studentAnswers[x].equals(this.testAnswers[x])) 
            this.answeredCorrectly++;
        else 
            this.wrongAnswers[arrayLocation++] = x;
    }

    return this.answeredCorrectly;
}

public int totalIncorrect() {
    return 20 - this.answeredCorrectly;
}

public boolean passed() {
    return this.answeredCorrectly >= 15;
}

public void questionsMissed() {
    if(this.answeredCorrectly != 20) {
            for(int x = 0; x < this.wrongAnswers.length; x++) {
            System.out.println(this.wrongAnswers[x]);
        }
    }
}
6
  • wrongAnswers = new int[someNumber]; But i think it would be better to use a List instead Commented Nov 8, 2016 at 14:02
  • I wouldn't be able to do that since the array size of wrongAnswers is dependent on how many correct answers were given. It would change every time I ran the program. The number needs to be flexable Commented Nov 8, 2016 at 14:04
  • 1
    Either you need to manually create a new bigger one each time and copy the old one + the new wrong answer in every time you want to add one. Or use an ArrayList Commented Nov 8, 2016 at 14:05
  • Somehow before I was able to do neither and get it to work. I wish I could show you. Commented Nov 8, 2016 at 14:09
  • 1
    That is impossible. You probably just allocated it to be big enough at the beginning. Like declaring it to have the same size as there are anwsers. Or maybe you used some util methods that hid that. Commented Nov 8, 2016 at 14:12

2 Answers 2

1

If code is well written, saving space (which is what you are trying to do) will usually cost performance and vice versa. You can achieve what you want, but you'll lose performance, as you'll see.

I find deduction to be useful when solving similar problems. Conditions:

1) Arrays are immutable 2) You want to allocate the exact amount of space that you need

Point 2 poses a question: how do you know how much space you need? Obvious answer: know how many (in)correct answers you have. Following from there you can do:

public int totalCorrect() {
    for(int x = 0; x < 20; x++) {
        if (this.studentAnswers[x].equals(this.testAnswers[x])) 
            this.answeredCorrectly++;
    }

    this.wrongAnswers = int[20 - this.answeredCorrectly];

    // Here you want to create your wrongAnswers, but you have to go over 
    // the same array twice...
    int arrayLocation = 0;
    for(int x = 0; x < 20; x++) {
        if (!this.studentAnswers[x].equals(this.testAnswers[x])) 
            this.wrongAnswers[arrayLocation++] = x;
    }


    return this.answeredCorrectly;
}

There are probably more ways to do something similar and achieve better performance too. At first sight they seem to me like bad approaches and I'd use a List, as has been proposed already, or perhaps a Set, but who knows...

Sign up to request clarification or add additional context in comments.

1 Comment

I will try this out once I get into class. I finally caved in and learned ArrayList. Though, I'm sure my teacher will take points off for jumping ahead the chapter I'm in by using it
0

private int[] wrongAnswers = new int [20];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.