Assignment help :)

Hi fellas, looking for some help in my progress with my assigment, program so far compiles fine its in 2 seperate files driver file and object file, im preety newb and the problem i cant solve is when i choose option 2 and enter an isbn number which is not found it doesnt do what its supposed to do, it gives me errors. Ignore the comments.

OBJECT FILE

/* Class details

* 1- Class Data members are defined

* 2- Two constructores

* 3_ service "CollectStudentRec()" [one record only]

* Note: nothing has being added to Step2Class to make Step3Class

*/

//importing JOptionPane class

import javax.swing.JOptionPane;

//menu options using switchStatement

public class Step3Class

{ // data memebers

static String HEADER = "ISBN Number Author BookTitlePrice\n"+

"===============================================================\n";

final static int MAX_BOOKS = 10;// Maximum number of students possible

static int books=0; // current number of students at any given time

String isbn ;// data member not an array

String author ;

String booktitle;

double price ;

// methods

//Constructor-1 with no args

public Step3Class()

{

isbn = "1-741";

author = "John Brown";

booktitle= "The Java Way";

price = 125.45;

}

//Constructor-2 with args

public Step3Class(String BId, String AName, String BName, double BCost)

{

isbn = BId;

author = AName;

booktitle= BName;

price = BCost;

}

// Class Services

public String CollectStudentRec()//Collect Rec-info

{String result ="";

result =isbn+""+

author+" "+

booktitle+""+

price+"\n";

return result;

}// Ednd of method CollectStudentRec()

public String getID() {

String id;

id = isbn;

return id;

}

}// end of 'class'

DRIVER FILE

/* 1_switchStatement

* 2_ A_ Creating one object & initialising the Student DB

*B_ Displaying the DB using "ref array"

* 3- Adding new student

*/

//importing JOptionPane class

import javax.swing.JOptionPane;

//menu options using switchStatement

public class Step3Driver

{

public static void main(String[] args)

{

// array of ref var of size 10 of type Step2Class

Step3Class[] arrBook= new Step3Class[10];

// creating the first object using default constructor

arrBook[0] = new Step3Class(); // default constructor

++Step3Class.books;// incrementing number of students

String menu="Enter a number to choose one of the following :\n"+

"(1) Process a new book\n"+

"(2) Search for a given ISBN number\n"+

"(3) Search for a given book title\n"+

"(4) Delete a given ISBN number\n"+

"(5) Calulate total number of books\n"+

"(6) Calculate total of all prices\n"+

"(7) Display current database\n"+

"(0) Quit(activity report)\n";

int option;// used to store users selection

String strOption;

do

{// Reading menu-input

strOption=JOptionPane.showInputDialog(null,menu,

"Please Select Option",JOptionPane.INFORMATION_MESSAGE );

option = Integer.parseInt(strOption);

// switch statement

switch(option){

// add a student to the database

// New code

case 1 :addBook(arrBook);

// End of new code

break;

// search for an ID number of a student

case 2:

String target = JOptionPane.showInputDialog(null,"Insert Book ISBN ","Search for Book", JOptionPane.INFORMATION_MESSAGE );

int answer = getisbnresult(target,arrBook);

if(answer==-1) // book is not found

JOptionPane.showMessageDialog(null,"*** ISBN number NOT FOUND ***\n"+

"The ISBN number "+ target +" does not exist.","Error",JOptionPane.INFORMATION_MESSAGE);

else

JOptionPane.showInputDialog(null,"menu",

"Please Select Option",JOptionPane.INFORMATION_MESSAGE );

break;

// find students name

case 3: JOptionPane.showMessageDialog(null,"InsertCode","Search for student name", JOptionPane.INFORMATION_MESSAGE );

break;

case 4: JOptionPane.showMessageDialog(null,"InsertCode","Delete a given ID rec", JOptionPane.INFORMATION_MESSAGE );

break;

// calculate total amount of students.

case 5: JOptionPane.showMessageDialog(null,"InsertCode","Total Amount of student", JOptionPane.INFORMATION_MESSAGE );

break;

// calculate total fees for students

case 6: JOptionPane.showMessageDialog(null,"InsertCode","Total Fees", JOptionPane.INFORMATION_MESSAGE );

break;

case 7: // to display current DB

//New cod//

//*******//

displayBooks(arrBook);

// End of new code//

// ***************//

break;

case 0: // to quit and print an activity report

JOptionPane.showMessageDialog(null,"InsertCode","Quit the program", JOptionPane.INFORMATION_MESSAGE );

break;

default:

JOptionPane.showMessageDialog(null,"Whoops !!!! Number must" +

" be between 0 & 7","Error in selection", JOptionPane.ERROR_MESSAGE );

} // end of switch

} while(option != 0); // end of loop

System.exit(0);// terminate the program

} // end of driver'main()'

// A static method calling a non static method for displaying Student DB

public static void displayBooks(Step3Class[] arrBook)

{String result;

result =Step3Class.HEADER;

for(int i=0;i<Step3Class.books;i++)

{

result +=arrBook.CollectStudentRec(); // "arrStudent" is the object

// "collectStudentRec()" is a class service

}

JOptionPane.showMessageDialog(null,result,"Current DataBase", JOptionPane.INFORMATION_MESSAGE );

}// Ednd of method display

public static void addBook(Step3Class[] arrBook)

{

String priceStr="", isbn,author,booktitle;

double price;

//checking if DB is full

if (Step3Class.books == arrBook.length)

{

JOptionPane.showMessageDialog(null,"Bookshelf is FULL !!","Error",

JOptionPane.ERROR_MESSAGE);

return;

}

// DB is not full. Reading a rec from user

isbn =JOptionPane.showInputDialog("What is the ISBN # ?");

author=JOptionPane.showInputDialog("What is the authors name ?");

booktitle=JOptionPane.showInputDialog("What is the book title ?");

priceStr=JOptionPane.showInputDialog("What is the price of the book ?");

price= Double.parseDouble(priceStr);

// creating an object using constructor2 with values read from the user

arrBook[Step3Class.books] = new Step3Class(isbn,author,booktitle,price);

// update student number

++Step3Class.books;

}

public static int getisbnresult(String target, Step3Class[] arrBook){

for (int i=0; i><=Step3Class.books; i++)

{

if (target.equals(arrBook.getID()))

return i;

}

return -1;

}

//***********************************************

}// end of 'class'

