java code error

Started by
7 comments, last by smc 17 years, 6 months ago
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);
   }
    	
}



Advertisement
Use the '+' operator to concatinate strings. Ie.:

System.out.println("Welcome to the grade book for " + courseName);
this is java
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
∫Mc
Does the import statement have to be at the very beginning of the file?

Edit: bugger, replies in this forum are fast...
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.
nevermind, i found whats wrong. i have to make 2 files. one is gradebook.java, and the other is Gradebooktest.java.
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.

∫Mc
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);   }    	}
∫Mc

This topic is closed to new replies.

Advertisement