Can someone help me fix this? After I compile I get ilegal start of expression for the line that says public String getBook(). I cannot see why this is not allowed.

My output should be as follows:

Jane Doe
Long night
12.5

Long Night by Jane Doe

I am supposed to use a toString constructor for this program to produce the above output. My program is as follows:

public class Book{

private String author, title;
private double price;

public Book (String abook){
book = abook;
title = title;
price = 0.0;

public String getBook (){
return book;
}

public String getAuthor(){
return author;
}
public String getTitle(){
return title;
}
public double getPrice (){
return price;
}
public void setBook(String newbook){
book = new book();
}
public String toString( ){
return title + "" + author;
}

Book abook = new Book ("Jane Doe", "Long Night", "12.50");
System.out.println(abook.getAuthor());
System.out.println(abook.getTitle());
System.out.println(abook.getPrice());
System.out.println(abook);


}
}

Thanks for your help.

Member Avatar for iamthwee
public class Book
{

   private String author, title;
   private double price;

   public Book ( String abook )
   {
      book = abook;
      title = title;
      price = 0.0;

      public String getBook ()
      {
         return book;
      }

      public String getAuthor()
      {
         return author;
      }
      public String getTitle()
      {
         return title;
      }
      public double getPrice ()
      {
         return price;
      }
      public void setBook ( String newbook )
      {
         book = new book();
      }
      public String toString( )
      {
         return title + "" + author;
      }

      Book abook = new Book ( "Jane Doe", "Long Night", "12.50" );
      System.out.println ( abook.getAuthor() );
      System.out.println ( abook.getTitle() );
      System.out.println ( abook.getPrice() );
      System.out.println ( abook );


   }
}

I don't have my java compiler but could you possibly be missing a constructor for:-

Book ( "Jane Doe", "Long Night", "12.50" );

Yeh you are probably right but where do I put this constructor? I am sorry I have looked at java codes all day so, right now they all look the same.

My idea is to enter it where public void setBook (String newbook)
is, would it work?

Thanks.

Member Avatar for iamthwee

mm, obviously you have three variables here

Book ( "Jane Doe", "Long Night", "12.50" );

so your constructor should have three variables.

>My idea is to enter it where public void setBook (String newbook)

Try it and see.

I tried it and it works but I get an error somewhere else now that public String getBook (), is an illegal start of expression. No clue what's goin on.

Thanks for your help so far though.

mm, obviously you have three variables here

Book ( "Jane Doe", "Long Night", "12.50" );

so your constructor should have three variables.

>My idea is to enter it where public void setBook (String newbook)

Try it and see.

Member Avatar for iamthwee

Can you post the new updated code with all your compiler errors please.

Member Avatar for iamthwee
private String author, title;
   private double price;

It looks like you haven't declare book anywhere.

public class Book{

private String author, title;
private double price;

public Book (String abook){
book = abook;
title = title;
price = 0.0;

/* My error happens here, illegal start of expression and I don't think it is

public String getBook (){
return book;
}

public String getAuthor(){
return author;
}
public String getTitle(){
return title;
}
public double getPrice (){
return price;
}

public void setBook(String author, String title, double price){
author = "Jane Doe"
title = "Long Night"
price = "12.50"
}
public String toString( ){
return title + "" + author;
}

Book abook = new Book ("Jane Doe", "Long Night", "12.50");
System.out.println(abook.getAuthor());
System.out.println(abook.getTitle());
System.out.println(abook.getPrice());
System.out.println(abook);


}
}

Thanks again, I will keep trying.

Member Avatar for iamthwee

hmm, some parts you are off by a long shot.

private String author, title;
private double price;

public Book (String abook){
book = abook;
title = title;
price = 0.0;

Here for example, you have declared book = abook; but above it, you don't have private String book . In any case, do you really need it?

public void setBook(String author, String title, double price){
author = "Jane Doe"
title = "Long Night"
price = "12.50"
}

Ok, I assume that was the constructor we were talking about. It's wrong it should be something like:-

public Book(String a, String t, double p){
author = a;
title = t;
price = p;
}

Also this:-

Book abook = new Book ("Jane Doe", "Long Night", "12.50");

Should be:-

Book abook = new Book ("Jane Doe", "Long Night", 12.50);

Because 12.50 is a double and NOT a String.

Another thing you are missing is a driver program i.e

public static void main(String[] args)

That's quite a lot of mistakes. Maybe you need to study your notes/book again.

Cheers

Thank you very much. It is working now after several attempts, I am new and just getting to know the java language.

Again, thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.