Two problems with two different classes.

im having two problems: i get nothing with the printConferences() method inside ReferenceBook class, the arraylist size is equal to zero. and my printSchoolsAndCopies() and schoolsAndCopiesToArray() methods which are inside TextBook class, give exceptions:

Exception in thread "main" java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.ArrayList.toArray(ArrayList.java:304) at project3.TextBook.schoolsAndCopiesToArray(TextBook.java:64) at project3.TextBook.printSchoolsAndCopies(TextBook.java:74) at project3.BookTester.main(BookTester.java:27) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

all you need to read is TextBook and ReferenceBook class, but i added the tester and BookCatalog, which is an arraylist of books.

here are my TextBook and ReferenceBook classes and the tester, there are two abstract classes Book and TechnicalBook, but i wont post them unless anybody needs them, i presume not.

/*

* TextBook.java

*

* Created on April 19, 2007, 8:02 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package project3;

import java.util.*;

/**

*

* @author Kevin

*/

publicclass TextBookextends TechnicalBook{

private ArrayList <String> schools;

private ArrayList <Integer> numberCopiesForSchool;

private String [][] schoolsAndCopies;

publicstaticfinal String TEXTBOOK ="Textbook";

/** Creates a new instance of TextBook */

public TextBook(String author, String title,int numberPages,int copiesSold,

double price){

super(author, title, numberPages, copiesSold, price);

schools =new ArrayList();

numberCopiesForSchool =new ArrayList();

}

publicint getNumberCopiesForSchool(String school){

Integer temp =null;

for(int i = 0; i < schools.size();i++){

if(schools.get(i).equals(school))

temp = numberCopiesForSchool.get(i);

}

return temp;

}

publicvoid addSchool(String school){

Integer one =new Integer(1);

schools.add(school);

// number of copies for the school is initially 1

numberCopiesForSchool.add(one);

}

publicint getNumberSchools(){

int count = 0;

for(int i = 0; i < schools.size(); i++){

if(schools.get(i) !=null)

count++;

}

return count;

}

publicvoid addCopiesForSchool(String school,int copies){

for (int i = 0; i < schools.size(); i++){

if (schools.get(i).equals(school))

numberCopiesForSchool.set(i, numberCopiesForSchool.get(i)

+ (Integer) copies);

}

}

privatevoid schoolsAndCopiesToArray(ArrayList <String> a, ArrayList <Integer> b){

String [] c =null;

String [] d =null;

c = (String[]) a.toArray(new String[a.size()]);

d = (String[]) b.toArray(new String[b.size()]);

String [] [] schoolsAndCopies =null;

for(int i = 0; i < c.length;i++){

for(int j = 0; j < d.length;j++){

schoolsAndCopies[i] = c;

schoolsAndCopies[j] = d;

}

}

}

publicvoid printSchoolsAndCopies(String title){

schoolsAndCopiesToArray(schools, numberCopiesForSchool);

System.out.println("");

System.out.println("Schools and copies sold to each school for " + this.title+".");

System.out.println("Schools:\tCopies:");

for(int i = 0; i < schoolsAndCopies.length;i++)

for(int j = 0; j < schoolsAndCopies[i].length;j++)

System.out.println(schoolsAndCopies [i] +"\t" +

schoolsAndCopies[j]);

}

public String getClassName(){

return TEXTBOOK;

}

// overrides TechnicalBook toString

public String toString(){

return"Author = " + author +". Title = " + title +". Number of Pages = " +

numberPages +". Copies Sold = " + copiesSold +". Schools using " + title +

". Price = " + this.getPrice() +" = " + this.getNumberSchools() +". ";

}

}

/*

* ReferenceBook.java

*

* Created on April 19, 2007, 8:02 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package project3;

import java.util.*;

/**

*

* @author Kevin

*/

