Witshaper is the Professional (Computing Skills, English Learning and Soft Skills) Skill Development Training Center. We provide Professional IT Courses and Soft Skill Training in Dehradun to Students, Employees and organization. Who wish to pursue a career in IT Technology. Witshaper is led by a motivated team of IT experts and Soft Skill Professionals. We provide high quality trainings. Our Emphasis is on giving the practical knowledge to the students, so that they will get to know in depth and never forget what they opt, we provide to the students real learning environment. Witshaper prepares students and professionals to be the part of this growing industry. Be a part of Witshaper and get your dreams successful

Sunday 15 May 2016

console based Quiz application java using file handling

QuizTest.java 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author rajani sharma
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class QuizTest {
public static void main (String[] args) throws IOException{

    String question = "";
    String choiceA = "";
    String choiceB = "";
    String choiceC = "";
    String choiceD = "";
    String answer = "";
    String line;
    String userAnswer;
    int i = 0;
    BufferedReader reader = null;          

        reader = new BufferedReader(new FileReader("Caitto.txt"));
        Scanner scan = new Scanner(System.in);

        QuizClass run = new QuizClass(i, question, choiceA, choiceB, choiceC, choiceD, answer);

        while((line = reader.readLine()) != null){

            //Finds the question
            if(line.contains("?")){
                run.question = line;
                System.out.println(run.getQuestion() + "\n");
            }

            //Finds answer "A"
            if(line.contains("A.")){
                run.choiceA = line;
                System.out.println(run.getChoiceA());
            }

            //Finds answer "B"
            if(line.contains("B.")){
                run.choiceB = line;
                System.out.println(run.getChoiceB());
            }

            //Finds answer "C"
            if(line.contains("C.")){
                run.choiceC = line;
                System.out.println(run.getChoiceC());
            }
            //Finds answer "D"
            if(line.contains("D.")){
                run.choiceD = line;
                System.out.println(run.getChoiceD() + "\n");
            }

            //Finds the correct answer for the question
            if(line.contains("Correct Answer:"))
            {
                //System.out.println("correct answer block");
                String[] a = line.split(":");
                answer = a[a.length - 1];
                run.answer = answer;
               // System.out.print(" Answer:"+answer);
                System.out.print("Your Answer:");
                userAnswer = scan.next();

                    //Checks if the user's input matches the correct answer from the file
                    if(userAnswer.equalsIgnoreCase(answer)){
                        System.out.println("Correct!\n");
                        i++;
                    }

                    //Checks if the user's input doesn't match the correct answer from the file
                    else if (!userAnswer.equalsIgnoreCase(answer)) {
                        System.out.println("Wrong, the correct answer was: " + run.getAnswer());
                        i++;
                    }

            }


        }

                if(i == 274){
                    reader.close();
                    scan.close();
                }
}
}




QuizClass.java 


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author rajani sharma
 */
public class QuizClass {

    String question, answer, choiceA, choiceB, choiceC, choiceD;
    private int questionNum;

    public QuizClass(int questionNum, String question, String choiceA, String choiceB, String choiceC, String choiceD, String answer){
        this.question = question;
        this.answer = answer;
        this.choiceA = choiceA;
        this.choiceB = choiceB;
        this.choiceC = choiceC;
        this.choiceD = choiceD;
    }

    public String getQuestion(){
        return question;
    }

    public String getAnswer(){
        return answer;
    }

    public String getChoiceA(){
        return choiceA;
    }

    public String getChoiceB(){
        return choiceB;
    }

    public String getChoiceC(){
        return choiceC;
    }

    public String getChoiceD(){
        return choiceD;
    }

    public int getQuestionNum(){
        return questionNum;
    }


}

No comments:

Post a Comment