Hey, I am trying to use this program, it is supposed to ask one multiple choice question of the user and get the response. For some reason it will not work for me. I am using Ready to Program by HoltSoft. Please help me.

import java.awt.*;
import java.applet.*;
import java.io.*;

public class Quiz_Show extends Applet
{
    public static void main (String args[])
    {
        InputStreamReader istream = new InputStreamReader (System.in);
        BufferedReader bufRead = new BufferedReader (istream);
        String First_Answer = "";
        int score = 0;
        System.out.println ("Welcome to my quiz show.");
        System.out.println ("Please enter the letter of the answer for each question.");
        System.out.println ("Here is the first question.");
        System.out.println ("What is the largest country in the world?");
        System.out.println ("A-Canada");
        System.out.println ("B-Russia");
        System.out.println ("C-USA");
        System.out.println ("D-China");
        while (First_Answer.equalsIgnoreCase (""))
        {
            try
            {
                System.out.println ("Please answer with a, b, c, or d");
                First_Answer = bufRead.readLine ();
            }
            catch (IOException err)
            {
                System.out.println ("Error reading line");
            }
        }
        if (First_Answer.equals ("b"))
        {
            System.out.println ("Good job, that is correct!");
            score = score + 15;
        }
        else
        {
            System.out.println ("Sorry, that answer is incorrect.");
        }
    }
}

When I try to run it it comes up "Error Message=The specified module could not be found.
Error Code=126"

Thanks in advance for any hep you can provide.

Dani AI

Generated

The immediate bug is a logic one: the same Answer string is reused for the second question without being cleared, so the second while loop never runs (the variable already contains the first answer). As and noted, the class also extends Applet while using console I/O — that worked in the IDE because you ran main, but real applets in a browser do not have System.in.

Fixes (pick one):

  • Reset or null the answer before prompting the next question, for example answer = "" (or answer = null) so the input loop will run again.
  • Use distinct variables (answer1, answer2) or, better, a helper method that prompts and returns a validated answer. That keeps the code clean and avoids accidental reuse.
  • Compare answers case-insensitively and trim whitespace so B or b both match. See String.equalsIgnoreCase for comparisons.

Example helper pattern (use one helper call per question):

import java.util.Scanner;

private static String promptAnswer(Scanner sc, String prompt) {
    String a;
    do {
        System.out.print(prompt);
        a = sc.nextLine();
        if (a == null) return "";
        a = a.trim();
    } while (a.isEmpty());
    return a.toLowerCase();
}

Additional notes: if you truly want an applet, remove console code and use applet lifecycle methods and GUI components; applets do not get System.in in a browser and applets are obsolete in modern Java. For simple learning work, stick to a standalone console program (no extends Applet) or convert to a Swing/JavaFX GUI. For reference on string comparison and input helpers see the Java docs for String.equalsIgnoreCase and Scanner (both in the Java SE docs).

Recommended Answers

All 5 Replies

Sounds like a question for the producers of your IDE, as that is not a Java error.

However, do you want an Applet or a Stand-alone app?

You've extended Applet, but written a Stand-Alone, so which is it?

I ran it, and it works on my IDE, it has to be your IDE. I agree with masijade though, you have Applet extended but you've coded for console IO.

Thank you masijade and ragedsparrow , since reading what you have said I have unistalled and reinstalled my IDE and now the program seems to he working. As for the other stuff you have noted, I am just beginning to learn Java and was instructed by my teacher to make a Java applet that is modelled after a quiz show that I made in Turing, and this is what I managed to do. Also thank you for the quick responses, I really appreciate it.

It's all good, glad I could help a little.

Ok, the program now is working for me, but now today i have tried to add in my second question. The applet works but it won't let me input a answer for the second question, it just uses my answer from the first question. How do I fix this?

import java.awt.*;
import java.applet.*;
import java.io.*;

public class Quiz_Show extends Applet
{
    public static void main (String args[])
    {
        InputStreamReader istream = new InputStreamReader (System.in);
        BufferedReader bufRead = new BufferedReader (istream);
        String Answer = "";
        int score = 0;
        System.out.println ("Welcome to my quiz show.");
        System.out.println ("Please enter the letter of the answer for each question and press enter.");
        System.out.println ("Here is the first question.");
        System.out.println ("What is the largest country in the world?");
        System.out.println ("A-Canada");
        System.out.println ("B-Russia");
        System.out.println ("C-USA");
        System.out.println ("D-China");
        while (Answer.equalsIgnoreCase (""))
        {
            try
            {
                System.out.println ("Please answer with a, b, c, or d");
                Answer = bufRead.readLine ();
            }
            catch (IOException err)
            {
                System.out.println ("Error reading line");
            }
        }
        if (Answer.equals ("b"))
        {
            System.out.println ("Good job, that is correct!");
            score = score + 15;
        }
        else
        {
            System.out.println ("Sorry, that answer is incorrect.");
            score = score - 5;
        }
        System.out.println ("Here is the second question.");
        System.out.println ("What is 32+(4x9)?");
        System.out.println ("A-32");
        System.out.println ("B-324");
        System.out.println ("C-36");
        System.out.println ("D-68");
        while (Answer.equalsIgnoreCase (""))
        {
            try
            {
                System.out.println ("Please answer with a, b, c, or d");
                Answer = bufRead.readLine ();
            }
            catch (IOException err)
            {
                System.out.println ("Error reading line");
            }
        }
        if (Answer.equals ("d"))
        {
            System.out.println ("Good job, that is correct!");
            score = score + 15;
        }


        else
        {
            System.out.println ("Sorry, that answer is incorrect.");
            score = score - 5;
        }

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.