0

I am writing a program to implement "quiz" in Java 8

import java.util.Scanner;
import java.util.*;

class Question {
    String question;
    int no;
    HashMap<Integer, String> options;
    String correctanswer;

    public static void q_no() {
        System.out.println("enter a question no");
        int num;
        Scanner no = new Scanner(System.in);
        num = no.nextInt();
    }

    public static void set_question(){
        System.out.println("enter aquestion");
        Scanner f = new Scanner(System.in);
        String question = f.nextLine();

    }

Here in setting options, I am getting errors in the while loop while implementing the program."runtime errors thrown as NullPointerExceptions"

    public void set_options() {
        System.out.println("enter options");
        int count = 1;
        String q;
        Scanner str = new Scanner(System.in);
        while (count < 5) {
            System.out.print("set option" + count);
            q = str.nextLine();
            options.put(count, q);
            count++;
        }
    }

    public void set_correctanswer() {
        System.out.println("enter correct answer");
        Scanner d = new Scanner(System.in);
        String ans = d.nextLine();
    }

    public static void marks() {

    }          

}

Here you would write your questions to an ArrayList and display them in a second question.

class Quiz {
    ArrayList<Question> list = new ArrayList<>();
    public ArrayList<Question> adding(Question q){
        list.add(q);
        return list;
    }
    public void show() {
        int g = list.size();
        int count = 0;
        while (count < g) {
            list.get(count);
        }
    }
}

In menu, if you choose 1 you get add questions or type 2 you could get a list of questions. I am still working on printing questions.

class Menu {
    public static void main(String[] args) {
        System.out.println("enter 1 for adding questions");
        Scanner input = new Scanner(System.in);
        int g;
        g = input.nextInt();
        if (g == 1) {
            System.out.println("how many questions do you add");
            int number;
            Scanner b = new Scanner(System.in);
            Quiz programming = new Quiz();
            number = b.nextInt();
            int count = 0;
            while (count < number) {
                Question q1 = new Question();
                q1.q_no();
                q1.set_question();
                q1.set_options();
                q1.set_correctanswer();
                programming.adding(q1);
            }

        } else if(g == 2){
            Quiz programming = new Quiz();
            programming.show();
        }

    }
} 
egor.zhdan
  • 4,555
  • 6
  • 39
  • 53
  • What was the question? – Morten Jensen Aug 24 '16 at 15:18
  • if you think my question is still not upto standards, inform me guys so that i would remove it immediately i dont want to get m account blocked for 2-3 days . – Papasani Mohansrinivas Aug 24 '16 at 15:18
  • I would give options to user to enter either 1 or 2."1" would allow to set questions for quiz. And "2" would allow them to enter a quiz. – Papasani Mohansrinivas Aug 24 '16 at 15:25
  • In class Menu i would give options to user to enter either 1 or 2."1" would allow to set questions for quiz. And "2" would allow them to enter a quiz.if user enters option 1. He would have to set questions and options too.In method called "set_options" my compiler is throwing null pointer exception – Papasani Mohansrinivas Aug 24 '16 at 15:30
  • Next time before posting a question please reformat your code to make it a little bit more readable. Most of the modern IDEs can even do it for you automatically. – egor.zhdan Aug 24 '16 at 15:51
  • **options** was not initialized as = new HashMap<...>() – Sgene9 Aug 24 '16 at 16:33

1 Answers1

0

This is a poor implementation.

You have System.out calls to interact with a user inside your Question class.

A better idea would be to move user actions out into a driver class.

You don't follow Java coding standards or JavaBean standards. You will surely come to grief over both. Should be setCorrectanswer(), not set_correctanswer().

duffymo
  • 305,152
  • 44
  • 369
  • 561