6 Replies - 355 Views - Last Post: 04 February 2012 - 07:44 PM Rate Topic: -----

Topic Sponsor:

#1 marcam129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-December 11

Where is the symbol?

Posted 03 February 2012 - 11:17 PM

Hi, I am new to Java, and I'm getting some errors.

The problem is:
a. Create a class named LibraryBook that contains fields to hold methods for setting and getting a LibraryBook's title, author, and page count. (I think I managed this one okay, but I will post it if needed for the second part.)

b. Write an application that instantiates five LibraryBook objects and prompts the user for values for the data fields. Then prompt the user to enter which field the LibraryBooks should be sorted by-title, author, or page count. Perform the requested sort procedure and display LibraryBook objects.

a.
import java.util.*;
import javax.swing.*;
   
public class LibraryBook
{
   private String title;
   private String author;
   private int pageCount;
   
	public LibraryBook(String bookTitle, String bookAuthor, int bookPageCount)
	{
		title = bookTitle;
		author = bookAuthor;
		pageCount = bookPageCount;
 	}
  
	public String getTitle()
	{
		return title;
	}

	public String getAuthor()
  	{
		return author;
  	}
  
	public int getPageCount()
	{
		return pageCount;
  	}
	
	public void setTitle(String bookTitle)
	{
		title = bookTitle;
	}
	
	public void setAuthor(String bookAuthor)
	{
		author = bookAuthor;
	}
	
	public void setPageCount(int bookPageCount)
	{
		pageCount = bookPageCount;
	}
 
   
	    
}



b.
public class LibraryBookSort
  
{
  
	public static void main(String[] args)
  
  {
  
     LibraryBook[] myBook = new LibraryBook[5];
  
      
  
     myBook[0] = new LibraryBook("Java Programming", "Joyce Farrell", 881);
  
     myBook[1] = new LibraryBook("A Book", "E Author", 345);
  
     myBook[2] = new LibraryBook("B Book", "F Author", 450);
 
     myBook[3] = new LibraryBook("C Book", "G Author", 650);
 
     myBook[4] = new LibraryBook("D Book", "H Author", 750);
 
      
 
     for (int x = 0; x < myBook.length; ++x)
 
     myBook[x].setTitle("this is the new title");
 
     for (int x = 0; x < myBook.length; ++x)
 
     System.out.println(myBook[x].getTitle());
 
   }
 
      
  

	public static void display(String[] title)
 
   {
 
     for (int x=0; x < title.length; ++x)
 
     {
     		System.out.println(myBook[x].getTitle() + " ");
     }
 
     System.out.println();
 
   }
  
   public static void titleSort(String[] title)
 
   {
 
     for (int x=0; x < title.length - 1; ++x)
 
     	for (int y=0; y < title.length - 1; ++y)
 
     {
 
     		if (title[y].compareTo(title[y+1]) > 0)
 
     		{
     			String temp = title[y];
     			title[y] = title[y+1];
     			title[y+1] = temp;
     		}
      }
 	}
 }




This is the error that I keep on getting
LibraryBookSort.java:45: error: cannot find symbol
     		System.out.println(myBook[x].getTitle() + " ");
     		                   ^
  symbol:   variable myBook
  location: class LibraryBookSort
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


Is This A Good Question/Topic? 0
  • +

Replies To: Where is the symbol?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Where is the symbol?

Posted 03 February 2012 - 11:41 PM

