Could not find or load main class quiz

This topic is 675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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")) {

c++;
}
else {
}

Question_B.print();

for(String Alt_B : Alternatives_B);

String in;
in = user_input.nextLine();

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

c++;
}

else {
}
Question_C.print();

for(String Alt_C : Alternatives_C);

String in;
in = user_input.nextLine();

if (in.equalsIgnoreCase("C")); {

C++;
}
else {                          //why do i get this error here? "Syntax error on token "else", delete this token"
}

}
}

class Alternatives {

public static void alernatives(String []args) {

ArrayList<String> Alternatives_A = new ArrayList<String>();

ArrayList<String> Alternatives_B = new ArrayList<String>();

ArrayList<String> Alternatives_C = new ArrayList<String>();

}
}

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";

}

}


Share on other sites

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

Edited by Gold and Black Games

Share on other sites

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

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"
}


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:
{

C++;
}
else {           <--  As a result of the ;  you have an 'else' without an 'if' before it
}

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>();

ArrayList<String> Alternatives_B = new ArrayList<String>();

ArrayList<String> Alternatives_C = new ArrayList<String>();

}


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.

Edited by Alberth

Share on other sites

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

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"
}


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:
{

C++;
}
else {           <--  As a result of the ;  you have an 'else' without an 'if' before it
}

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>();

ArrayList<String> Alternatives_B = new ArrayList<String>();

ArrayList<String> Alternatives_C = new ArrayList<String>();

}


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>();

ArrayList<String> Alternatives_B = new ArrayList<String>();

ArrayList<String> Alternatives_C = new ArrayList<String>();

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")) {

}
else {
}

System.out.println(Question_B);

System.out.println(Alternatives_B);

in = user_input.nextLine();

if (in.equalsIgnoreCase("a")) {
}

else {
}
System.out.println(Question_C);

System.out.println(Alternatives_C);

in = user_input.nextLine();

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

}
else {                          //why do i get this error here? "Syntax error on token "else", delete this token"
}

}
}



Share on other sites

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("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;

public ArrayList<String> answers = new ArrayList<String>();

/**
*
*/
}
}

(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.good_answer = "b";

and then you can use that information as follows:

        System.out.println(question_a.question); // Print the question
}

in = user_input.nextLine();
} else {
}

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.

        QuizQuestion question_b = new QuizQuestion();
question_b.question = "What is 5^3?";
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?

Share on other sites

This topic is 675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628686
• Total Posts
2984237

• 16
• 13
• 13
• 10
• 10