publicclass ReferenceBookextends TechnicalBook{

private ArrayList <String> conferences;

publicstaticfinal String REFERENCE_BOOK ="Reference Book";

//private int numberConferences;

/** Creates a new instance of ReferenceBook */

public ReferenceBook(String author, String title,int numberPages,int copiesSold,

double price){

super(author, title, numberPages, copiesSold, price);

conferences =new ArrayList();

}

public String getConference(int a){

for(int i = 0; i < conferences.size();i++)

if(i == a)

return conferences.get(i);

returnnull;

}

publicvoid addConference(String conference){

for(int i = 0; i < conferences.size();i++){

conferences.add(conference);

break;

//}

}

}

publicvoid printConferences(){

System.out.println("-");

System.out.println("Conferences made for " + this.title +".");

for(int i = 0; i < conferences.size(); i++){

System.out.println("["+(i + 1) +"]: "+ conferences.get(i));

}

System.out.println(conferences.size());

}

public String getClassName(){

return REFERENCE_BOOK;

}

public String toString(){

return super.toString();

}

}

/*

* BookTester.java

*

* Created on April 19, 2007, 8:02 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package project3;

/**

*

* @author Kevin

*/

publicclass BookTester{

/** Creates a new instance of BookTester */

publicstaticvoid main(String [] args){

BookCatalog catalog =new BookCatalog("Library");

TextBook java =new TextBook("John","Java", 5, 20, 5.00);

TextBook beans =new TextBook("Mike","JavaBeans", 6, 21, 5.00);

ReferenceBook ref =new ReferenceBook("Jones","Standard Class Library", 6, 23, 5.00);

ref.addConference("Meeting");

ref.printConferences();

beans.addSchool("UTSA");

beans.printSchoolsAndCopies("c++");

catalog.addBook(java);

catalog.addBook(beans);

catalog.printCatalog();

}

}

my BookCatalog class is fine, and everything prints out with out using the listed methods that give trouble.

/*

* BookCatalog.java

*

* Created on April 20, 2007, 4:42 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package project3;

import java.util.*;

/**

*

* @author Kevin

*/

publicclass BookCatalog{

private String name;

private ArrayList <Book> bookList;

/** Creates a new instance of BookCatalog */

public BookCatalog(String name){

this.name = name;

bookList =new ArrayList <Book>();

}

public String getCatalogName(){

return name;

}

publicvoid addBook(Book b){

bookList.add(b);

}

publicvoid printCatalog(){

for(int i = 0; i < bookList.size(); i ++){

System.out.println("[" + i +"]: " + bookList.get(i).getClassName() +

": " + bookList.get(i).toString());

}

}

}

thanks in advance

[14949 byte] By [kevin123a] at [2007-11-27 1:53:10]
# 1
post the code of the classes Book and TechnicalBook plz
java_2006a at 2007-7-12 1:22:58 > top of Java-index,Java Essentials,Java Programming...
# 2

public void addConference(String conference){

for(int i = 0; i < conferences.size();i++){

conferences.add(conference);

break;

//}

}

}

each call to addConference with result in a noop operation for a simple reason:

at the first call, conferences.size() == 0 ; so the program won't enter the loop

at the second call, nothing has been added so conferences.size() still == 0

etc

with such a method, you never truly add conferences

replace this method with:

public void addConference(String conference){

conferences.add(conference);

}

and maybe it will work better ^^

EDIT: Plus, you have to rewrite the following method:

public String getConference(int a){

for(int i = 0; i < conferences.size();i++)

if(i == a)

return conferences.get(i);

return null;

}

just do:

public String getConference(int a){

return conferences.get(a);

}

it will be better :P

EDIT2 : The second error comes from here:

String [] [] schoolsAndCopies = null;

for(int i = 0; i < c.length;i++){

for(int j = 0; j < d.length;j++){

schoolsAndCopies[i] = c;

schoolsAndCopies[j] = d;

}

}

you try to add objects to a null table;

just replace the declaration with:

String[][] schoolAndCopies = new String[c.length][d.length];

Plus: schoolsAndCopies = c; should be in the first loop

, not the second one

calvino_inda at 2007-7-12 1:22:58 > top of Java-index,Java Essentials,Java Programming...
# 3

i have one more problem, i get these errors in my BookCatalog class:

EDIT: i forgot to mention what methods these are:

insertItemByTitle() , sortByTitle(), and printCatalog()

