Advertisement

Could not find or load main class quiz

Started by February 07, 2016 07:09 PM
3 comments, last by Alberth 8 years, 10 months ago

When i run this code i get this error:

Error: Could not find or load main class quiz

if i delete the "extend" token, the error goes away, but then i cant connect the classes xD i get error that the variable cant be used etc.


import java.util.Scanner;
import java.util.ArrayList;
 
public class extends quiz {
   
public static void main(String []args)
   {
  Scanner user_input = new Scanner( System.in );
  ArrayList<String> list = new ArrayList<String>();
  
  int c = 0;
  
  Question_A.print();  
  
  for(String Alt_A : Alternatives_A);    
  
   String in;
   in = user_input.nextLine();
   
   if (in.equalsIgnoreCase("B")) {
    Answer_Right.print();
    
    c++;
   }
   else {
            Answer_Wrong.print();
   }
   
   Question_B.print();
   
   for(String Alt_B : Alternatives_B);
   
   String in;
   in = user_input.nextLine();
   
   if (in.equalsIgnoreCase("A")) {
    Answer_Right.print();
    
    c++;  
   }
    
   else {
     Answer_Wrong.print();     
}
   Question_C.print();
   
   for(String Alt_C : Alternatives_C);
   
   String in;
   in = user_input.nextLine();
   
   if (in.equalsIgnoreCase("C")); {
    Answer_Right.print();
    
    C++;
        }
   else {                          //why do i get this error here? "Syntax error on token "else", delete this token"
    Answer_Wrong.print();
   }
    
   }
}   
   
class Alternatives {
   
public static void alernatives(String []args) {
   
  ArrayList<String> Alternatives_A = new ArrayList<String>();
 Alternatives_A.add("A, Toronto");
 Alternatives_A.add("B, Otawa");
 Alternatives_A.add("C, vancoover");
 
ArrayList<String> Alternatives_B = new ArrayList<String>();
 Alternatives_B.add("A, 125");
 Alternatives_B.add("b, 15");
 Alternatives_B.add("C, 100");
 
ArrayList<String> Alternatives_C = new ArrayList<String>();
 Alternatives_C.add("A, Insect");
 Alternatives_C.add("B, Fish");
 Alternatives_C.add("C, bird ");
   
   }
}
   
class questions {
   
public static void question(String []args) {
   
    String Answer_Right = "That is right, next question!";  //the value of the local variable Answer_Right is not used
String Answer_Wrong = "That is wrong, next question!";  //the value of the local variable Answer_Wrong is not used
String Question_A = "What is the capital of canada?";   //The value of the local Variable Question_A is not used
String Question_B = "What is 5^3?";                     //I think u got the point ;)
String Question_C = "what is a grouse";
 
   
   }
 
   }

It looks as if you the compiler isnt finding a quiz.java file in the same folder as your main java file

You also have 2 main classes and should only have one

Advertisement

Some comments and clarifications:


public class extends quiz {

You must insert a name between "class" and "extends", eg "public class XXXXX extends quiz { .." (with something better than XXXX)"


for(String Alt_A : Alternatives_A);

Is there something you wanted to do here? Now it says (expanding it a bit to make it more clear)


for (String Alt_A : Alternatives_A) { 
    // Do nothing
}

(The ";" at the end means 'empty statement, just like { } does.) Most likely you wanted to print Alt_A ?


if (in.equalsIgnoreCase("C")); {     <--- Note the ; here
    Answer_Right.print();
    
    C++;   <--- Uppercase C does not exist, you only have lower case c. 'C' and 'c' are different in Java!
        }
   else {                          //why do i get this error here? "Syntax error on token "else", delete this token"
    Answer_Wrong.print();
   }

The ";" after the "if" makes it an empty test (expanding it a bit to make it clear)


if (in.equalsIgnoreCase("C")) {
    // Do nothing
}
// Here the 'if' ends!

// And then your remaining code follows:
{
    Answer_Right.print();
    
    C++;
        }
   else {           <--  As a result of the ;  you have an 'else' without an 'if' before it 
    Answer_Wrong.print();
   }

The compiler gets confused of the 'else' without an 'if' that it belongs to.


public static void alernatives(String []args) {
   
  ArrayList<String> Alternatives_A = new ArrayList<String>();
 Alternatives_A.add("A, Toronto");
 Alternatives_A.add("B, Otawa");
 Alternatives_A.add("C, vancoover");
 
ArrayList<String> Alternatives_B = new ArrayList<String>();
 Alternatives_B.add("A, 125");
 Alternatives_B.add("b, 15");
 Alternatives_B.add("C, 100");
 
ArrayList<String> Alternatives_C = new ArrayList<String>();
 Alternatives_C.add("A, Insect");
 Alternatives_C.add("B, Fish");
 Alternatives_C.add("C, bird ");
   
   }

The "String []args" doesn't do anything and can be left out.

All the definitions here are local to the "alertnatives" functions, and cannot be used outside the function in this way.

There are way to make this work, but for now, I'd suggest you just copy the arrays into the main function so they become available for use.

Some comments and clarifications:


public class extends quiz {

You must insert a name between "class" and "extends", eg "public class XXXXX extends quiz { .." (with something better than XXXX)"


for(String Alt_A : Alternatives_A);

Is there something you wanted to do here? Now it says (expanding it a bit to make it more clear)


for (String Alt_A : Alternatives_A) { 
    // Do nothing
}

(The ";" at the end means 'empty statement, just like { } does.) Most likely you wanted to print Alt_A ?


if (in.equalsIgnoreCase("C")); {     <--- Note the ; here
    Answer_Right.print();
    
    C++;   <--- Uppercase C does not exist, you only have lower case c. 'C' and 'c' are different in Java!
        }
   else {                          //why do i get this error here? "Syntax error on token "else", delete this token"
    Answer_Wrong.print();
   }

The ";" after the "if" makes it an empty test (expanding it a bit to make it clear)


if (in.equalsIgnoreCase("C")) {
    // Do nothing
}
// Here the 'if' ends!

// And then your remaining code follows:
{
    Answer_Right.print();
    
    C++;
        }
   else {           <--  As a result of the ;  you have an 'else' without an 'if' before it 
    Answer_Wrong.print();
   }

The compiler gets confused of the 'else' without an 'if' that it belongs to.


public static void alernatives(String []args) {
   
  ArrayList<String> Alternatives_A = new ArrayList<String>();
 Alternatives_A.add("A, Toronto");
 Alternatives_A.add("B, Otawa");
 Alternatives_A.add("C, vancoover");
 
ArrayList<String> Alternatives_B = new ArrayList<String>();
 Alternatives_B.add("A, 125");
 Alternatives_B.add("b, 15");
 Alternatives_B.add("C, 100");
 
ArrayList<String> Alternatives_C = new ArrayList<String>();
 Alternatives_C.add("A, Insect");
 Alternatives_C.add("B, Fish");
 Alternatives_C.add("C, bird ");
   
   }

The "String []args" doesn't do anything and can be left out.

All the definitions here are local to the "alertnatives" functions, and cannot be used outside the function in this way.

There are way to make this work, but for now, I'd suggest you just copy the arrays into the main function so they become available for use.

i seem to have fixed it. it works now.

import java.util.Scanner;
import java.util.ArrayList;
 
public class quiz {
   
public static void main(String []args)
   {
  Scanner user_input = new Scanner( System.in );
  new ArrayList<String>();
  
  
  ArrayList<String> Alternatives_A = new ArrayList<String>();
 Alternatives_A.add("A, Toronto");
 Alternatives_A.add("B, Otawa");
 Alternatives_A.add("C, vancoover");
 
 
ArrayList<String> Alternatives_B = new ArrayList<String>();
 Alternatives_B.add("A, 125");
 Alternatives_B.add("b, 15");
 Alternatives_B.add("C, 100");
 
 
ArrayList<String> Alternatives_C = new ArrayList<String>();
 Alternatives_C.add("A, Insect");
 Alternatives_C.add("B, Fish");
 Alternatives_C.add("C, bird ");
 
  
 String Answer_Right = "That is right, next question!";  //the value of the local variable Answer_Right is not used
String Answer_Wrong = "That is wrong, next question!";  //the value of the local variable Answer_Wrong is not used
String Question_A = "What is the capital of canada?";   //The value of the local Variable Question_A is not used
String Question_B = "What is 5^3?";                     //I think u got the point ;)
String Question_C = "what is a grouse";
 
  System.out.println(Question_A);  
  
   
   
  System.out.println(Alternatives_A);
  
   String in;
   in = user_input.nextLine();
   
   if (in.equalsIgnoreCase("b")) {
    System.out.print(Answer_Right);
    
    
   }
   else {
            System.out.println(Answer_Wrong);
   }
   
   System.out.println(Question_B);
   
   System.out.println(Alternatives_B);          
 
    
 
   
   
   in = user_input.nextLine();
   
   if (in.equalsIgnoreCase("a")) {
    System.out.println(Answer_Right);  
   }
    
   else {
     System.out.println(Answer_Wrong);     
}
   System.out.println(Question_C);
   
   System.out.println(Alternatives_C);
 
 
   
   
   in = user_input.nextLine();
   
   if (in.equalsIgnoreCase("c")) {
    System.out.println(Answer_Right);
    
    
        }
   else {                          //why do i get this error here? "Syntax error on token "else", delete this token"
    System.out.println(Answer_Wrong);
   }
    
   }
}   
   

Great!

(If you just start typing the next post instead of hitting "reply", you don't get the entire previous post in your post.)

There are quite a few things you can do now.

First of all, I would suggest you reformat your code. Currently, some lines start at the left margin, while the line below is indented by some amount. Try to bring a system into that.

Many people indent lines in multiple of 4. You start at the left margin. Each time you start a new block, the next line indents 4 more spaces. Each time you end a block, the next line decreases 4 spaces indent.

The result is that most lines start vertically at the same offset, unless you start or end new blocks (often also indicated with { and }, but not always).

Similarly, decide how to write each type of name. As you can see, Java itself uses camelcase (lower case name, with uppercase letter for a new word), like "ArrayList" for classes. If you follow that idea, your "quiz" class should be named "Quiz" instead. You currently use names like "Alternatives_C" for a variable. As you can see, it looks very similar to a Java class name. If you use lowercase and underscore characters only, like "alternatives_c", it becomes more clear that it's a variable rather than a class name.

It may seem silly to you to spend time on this, and at the current stage, you are right. The reason why I bring it up (and also why everybody does this) is to increase clarity. We are very visually oriented, so if you write code that contains visual clues what things are, you get a lot more information just by glancing over the code, without even carefully studying it. This enhanced clarity gives more room to focus on the problem you are trying to solve, and allows you to work faster with less errors.

For example, using the above convention, I can see that 'Quiz' is a class, and 'quiz' is a variable at one glance at any point in the code.

If you learn yourself to use a convention like the above from the start, it becomes a habit, something you do without it taking extra time or effort. You shift one gear up in productivity without cost other than picking the right case convention for naming things.

Secondly, if you look at the information of a single quiz question, you will notice it is all scattered through the program. For example question a:


ArrayList<String> Alternatives_A = new ArrayList<String>();
 Alternatives_A.add("A, Toronto");
 Alternatives_A.add("B, Otawa");
 Alternatives_A.add("C, vancoover");

String Question_A = "What is the capital of canada?";

if (in.equalsIgnoreCase("b")) {

Wouldn't it be nice if you could keep this data all together? Well, you can, with a new class QuizQuestion.java:


import java.util.ArrayList;

public class QuizQuestion {
    /** The question to ask. */
    public String question;

    /** Possible answers. */
    public ArrayList<String> answers = new ArrayList<String>();

    /** The correct answer. */
    public String good_answer;

    /**
     * Append another answer to the list of answers.
     *
     * @param answer New answer to add.
     */
    public void addAnswer(String answer) {
        answers.add(answer);
    }
}

(have a look at how I format the code, how does that look to you?)

Ignore the "addAnswer" method for now, I'll come to that later. So here is a new class, and it has a 'question', a 'answers' array, and a 'good_answer'. All the things you have scattered in your program now. (No shame in that, I often also start trying a random piece of code, seeing how things work, before deciding how to organize things.)

In your main program, instead of making all the various variables, you now fill a QuizQuestion:


        QuizQuestion question_a = new QuizQuestion();
        question_a.question = "What is the capital of canada?";
        question_a.answers.add("A, Toronto");
        question_a.answers.add("B, Otawa");
        question_a.answers.add("C, vancoover");
        question_a.good_answer = "b";

and then you can use that information as follows:


        System.out.println(question_a.question); // Print the question
        for (String answer: question_a.answers) {
            System.out.println(answer); // Print each answer (I don't think your solution actually did the right thing?)
        }

        in = user_input.nextLine();
        if (in.equalsIgnoreCase(question_a.good_answer)) { // Compare the answer with the good_answer
            System.out.print(Answer_Right);
        } else {
            System.out.println(Answer_Wrong);
        }

When filling the answers, I didn't like ".answers.add" very much, it's too much detail. Instead, I wanted the QuizQuestion itself to handle that.

For this reason I wrote the "addAnswer". It takes a new answer, and then adds it to the answers list, just like "question_a.answers.add()" does. However, now I can write


        QuizQuestion question_b = new QuizQuestion();
        question_b.question = "What is 5^3?";
        question_b.addAnswer("A, 125");
        question_b.addAnswer("b, 15");
        question_b.addAnswer("C, 100");
        question_b.good_answer = "a";

Et voila, now I have "question.addAnswer", the question itself handles storing the answer now. It looks nicer now.

Try to make it work (keep a copy of your original code, so you can fallback to the previous version if you need to), perhaps there are other things you can change as well?

This topic is closed to new replies.

Advertisement