tags:

views:

101

answers:

3

Dunno why it is...heres the coding for it.

http://pastebin.org/301343

/**********************************\
*                                  *
*       Multiplication Quiz        *
*    Developed and Written by:     *
*          Nick Gibson             *
*             CIT                  *
*       ***Finished Code***        *
*                                  *
\**********************************/

import javax.swing.JOptionPane;
import java.awt.*;
import java.lang.Math.*;
import java.util.*;
import javax.swing.*;
import java.text.*;
import java.text.DecimalFormat.*;
import java.io.*;
import java.lang.Integer.*;



public class TestMaker
{
    public static void main(String[] args)
    {
        int y = 1, x, quesNum = 1, z, zz, qq, g, u = 1;

        while (y == 1)
        {
            String xx = JOptionPane.showInputDialog(null,"How many questions is this test? \n(May be 1 - 99 questions)");
                x = Integer.parseInt(xx);

            if(x > 99 || x < 1)
            {
                JOptionPane.showMessageDialog(null,"Invalid number. Please try again.", "Invalid Number", JOptionPane.ERROR_MESSAGE);
            }
            else
            {

                String[] questions = new String[25];
                String[] answers = new String[25];
                String[] a = new String[25];
                String[] b = new String[25];
                String[] c = new String[25];
                String[] d = new String[25];

                while(u == 1)
                {

                    questions[1] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[1] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[1] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[1] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[1] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[1] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[2] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[2] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[2] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[2] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[2] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[2] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[3] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[3] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[3] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[3] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[3] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[3] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[4] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[4] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[4] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[4] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[4] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[4] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[5] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[5] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[5] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[5] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[5] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[5] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[6] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[6] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[6] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[6] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[6] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[6] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[7] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[7] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[7] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[7] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[7] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[7] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[8] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[8] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[8] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[8] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[8] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[8] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[9] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[9] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[9] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[9] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[9] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[9] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[10] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[10] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[10] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[10] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[10] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[10] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                }//while

                    zz = JOptionPane.showConfirmDialog(null,"All questions are completed. \nWould you like to review any questions?", "Question Review", JOptionPane.YES_NO_OPTION);

                    if(zz == JOptionPane.YES_OPTION)
                    {
                        while(u == 1)
                        {
                            String q = JOptionPane.showInputDialog(null,"Which question would you like to review? \n\nCurrently on Question "+quesNum+"\n\nPress 0 to exit.");
                            qq = Integer.parseInt(q);

                            if(qq == 0)
                            {
                                break;
                            }

                            JOptionPane.showMessageDialog(null,"You are now going to edit question "+qq);
                            questions[qq] = JOptionPane.showInputDialog(null,"Question: "+questions[qq]+"\nType in the correct question.");
                            a[qq] = JOptionPane.showInputDialog(null,"A: "+a[qq]+"\nType in answer A");
                            b[qq] = JOptionPane.showInputDialog(null,"B: "+b[qq]+"\nType in answer B");
                            c[qq] = JOptionPane.showInputDialog(null,"C: "+c[qq]+"\nType in answer C");
                            d[qq] = JOptionPane.showInputDialog(null,"D: "+d[qq]+"\nType in answer D");
                            answers[qq] = JOptionPane.showInputDialog(null,"Answer: "+answers[qq]+"\nPlease type the correct answer. \nType in lowercase a - d");

                            JOptionPane.showMessageDialog(null,questions[qq]+"\n\nA: "+a[qq]+"\nB: "+b[qq]+"\nC: "+c[qq]+"\nD: "+d[qq]+"\n\nCorrect Answer: "+answers[qq]);

                        }//while

                    }//if

                if(quesNum > x)
                {
                    JOptionPane.showMessageDialog(null,"That concludes the creation portion of the test. \nIt is now time to take the test.");
                }

                JOptionPane.showMessageDialog(null,"When you ready for the test, press OK.");

                double number1 = (Math.random() * 10);
                double number2 = (Math.random() * 10);
                double number3 = (Math.random() * 10);

                long aa, bb, cc, ss;
                int abc;

                aa = Math.round(number1);
                bb = Math.round(number2);
                cc = Math.round(number3);

                ss = aa;

                String str = Long.toString(ss);
                abc = Integer.parseInt(str);

                JOptionPane.showMessageDialog(null,questions[abc]+"\n\n"+a[abc]+"\n"+b[abc]+"\n"+c[abc]+"\n"+d[abc]);

            }//else

        }//while y = 1

    }//public main

}//public class