/Users/Kevin/Documents/Programming/NetBeans/Java/project3/src/project3/BookCatalog.java:44: unexpected type

required: variable

found: value

bookList.get(position) = bookList.get(position-1);

/Users/Kevin/Documents/Programming/NetBeans/Java/project3/src/project3/BookCatalog.java:47: unexpected type

required: variable

found: value

bookList.get(position) = key;

/Users/Kevin/Documents/Programming/NetBeans/Java/project3/src/project3/BookCatalog.java:51: cannot find symbol

symbol : method sortByTitle()

location: class java.util.ArrayList<project3.Book>

bookList.sortByTitle();

3 errors

BUILD FAILED (total time: 0 seconds)

bookList.get(position) = bookList.get(position-1);

bookList.get(position) = key;

Heres my BookCatalog class

/*

* BookCatalog.java

*

* Created on April 20, 2007, 4:42 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package project3;

import java.util.*;

/**

*

* @author Kevin

*/

public class BookCatalog {

private String name;

private ArrayList <Book> bookList;

private Book [] bookList2;

/** Creates a new instance of BookCatalog */

public BookCatalog(String name) {

this.name = name;

bookList = new ArrayList <Book>();

}

public String getCatalogName(){

return name;

}

public void addBook(Book b){

bookList.add(b);

}

private void sortByTitle(){

if (bookList.size() > 1)

for (int index = 1; index < bookList.size(); index++)

insertItemByTitle(bookList, index);

}

private void insertItemByTitle(ArrayList <Book> bookList, int index) {

Book key = bookList.get(index);

int position = index;

while (position > 0 && key.getTitle().compareTo(bookList.get(index).getTitle()) < 0){

int posMinusOne = position - 1;

bookList.get(position) = bookList.get(position-1);

position--;

}

bookList.get(position) = key;

}

public void printCatalog(){

bookList.sortByTitle();

Book [] bookList2 = (Book[])bookList.toArray(new Book[bookList.size()]);

for(int i = 0; i < bookList2.length; i ++){

System.out.println("[" + (i+1) + "]: " + bookList2[i].getClassName() +

": " + bookList2[i].toString());

}

}

}

thanks

Message was edited by:

kevin123

Message was edited by:

kevin123

kevin123a at 2007-7-12 1:22:58 > top of Java-index,Java Essentials,Java Programming...
# 4

the first and second error comes from the fact you're calling the wrong method

you shouldn't do:

bookList.get(position) = key;

but :

bookList.set(position, key);

the third error comes from the fact you re calling "sortByTitle" on an ArrayList object ; that one does not have such a method

what you could do is to make a static method:

public static void sortByTitle(ArrayList<Book> bookList) {

// code

}

and call it with "sortByTitle(bookList)" in the code

calvino_inda at 2007-7-12 1:22:58 > top of Java-index,Java Essentials,Java Programming...
# 5

everything runs now, but it doesnt sort them. it prints them in the original order: here is the revised BookCatalog and Tester, also when making sortByTitle static it gives non static variable errors so i have no idea. Thanks for helping me fix the errors, i have no idea why it doesnt sort.

/*

* BookCatalog.java

*

* Created on April 20, 2007, 4:42 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package project3;

import java.util.*;

/**

*

* @author Kevin

*/

public class BookCatalog {

private String name;

private ArrayList <Book> bookList;

//private Book [] bookList2;

/** Creates a new instance of BookCatalog */

public BookCatalog(String name) {

this.name = name;

bookList = new ArrayList <Book>();

}

public String getCatalogName(){

return name;

}

public void addBook(Book b){

bookList.add(b);

}

public void sortByTitle(ArrayList<Book> list){

if (bookList.size() > 1)

for (int index = 1; index < bookList.size(); index++)

insertItemByTitle(bookList, index);

}

private void insertItemByTitle(ArrayList <Book> bookList, int index) {

Book key = bookList.get(index);

int position = index;

while (position > 0 && key.getTitle().compareTo(bookList.get(index).getTitle()) < 0){

bookList.set(position, bookList.get(position-1));// = bookList.set(position-1, key);

position--;

}

bookList.set(position, key);

}

public void printCatalog(){

sortByTitle(bookList);

Book [] bookList2 = (Book[])bookList.toArray(new Book[bookList.size()]);

for(int i = 0; i < bookList2.length; i ++){

System.out.println("[" + (i+1) + "]: " + bookList2[i].getClassName() +

": " + bookList2[i].toString());

}

}

}

