Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main noDefClassStatement found

 
Lynn Cross
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why this is not working. I've busted my little brain for days. Very new at Java, please help. This is the exact error message.

Exception in thread "main" java.lang.NoClassDefError:
import java.io.*;
public class Quiz
{
public static void main(String args[])
{
int correctCounter;
InputStreamReader reader;
BufferedReader buffer; keyboard = new
BufferedReader (new InputStreamReader (System.in));
}
String seekResponse;
System.out.println("Welcome to Hoffman School .. ");
{
// choose the subject
System.out.println("enter h for humanities, e for English or x for exit");
seekResponse = keyboard.readline();
if (seekResponse.equals ("h"); //do humanities quiz
{System.out.println("The Declaration of Independence is the same as the
constitution");
seekResponse = keyboard.readline();
if (seekResponse=("t"));
System.out.println("incorrect answer");
if (seekResponse.equals ("f"));
System.out.println("correct");
System.out.println("At the Boston Tea Party the people threw coffee overboard");
if (seekResponse.equals ("t"));
System.out.println("incorrect answer");
If (seekResponse.equals ("f"));
System.out.println("correct");
}
else if (seekResponse.equals ("e"); //do English quiz

{System.out.println("A period goes at the end of a question");
seekResponse = keyboard.readline();
if (seekResponse.equals ("t"));
System.out.println("incorrect answerz");
if (seekResponse.equals ("f"));
System.out.println("correct");
System.out.println("Most sentences begin with a capital letter");
if (seekResponse.equals ("t"));
System.out.println("incorrect answer");
If (seekResponse.equals ("f"));
System.out.println("correct");
}
else if (seekResponse.equals ("x"));
exit// do exit logic
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lynn,

Welcome to JavaRanch!

If you're typing



then yes, I'd expect you to see a message like



because the class file Quiz.class -- the executable version of the code -- won't have been created. There are several syntax errors in the above, and you have to fix them, such that the "javac Quiz.java" command reports no errors, before trying to run the program.

To reiterate: NoClassDefFoundError means "No class def(initition) found."

If the above is the exact code you're trying to compile, then the first error is that the routine main() ends abruptly with what is probably just a stray closing curly brace at line 10. After that, at line 30, there's an extra semicolon, and missing close-parenthesis. The next few "if" statements are also followed by that extra semicolon, but the compiler actually won't report it -- the code is syntactically correct, but it just won't do what you intend.

Here's my advice -- feel free to take it or leave it: slow down. Get a "Hello, World" program to work, first. Then try a very slightly longer program with a single "if" statement. Then a longer one with several "if" statements. Get each program to work and make sure you understand it. Then you can work your way up to something of this size. Even when you do, try to write it a little piece at a time: write some, compile it, test it. Then add more, compile it, test it. Then add more. Make sure you understand what's happening at every step.
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have a compiled class to run, and the class you posted here isn't going to compile the way it is.

  • Your main method ends too early, and the rest of your code ends up outside of it.
  • You capitalized the "i" in "if" twice.
  • Always compare Strings with the equals method, not = or ==.
  • Semicolons everywhere
  • This really needs to be written as a loop.


  • [ August 03, 2004: Message edited by: Darin Niard ]
     
    Lynn Cross
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks so much for the advice. I will heed it.
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Note that to run the Quiz program defined in the source file Quiz.java, the command is java Quiz not java Quiz.java. (I'm thinking that Ernest just made a small boo-boo above.)
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic