Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,178 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,455 people online right now. Registration is fast and FREE... Join Now!




Unknown error

 
Reply to this topicStart new topic

Unknown error, array required but java.lang.String found

needhelpinjava
post 10 Oct, 2008 - 07:22 AM
Post #1


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 4

java
public class BookManager{

Book[] a= new Book[1000];
int count=0;



public void addBook(String i, String t, char ty, String s, String a, String p, String db){
Book b=new Book (i, t, ty, s, a, p, db);
a [count] = b;
count++;
}

public void deleteBook(String isbn){


}

public int getBook(String isbn){
for(int i=0; i<a.length; i++){
if(a[i].getIsbn().compareToIgnoreCase(isbn)==0){
return i;
}

else

System.out.println("Book not found.");
}
}


}






BOOK CLASS

java

public class Book {

private String isbn;
private String title;
private char type;
private String status;
private String author;
private String publisher;
private String dateBorrowed;




public Book(String i, String t, char ty, String s, String a, String p, String db){
isbn=i;
title=t;
type=ty;
status=s;
author=a;
publisher=p;
dateBorrowed=db;
}

//Getters
public String getIsbn(){
return isbn;
}

public String getTitle(){
return title;
}

public char getType(){
return type;
}

public String getStatus(){
return status;
}

public String getAuthor(){
return author;
}

public String getPublisher(){
return publisher;
}

public String getDateBorrowed(){
return dateBorrowed;
}

//Setters

public void setIsbn(String i){
isbn=i;
}

public void setTitle(String t){
title=t;
}

public void setType(char ty){
type=ty;
}

public void setStatus(String s){
status=s;
}

public void setAuthor(String a){
author=a;
}

public void setPublisher(String p){
publisher=p;
}

public void setDateBorrowed(String db){
dateBorrowed=db;
}
}


code.gif

This post has been edited by William_Wilson: 10 Oct, 2008 - 07:31 AM
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 10 Oct, 2008 - 07:31 AM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,969



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


For starters, your variable names are horrible, the constructor takes a lot of variables and I have no idea what any of them are.

your getBook method should always return a value you will need to add a final line that returns a value if the book is not found.

Finally what line is your error on?
User is offlineProfile CardPM

Go to the top of the page

needhelpinjava
post 10 Oct, 2008 - 08:01 AM
Post #3


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 4

QUOTE(needhelpinjava @ 10 Oct, 2008 - 08:22 AM) *

java
public class BookManager{

Book[] a= new Book[1000];
int count=0;



public void addBook(String i, String t, char ty, String s, String a, String p, String db){
Book b=new Book (i, t, ty, s, a, p, db);
a [count] = b;
count++;
}

public void deleteBook(String isbn){


}

public int getBook(String isbn){
for(int i=0; i<a.length; i++){
if(a[i].getIsbn().compareToIgnoreCase(isbn)==0){
return i;
}

else

System.out.println("Book not found.");
}
}


}






BOOK CLASS

java

public class Book {

private String isbn;
private String title;
private char type;
private String status;
private String author;
private String publisher;
private String dateBorrowed;




public Book(String i, String t, char ty, String s, String a, String p, String db){
isbn=i;
title=t;
type=ty;
status=s;
author=a;
publisher=p;
dateBorrowed=db;
}

//Getters
public String getIsbn(){
return isbn;
}

public String getTitle(){
return title;
}

public char getType(){
return type;
}

public String getStatus(){
return status;
}

public String getAuthor(){
return author;
}

public String getPublisher(){
return publisher;
}

public String getDateBorrowed(){
return dateBorrowed;
}

//Setters

public void setIsbn(String i){
isbn=i;
}

public void setTitle(String t){
title=t;
}

public void setType(char ty){
type=ty;
}

public void setStatus(String s){
status=s;
}

public void setAuthor(String a){
author=a;
}

public void setPublisher(String p){
publisher=p;
}

public void setDateBorrowed(String db){
dateBorrowed=db;
}
}


code.gif


my error is on line 10, bookmanager class. Sorry but i am very new to this programming.
User is offlineProfile CardPM

Go to the top of the page

needhelpinjava
post 10 Oct, 2008 - 08:09 AM
Post #4


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 4

QUOTE(William_Wilson @ 10 Oct, 2008 - 08:31 AM) *

For starters, your variable names are horrible, the constructor takes a lot of variables and I have no idea what any of them are.

your getBook method should always return a value you will need to add a final line that returns a value if the book is not found.

Finally what line is your error on?


line 10
User is offlineProfile CardPM

Go to the top of the page

pbl
post 10 Oct, 2008 - 01:54 PM
Post #5


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,946



Thanked 187 times

Dream Kudos: 75
My Contributions


As William Wilson mentionned your variable names are horrible and generate your problem

CODE

    Book[] a= new Book[1000];  
    int count=0;  
      
    public void addBook(String i, String t, char ty, String s, String a, String p, String db){  
    Book b=new Book (i, t, ty, s, a, p, db);  
     a [count] = b;  


When you write

a [count] = b;

you are not referencing: Book[] a= new Book[1000];

but the String a in your parameters: public void addBook(String i, String t, char ty, String s, String a, String p, String db){

using "this" that says use the "a" instance variable instead of the "a" parameter would fix your problem... but use more appropriate variables name

CODE

    Book[] a= new Book[1000];  
    int count=0;  
      
    public void addBook(String i, String t, char ty, String s, String a, String p, String db){  
    Book b=new Book (i, t, ty, s, a, p, db);  
    this.a [count] = b;  


This post has been edited by pbl: 10 Oct, 2008 - 01:56 PM
User is online!Profile CardPM

Go to the top of the page

needhelpinjava
post 11 Oct, 2008 - 10:46 AM
Post #6


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 4

QUOTE(pbl @ 10 Oct, 2008 - 02:54 PM) *

As William Wilson mentionned your variable names are horrible and generate your problem

CODE

    Book[] a= new Book[1000];  
    int count=0;  
      
    public void addBook(String i, String t, char ty, String s, String a, String p, String db){  
    Book b=new Book (i, t, ty, s, a, p, db);  
     a [count] = b;  


When you write

a [count] = b;

you are not referencing: Book[] a= new Book[1000];

but the String a in your parameters: public void addBook(String i, String t, char ty, String s, String a, String p, String db){

using "this" that says use the "a" instance variable instead of the "a" parameter would fix your problem... but use more appropriate variables name

QUOTE
thats alot pbl, i will work on my names. u wre very helpfull.


CODE

    Book[] a= new Book[1000];  
    int count=0;  
      
    public void addBook(String i, String t, char ty, String s, String a, String p, String db){  
    Book b=new Book (i, t, ty, s, a, p, db);  
    this.a [count] = b;  


User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 03:40PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month