MyBook is declared in main(0 method so it can only be seen in it, that is its scope. display() method has no variable called myBook, that is what it is complaining. You may pass is as a parameter if you need to use it there
Was This Post Helpful? 1
  • +
  • -

#3 marcam129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-December 11

Re: Where is the symbol?

Posted 04 February 2012 - 01:58 PM

View Postsmohd, on 03 February 2012 - 11:41 PM, said:

MyBook is declared in main(0 method so it can only be seen in it, that is its scope. display() method has no variable called myBook, that is what it is complaining. You may pass is as a parameter if you need to use it there


I am sorry I am very new to Java and I am not yet familiar with all the terms yet. So this kind of made no sense to me. Can you explain in a simpler way?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,895
  • Joined: 27-December 08

Re: Where is the symbol?

Posted 04 February 2012 - 02:03 PM

You declare the variable myBook in the main() method. That means it can only be seen within the main() method. That means myBook is a local variable. The display() method is not (and cannot be) declared in the main() method, and therefore cannot access variables local to the main() method.

You must either:
-Declare myBook as a global (static) variable outside of the main() method
public class LibraryBookSort{

   static LibraryBook[] myBooks;

   public static void main(String[] args){
        //use myBooks
   }

   public static void display(String[] title){
        //use myBooks
   }
}



-Or define display() to accept a LibraryBook[] and pass your array to it
public class LibraryBookSort{

   public static void main(String[] args){
        LibraryBook[] myBooks = new LibraryBook[10];
        
        ..other code..
        display(titleArray, myBooks);
   }

   public static void display(String[] title, LibraryBook[] books){
        //use books
   }
}


Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: Where is the symbol?

Posted 04 February 2012 - 02:09 PM

You should understand visibility and scope early in the use of a strictly typed language like Java. Here are some things to read on the topics:

Scope.

Visibility rules.

I expect you'll have more questions, but if you read those articles and anything you can find in your textbook on the topic before you come back, we'll be able to continue the discussion using terms with which you have a passing familiarity.
Was This Post Helpful? 1
  • +
  • -

#6 marcam129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-December 11

Re: Where is the symbol?

Posted 04 February 2012 - 03:39 PM

View Postmacosxnerd101, on 04 February 2012 - 02:03 PM, said:

You declare the variable myBook in the main() method. That means it can only be seen within the main() method. That means myBook is a local variable. The display() method is not (and cannot be) declared in the main() method, and therefore cannot access variables local to the main() method.

You must either:
-Declare myBook as a global (static) variable outside of the main() method
public class LibraryBookSort{

   static LibraryBook[] myBooks;

   public static void main(String[] args){
        //use myBooks
   }

   public static void display(String[] title){
        //use myBooks
   }
}



-Or define display() to accept a LibraryBook[] and pass your array to it
public class LibraryBookSort{

   public static void main(String[] args){
        LibraryBook[] myBooks = new LibraryBook[10];
        
        ..other code..
        display(titleArray, myBooks);
   }

   public static void display(String[] title, LibraryBook[] books){
        //use books
   }
}


Ok I used your first example, however I am getting the wrong output now. It wont display the book tiles etc.

Here is my updated code

public class LibraryBookSort
  
{
  static LibraryBook[] myBook;
	public static void main(String[] args)
  
  {
  
     LibraryBook[] myBook = new LibraryBook[5];
  
      
  
     myBook[0] = new LibraryBook("Java Programming", "Joyce Farrell", 881);
  
     myBook[1] = new LibraryBook("A Book", "E Author", 345);
  
     myBook[2] = new LibraryBook("B Book", "F Author", 450);
 
     myBook[3] = new LibraryBook("C Book", "G Author", 650);
 
     myBook[4] = new LibraryBook("D Book", "H Author", 750);
 
      
 
     for (int x = 0; x < myBook.length; ++x)
 
     myBook[x].setTitle("this is the new title");
 
     for (int x = 0; x < myBook.length; ++x)
 
     System.out.println(myBook[x].getTitle());
 
   }
 
      
  

	public static void display(String[] title)
 
   {
 
     for (int x=0; x < title.length; ++x)
 
     {
     		System.out.println(myBook[x].getTitle() + " ");
     }
 
     System.out.println();
 
   }
  
   public static void titleSort(String[] title)
 
   {
 
     for (int x=0; x < title.length - 1; ++x)
 
     	for (int y=0; y < title.length - 1; ++y)
 
     {
 
     		if (title[y].compareTo(title[y+1]) > 0)
 
     		{
     			String temp = title[y];
     			title[y] = title[y+1];
     			title[y+1] = temp;
     		}
      }
 	}
 }



Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,895
  • Joined: 27-December 08

Re: Where is the symbol?

Posted 04 February 2012 - 07:44 PM

You re-declare myBook inside your main() method. Don't do this. Just use the myBook variable you declared as static. Remove the LibraryBook[] type from the one in the main() method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1