0

Why won't this compile correctly? I added the main method line in an attempt to test it, but I get a lot of errors, saying the private constructors are illegal starts of expressions, as well as the public ones, in addition to saying they aren't statements. It's also asking me to add semicolons in places I didn't think were necessary due to them being the beginning of a method. I don't expect anyone to retype the code for me but can someone at least point me in the right direction and tell me where I'm going wrong?

import java.util.Arrays 
public class Book{
    public static void main (String[] args) {



    private String title;           
    private String authors[];       


    public Book() {
        title = "Test";     
        authors = null;             
    }

    public Book(String title, String[] authors) {   
        this.title = title;
        this.authors = authors;     
    }
    public String getterTitle() {       
        return title;
    }
    public void setterTitle(String title) { 
        this.title = title;
    }
    public String[] getterAuthors() {       
        return authors; 
    }
    public void setterAuthors(String[] authors) {       
        this.authors = authors;
    }
    public String bookToString() {
        return "" + getterTitle() + " by " + getterAuthors() + "";
    }
}
Serge Breusov
  • 1,316
  • 4
  • 11
  • 24
M. G
  • 5
  • 1
  • 4
  • Possible duplicate of [Java Error: illegal start of expression](https://stackoverflow.com/questions/24562950/java-error-illegal-start-of-expression) – Makoto Jun 28 '18 at 03:14
  • 1
    You seem to be missing a semicolon at the end of your import line. – Powerlord Jun 28 '18 at 03:16
  • Just to clarify some terminology, you have private **member variables**, also called fields. These are not constructors. The constructors are `public Book()...` and `public Book(String title, String[] authors)...`. You can spot these because they have the same name as the class. – Code-Apprentice Jun 28 '18 at 03:25
  • Note that "illegal start of expressions" means that the error is on some line **before** the one indicated by the error. – Code-Apprentice Jun 28 '18 at 03:26
  • 1
    In this case, a closing brace `}` for `main()` will fix the problem as stated in one of the answers. – Code-Apprentice Jun 28 '18 at 03:44

3 Answers3

2

You're missing semicolon after java.util.Arrays and your main has no closing } brace:

import java.util.Arrays;

public class Book {
    private String title;           
    private String authors[];

    public Book(String title, String[] authors) {   
        this.title = title;
        this.authors = authors;     
    }

    public String getterTitle() {       
        return title;
    }

    public void setterTitle(String title) { 
        this.title = title;
    }

    public String[] getterAuthors() {       
        return authors; 
    }

    public void setterAuthors(String[] authors) {       
        this.authors = authors;
    }

    public String bookToString() {
        return "" + getterTitle() + " by " + getterAuthors() + "";
    }

    public static void main (String[] args) {
         // Do something here
    }
}
dmitryro
  • 3,463
  • 2
  • 20
  • 28
1

I think I know what do you mean.

So you want to make 2 classes.

this one is main.

TestBook.java

    package testbook;

    public class TestBook {

        public static void main(String[] args) {
            // TODO code application logic here
            Book bookClass = new Book();

        }
    }

This one is classes.

Book.java

package testbook;

public class Book {

    private String title;
    private String authors[];

    public Book() {
        title = "Test";
        authors = null;
    }

    public Book(String title, String[] authors) {
        this.title = title;
        this.authors = authors;
    }

    public String getterTitle() {
        return title;
    }

    public void setterTitle(String title) {
        this.title = title;
    }

    public String[] getterAuthors() {
        return authors;
    }

    public void setterAuthors(String[] authors) {
        this.authors = authors;
    }

    public String bookToString() {
        return "" + getterTitle() + " by " + getterAuthors() + "";
    }
}
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
0
import java.util.Arrays; // the semi - colon is missing on this line  
public class Book{
private String title;         
private String authors[]; // to make these variables global you 
                         // need to declare them outside of a method  

public static void main (String[] args) {   
 } // you need to close the method before beginning with others

Correcting the above should fix the code for you.

Nikhil Jain
  • 586
  • 6
  • 21