Jump to content
  • Advertisement
Sign in to follow this  
derekpainter1

java code error

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, im getting a 'class or interface expected' error. here is the code. any help appreciated.


public class GradeBook
{
	
	public void displayMessage(String courseName)
	{
		
		System.out.printf("Welcome to the grade book for \n %s \n",courseName);
	}
	
}


import java.util.Scanner;

public class ninth
{
	
	public static void main(String args[])
	
	{

  
    Scanner input = new Scanner(System.in);
    
    GradeBook myGradeBook = new GradeBook();
    
    System.out.println(" Please enter the course name:");
    String nameOfCourse = input.nextLine();
    System.out.println();
    
    myGradeBook.displayMessage(nameOfCourse);
   }
    	
}



Share this post


Link to post
Share on other sites
Advertisement
First... decare the import at the top of the file.

Second... it will still fail to compile. I can help no further unless you can convince me this is not homework

Share this post


Link to post
Share on other sites
this is not homework, i got a deitel an deitel java book. anyway, i better ask this in the java forum. i typed the code exactly as it is in the book and i get that error.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mashiniblik
Does the import statement have to be at the very beginning of the file?


Imports should come before class definitions. Otherwize you get the error the OP is talking about. They do not have to be the top line however.

Share this post


Link to post
Share on other sites
Quote:
Original post by derekpainter1
nevermind, i found whats wrong. i have to make 2 files. one is gradebook.java, and the other is Gradebooktest.java.


Now it will compile.

You can also do this...

ninth.java


import java.util.Scanner;
public class ninth
{

private class GradeBook
{

public void displayMessage(String courseName)
{

System.out.printf("Welcome to the grade book for \n %s \n",courseName);
}

}



public static void main(String args[])
{


Scanner input = new Scanner(System.in);

GradeBook myGradeBook = new GradeBook();

System.out.println(" Please enter the course name:");
String nameOfCourse = input.nextLine();
System.out.println();

myGradeBook.displayMessage(nameOfCourse);
}

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!