1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Java program won't compile

  1. Mar 10, 2008 #1
    Supposed to make a program that displays an entered name and date, etc, but it won't run.

    Code (Text):
    import java.util.*;
    import java.text.*;
    import javax.swing.*;

    public class ch2NameAndDate {

        public static void main(String[] args) {
            // Input the first, middle, and last name
            // Display the name and the date.
            String firstNameString, middleNameString, lastNameString;
            //Create a Scanner object
            Scanner inputScanner = new Scanner(System.in);
            //Objects needed for date.
            Date todayDate = new Date();
            SimpleDateFormat formattingSimpleDateFormat=
                new SimpleDateFormat("MMMM dd yyyy");
            //Input the data
            System.out.print("enter your First name  ");
            firstNameString = inputScanner.next();
            //firstNameString = JOptionPane.showInputDialog("Enter your First Name");
            //middleNameString enter middle name.
            System.out.print("enter your Middle name  ");
            middleNameString = inputScanner.next();

            //lastNameString = JOptionPane.showInputDialog("enter your Last name");
            System.out.print("Enter your Last name ");
            lastNameString = inputScanner.next();
            //Print out first, middle, and last name with date.  
            System.out.println("Hello" + lastNameString + "," + " " + firstNameString + " " + str.substring(0); + "." + "today is " +

    I tried running it and eclipse said:
    Code (Text):
    java.lang.NoClassDefFoundError: NameAndDate
    Exception in thread "main"
    what's wrong? I thought I declared the class in the code.
  2. jcsd
  3. Mar 10, 2008 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Let me guess -- you saved the file as NameAndDate.java? If so, then it expects to find a class named NameAndDate in the file... and that's not the name of the class you defined.
  4. Mar 10, 2008 #3
    Ahh, thanks. That fixed the problem.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook