By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

Object and Classes error: cannot find symbol

P: 1
Hi guys,
I'm working on my java assignment but I don't know what did I do wrong. I successfully compile the file Book.java but got the error cannot find symbol while compiling BookTest.java . Thank you very much for any help.

Here's my Book.java

Expand|Select|Wrap|Line Numbers
  1. package question_1;
  2.     public class Book {
  3.     private String title; // Title of the book
  4.     private String author; // Author of book :
  5.     private String isbn; // International Standard Book Number 0-13-213198-7
  6.     private float cost; // Cost of the book in U.S. dollars
  7.     public Book() {
  8.     title = "UnKnown";
  9.     author = "UnKnown";
  10.     isbn = "UnKnown";
  11.     cost = 0;
  12.     }
  13.     public Book(String _title, String _author, String _isbn, float _cost) {
  14.     title = _title;
  15.     author = _author;
  16.     isbn = _isbn;
  17.     cost = _cost;
  18.     }
  19.     public String getTitle() {
  20.     return title;
  21.     }
  22.     public String getAuthor() {
  23.     return author;
  24.     }
  25.     public String getIsbn() {
  26.     return isbn;
  27.     }
  28.     public float getCost() {
  29.     return cost;
  30.     }
  31.     public void setTitle(String _title) {
  32.     title = _title;
  33.     }
  34.     public void setAuthor(String _author) {
  35.     author = _author;
  36.     }
  37.     public void setIsbn(String _isbn) {
  38.     isbn = _isbn;
  39.     }
  40.     public void setCost(float _cost) {
  41.     cost = _cost;
  42.     }
  43.     public double convertUsToEuro() {
  44.     double euroconverted = 0;
  45.     euroconverted = .73 * cost;
  46.     return euroconverted;
  47.     }
  48.     }
And here's my BookTest.java:

Expand|Select|Wrap|Line Numbers
  1. package question_1;
  2.  
  3.     import javax.swing.JFrame;
  4.     import javax.swing.JOptionPane;
  5.     import java.*;
  6.     public class BookTest {
  7.     public void test() {
  8.     System.out.println("Begin test");
  9.     Book book1 = new Book("Programing Java", "Sun System", "001", 86);
  10.     Book book2 = new Book("Programing C#", "MS", "002", 89);
  11.     /// print output for user
  12.     JOptionPane.showMessageDialog(new JFrame(), "Book 1nTitle: " + book1.getTitle() + "nAuthor: " + book1.getAuthor() + "nISBN: " + book1.getIsbn() + "nCost in USD: " + book1.getCost() + "nCost in Euro: " + book1.convertUsToEuro());
  13.     JOptionPane.showMessageDialog(new JFrame(), "Book 2nTitle: " + book2.getTitle() + "nAuthor: " + book2.getAuthor() + "nISBN: " + book2.getIsbn() + "nCost in USD: " + book2.getCost() + "nCost in Euro: " + book2.convertUsToEuro());
  14.     }
  15.     public static void main(String[] args) {
  16.     BookTest obj = new BookTest();
  17.     obj.test();
  18.     }
  19.     }
Errors:

Expand|Select|Wrap|Line Numbers
  1.  BookTest.java:14: cannot find symbol
  2.     symbol : class Book
  3.     location: class question_1.BookTest
  4.     Book book1 = new Book("Programing Java", "Sun System", "001", 86);
  5.     ^
  6.     BookTest.java:14: cannot find symbol
  7.     symbol : class Book
  8.     location: class question_1.BookTest
  9.     Book book1 = new Book("Programing Java", "Sun System", "001", 86);
  10.     ^
  11.     BookTest.java:15: cannot find symbol
  12.     symbol : class Book
  13.     location: class question_1.BookTest
  14.     Book book2 = new Book("Programing C#", "MS", "002", 89);
  15.     ^
  16.     BookTest.java:15: cannot find symbol
  17.     symbol : class Book
  18.     location: class question_1.BookTest
  19.     Book book2 = new Book("Programing C#", "MS", "002", 89);
  20.     ^
  21.     4 errors
  22.  
Thank you very much for any help.
Mar 28 '12 #1
Share this Question
Share on Google+
3 Replies


P: 21
hi Darkseeker,

Just remove your package name from both the classes, then it will work fine... :-)
Mar 29 '12 #2

10K+
P: 13,264
Not really, putting classes in the default package is discouraged. The OP should rather move the files into a folder structure that matches their package declarations.
Mar 30 '12 #3

P: 79
try importing the book class in the booktest.java file
Expand|Select|Wrap|Line Numbers
  1. import question_1.book;
Apr 2 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.