[7286 byte] By [Vapera] at [2007-11-27 7:56:15]
# 1
Please post your code between [code] tags, post a full stack-trace and descriptive error message.
Navy_Codera at 2007-7-12 19:37:49 > top of Java-index,Java Essentials,New To Java...
# 2

sorry

OBJECT FILE

/* Class details

* 1- Class Data members are defined

* 2- Two constructores

* 3_ service "CollectStudentRec()" [one record only]

* Note: nothing has being added to Step2Class to make Step3Class

*/

//importing JOptionPane class

import javax.swing.JOptionPane;

//menu options using switchStatement

public class Step3Class

{ // data memebers

static String HEADER = "ISBN Number Author BookTitlePrice\n"+

"===============================================================\n";

final static int MAX_BOOKS = 10;// Maximum number of students possible

static int books=0; // current number of students at any given time

String isbn ;// data member not an array

String author ;

String booktitle;

double price ;

// methods

//Constructor-1 with no args

public Step3Class()

{

isbn = "1-741";

author = "John Brown";

booktitle= "The Java Way";

price = 125.45;

}

//Constructor-2 with args

public Step3Class(String BId, String AName, String BName, double BCost)

{

isbn = BId;

author = AName;

booktitle= BName;

price = BCost;

}

// Class Services

public String CollectStudentRec()//Collect Rec-info

{String result ="";

result =isbn+""+

author+" "+

booktitle+""+

price+"\n";

return result;

}// Ednd of method CollectStudentRec()

public String getID() {

String id;

id = isbn;

return id;

}

}// end of 'class'

DRIVER FILE--

/* 1_switchStatement

* 2_ A_ Creating one object & initialising the Student DB

*B_ Displaying the DB using "ref array"

* 3- Adding new student

*/

//importing JOptionPane class

import javax.swing.JOptionPane;

//menu options using switchStatement

public class Step3Driver