/*

* BookTester.java

*

* Created on April 19, 2007, 8:02 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package project3;

/**

*

* @author Kevin

*/

public class BookTester{

/** Creates a new instance of BookTester */

public static void main(String [] args){

BookCatalog catalog = new BookCatalog("Library");

TextBook java = new TextBook("John", "Java", 5, 20, 5.00);

TextBook beans = new TextBook("Mike", "JavaBeans", 6, 21, 5.00);

ReferenceBook ref = new ReferenceBook("Jones", "Standard Class Library", 6, 23, 5.00);

ref.addConference("Meeting");

ref.printConferences();

beans.addSchool("UTSA");

//beans.printSchoolsAndCopies("c++");

catalog.addBook(ref);

catalog.addBook(java);

catalog.addBook(beans);

catalog.printCatalog();

}

}

kevin123a at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 6

about the non static context, this is because you do:

public void sortByTitle(ArrayList<Book> list){

if (bookList.size() > 1)

for (int index = 1; index < bookList.size(); index++)

insertItemByTitle(bookList, index);

}

where you should do:

public static void sortByTitle(ArrayList<Book> list){

if (list.size() > 1)

for (int index = 1; index < list.size(); index++)

insertItemByTitle(list, index);

}

you can't call non static attributes in a static method ; therefore, you use "list" which is a parameter of the static function, and not a class attribute

now, about the sort : what kind of sort do you want to do? lexicographic?

calvino_inda at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 7
yes alphabetically is how i will sort them, its an insertion sort algorithm that i've been using, but why isnt it working? i really dont understand.
kevin123a at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 8

i don't know why, and don't really want to read since i'm a bit tired, sorry :\

but for alphabetical sort, you may do something like that:

first, create a comparator object:

Comparator comp = new Comparator(){

public int compare(Object o1, Object o2) {

Book b1 = (Book) o1;

Book b2 = (Book) o2;

return b1.getTitle().compareTo(b2.getTitle());

}

};

then the only thing you have to do with this comparator is to do:

Collections.sort(bookList, comp);

and the list will be sorted :P

calvino_inda at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 9
thanks i really appreciate your help, i'll let ya know if i have any more trouble after i try it out
kevin123a at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 10
i'm off for tonight so good luck and good nightnext answers will be tomorrow's ;)
calvino_inda at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 11

i dont really understand what im supposed to do, i cant do it because compare is static, but i even tried making it static without implementing it and it still didnt print them out in order. I also dont know what to do with my comparator object and the method cant be seen by the rest of the class becuase it is closed off. I've never used comparator, and i have no idea whats going on.

kevin123a at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 12
anyone have any ideas?thanks
kevin123a at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 13

Here is what you have to do: add the following method to your BookCatalog class:

public void sortCatalog() {

Comparator comp = new Comparator(){

public int compare(Object o1, Object o2) {

Book b1 = (Book) o1;

Book b2 = (Book) o2;

return b1.getTitle().compareTo(b2.getTitle());

}

};

Collections.sort(bookList, comp);

}

just forget about static, if you don't even know what it means :p

copy this method, paste it in your class ; then, all you have to do is to call "sortCatalog()" wherever in your BookCatalog class, and the "bookList" arraylist will be sorted alphabetically

just try to call sortCatalog() and then to print the content of the list ; books must appear in lexicographic order

calvino_inda at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...
# 14
Thank you very much i really appreciate you taking your time to help me out. I've never made another comparator object, so i had no idea and it works great. thanks. I have no idea why my sorting algorithms werent working but now i dont care. Thanks.
kevin123a at 2007-7-12 1:22:59 > top of Java-index,Java Essentials,Java Programming...