Sign in to follow this  
derekpainter1

java code error

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this