I know theres a lot of repetitiveness in the coding...but its because the arrays were retuning null so I got tired of messing with them.

Everything works until it reaches line 224, which returns null values.

+1  A: 

null is a valid value for an array. Both the pointer to the array and the contents in the array may be null. I took the time to read your too-long code. It looks like your code is not filling the arrays completely. For one thing, you do not put anything in slot 0 (questions[0] is always null).

Also, your code should be a for loop. And you should allocate the arrays to the number of questions allowed, not a fixed number. This way you will be certain that your arrays have no null elements (assuming JOptionPane doesn't return null if the user doesn't enter any value).

String[] questions = new String[x];
String[] answers = new String[x];
String[] a = new String[x];
String[] b = new String[x];
String[] c = new String[x];
String[] d = new String[x];

for (int i = 0; i < x; i++)
{

  questions[i] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+i);
  a[i] = JOptionPane.showInputDialog(null,"What is the answer for a?");
  b[i] = JOptionPane.showInputDialog(null,"What is the answer for b?");
  c[i] = JOptionPane.showInputDialog(null,"What is the answer for c?");
  d[i] = JOptionPane.showInputDialog(null,"What is the answer for d?");
  answers[i] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
}
Mr. Shiny and New
Well its the user types in a question and it stores is in array[10]...then at the bottom it randomizes a number 1 - 10 and whichever it lands on depends on which question is pulled. So if it randomized a 7...question 7 would show in that format. Yet only null shows.
Nick Gibson
@mdma: The original question didn't specify Java.
Mr. Shiny and New
So then I can refer to i later down in the code as to display the correct value within the array?
Nick Gibson
@Nick Gibson: No, `i` is only valid while you are in the `for` loop. But later on if you want a new `i` you can create one. Once the arrays are filled you can see how many questions there are by doing `questions.length`.
Mr. Shiny and New
Ok well one more question, are the values still in the array after the for loop, so as to call them out and send them to a value during the later part of the code for testing.
Nick Gibson
@Nick Gibson: The arrays are declared outside the loop, and the loop merely writes data into the array. The `int i` is declared inside the foor loop (well... sorta) and disappears afterwards. So the data should be there. Even in your original code the data is still there. I debugged it and saw it for myself.
Mr. Shiny and New
A: 

Your arrays, a,b,c,d, questions, answers are initialized to size 25. This will mean that they all hold null values initially - 25 of them. Your code fills in indexes 1..10, and then reviews a random question.

EDIT: The root cause is how you are genering random numbers, which can be 0, and index 0 is not filled. The array elements 1..10 are filled, but 0 and 11..24 are not. If the random number comes out as 0, you'll get a NullPointerException.

The fix is to set your question numbers starting from 0, and instead of Math.round(), cast the random number to an int which truncates the frantional part, so the range is 0..9.

mdma
@mdma, "10..24 are not" as a typo. Should be: *11..24* are not
JohnB
thanks! - fixed.
mdma
+1  A: 
JohnB
Yeah I knew 1 - 10, I guess I forgot about including 0 in there since I had nothing in the 0 section. I was zero-indexing to start with but it was confusing when the user inserted 25 questions to add and it said Question #0 lol
Nick Gibson
Also thank you very much on the Math help. the Math class was giving me problems since it generates a number as a data type of long, and was trying to make it an int. I never got into converting numeric data types, so I parsed the long into a String and then parsed the string into an int.
Nick Gibson
@Nick Gibson, yes `Math.round(Math.random()*10);` could generate a **0** 9% of the time, and as you just realized, the `[0]` index of the array has nothing in it. If this is the answer to your question, you should click on the check mark. Good luck with you program.
JohnB