{

public static void main(String[] args)

{

// array of ref var of size 10 of type Step2Class

Step3Class[] arrBook= new Step3Class[10];

// creating the first object using default constructor

arrBook[0] = new Step3Class(); // default constructor

++Step3Class.books;// incrementing number of students

String menu="Enter a number to choose one of the following :\n"+

"(1) Process a new book\n"+

"(2) Search for a given ISBN number\n"+

"(3) Search for a given book title\n"+

"(4) Delete a given ISBN number\n"+

"(5) Calulate total number of books\n"+

"(6) Calculate total of all prices\n"+

"(7) Display current database\n"+

"(0) Quit(activity report)\n";

int option;// used to store users selection

String strOption;

do

{// Reading menu-input

strOption=JOptionPane.showInputDialog(null,menu,

"Please Select Option",JOptionPane.INFORMATION_MESSAGE );

option = Integer.parseInt(strOption);

// switch statement

switch(option){

// add a student to the database

// New code

case 1 :addBook(arrBook);

// End of new code

break;

// search for an ID number of a student

case 2:

String target = JOptionPane.showInputDialog(null,"Insert Book ISBN ","Search for Book", JOptionPane.INFORMATION_MESSAGE );

int answer = getisbnresult(target,arrBook);

if(answer==-1) // book is not found

JOptionPane.showMessageDialog(null,"*** ISBN number NOT FOUND ***\n"+

"The ISBN number "+ target +" does not exist.","Error",JOptionPane.INFORMATION_MESSAGE);

else

JOptionPane.showInputDialog(null,"menu",

"Please Select Option",JOptionPane.INFORMATION_MESSAGE );

break;

// find students name

case 3: JOptionPane.showMessageDialog(null,"InsertCode","Search for student name", JOptionPane.INFORMATION_MESSAGE );

break;

case 4: JOptionPane.showMessageDialog(null,"InsertCode","Delete a given ID rec", JOptionPane.INFORMATION_MESSAGE );

break;

// calculate total amount of students.

case 5: JOptionPane.showMessageDialog(null,"InsertCode","Total Amount of student", JOptionPane.INFORMATION_MESSAGE );

break;

// calculate total fees for students

case 6: JOptionPane.showMessageDialog(null,"InsertCode","Total Fees", JOptionPane.INFORMATION_MESSAGE );

break;

case 7: // to display current DB

//New cod//

//*******//

displayBooks(arrBook);

// End of new code//

// ***************//

break;

case 0: // to quit and print an activity report

JOptionPane.showMessageDialog(null,"InsertCode","Quit the program", JOptionPane.INFORMATION_MESSAGE );

break;

default:

JOptionPane.showMessageDialog(null,"Whoops !!!! Number must" +

" be between 0 & 7","Error in selection", JOptionPane.ERROR_MESSAGE );

} // end of switch

} while(option != 0); // end of loop

System.exit(0);// terminate the program

} // end of driver'main()'

// A static method calling a non static method for displaying Student DB

public static void displayBooks(Step3Class[] arrBook)

{String result;

result =Step3Class.HEADER;

for(int i=0;i<Step3Class.books;i++)

{

result +=arrBook[i].CollectStudentRec(); // "arrStudent[i]" is the object

// "collectStudentRec()" is a class service

}

JOptionPane.showMessageDialog(null,result,"Current DataBase", JOptionPane.INFORMATION_MESSAGE );

}// Ednd of method display

public static void addBook(Step3Class[] arrBook)

{

String priceStr="", isbn,author,booktitle;

double price;

//checking if DB is full

if (Step3Class.books == arrBook.length)

{

JOptionPane.showMessageDialog(null,"Bookshelf is FULL !!","Error",

JOptionPane.ERROR_MESSAGE);

return;

}

// DB is not full. Reading a rec from user

isbn =JOptionPane.showInputDialog("What is the ISBN # ?");

author=JOptionPane.showInputDialog("What is the authors name ?");

booktitle=JOptionPane.showInputDialog("What is the book title ?");

priceStr=JOptionPane.showInputDialog("What is the price of the book ?");

price= Double.parseDouble(priceStr);

// creating an object using constructor2 with values read from the user

arrBook[Step3Class.books] = new Step3Class(isbn,author,booktitle,price);

// update student number

++Step3Class.books;

}

public static int getisbnresult(String target, Step3Class[] arrBook){

for (int i=0; i><=Step3Class.books; i++)

{

if (target.equals(arrBook[i].getID()))

return i;

}

return -1;

}

//***********************************************

}// end of 'class'

ERROR =

Exception in thread "main" java.lang.NullPointerException

at Step3Driver.getisbnresult<Step3Driver.java:159>

at Step3Driver.main<Step3Driver.java:64>

Vapera at 2007-7-12 19:37:49 > top of Java-index,Java Essentials,New To Java...
# 3
Change this line:for (int i=0; i><=Step3Class.books; i++)To this:for (int i=0; i<Step3Class.books; i++)>
Navy_Codera at 2007-7-12 19:37:49 > top of Java-index,Java Essentials,New To Java...
# 4
I cant believe i didnt even see thatThank you so muchHey while i have you attention, could you give me any pointers on how to delete the book records (Case 4) ?
Vapera at 2007-7-12 19:37:49 > top of Java-index,Java Essentials,New To Java...
# 5

> I cant believe i didnt even see that

>

> Thank you so much

Sure thing.

>

> Hey while i have you attention, could you give me any

> pointers on how to delete the book records (Case 4) ?

Personally, I don't like using Arrays. I'd use another collection, such as an ArrayList. They allow for dynamic resizing and have an excellent array of methods available for your use. Here is the link to the API:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

Navy_Codera at 2007-7-12 19:37:49 > top of Java-index,Java Essentials,New To Java...