Results 1 to 5 of 5
Thread: HELP ME with tis code ?????
- 11-26-2008, 07:35 PM #1
Member
- Join Date
- Oct 2008
- Location
- Egypt
- Posts
- 26
- Rep Power
- 0
HELP ME with tis code ?????
import java.io.*;
public class booksmanagement {
private static String inDataBook;
private static int mm;
private static int m ;
private static int ii;
private static int aa;
private static int ss;
private static int IS;
private static String [] BooksInformation = new String[100];
public booksmanagement(){
m=0;
for(int x=0; x<BooksInformation.length;x++)
BooksInformation[x]="";
}
//method to load from the file into the array
public static String[] LoadBooksInFormation() throws IOException{
BufferedReader in=new BufferedReader(new FileReader("C:\\Users\\VooDoo\\Desktop\\LibrarySys tem\\books.txt"));
String read;
while((read = in.readLine()) != null && m<BooksInformation.length){
BooksInformation[m]=read.toLowerCase();
m++;
}
in.close();
return BooksInformation;
}
//method to get the number of elements which is not empety
public static int getDataSize(){
return m ;
}
// method to take data from the user and write it in the array and the file
public static String[] TakeBooksInformationfromUser() throws IOException{
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
inDataBook="";
while(inDataBook !=null && m < BooksInformation.length ){
System.out.println("Enter the book informations withe a (,) between like this \n (title,author,publisher,ISBN,date of publication,number of copies,current availible numper of copies,category): ");
inDataBook=in.readLine();
if(inDataBook.equals(""))
inDataBook=null;
else{
BooksInformation[m]=inDataBook.toLowerCase();
BufferedWriter out= new BufferedWriter(new FileWriter("C:\\Users\\VooDoo\\Desktop\\LibrarySys tem\\books.txt",true));
out.write(inDataBook.toLowerCase()+"\n");
out.close();
m++;}
}
return BooksInformation;
}
//method to search for books by the title of the book
public static String [] SearchWithTitle(String title)
{
String [] SearchResultT=new String[BooksInformation.length];
SearchResultT[0]="book is not found";
for(int o=1; o<SearchResultT.length; o++)
SearchResultT[o]="";
int hh;
mm=0;
for( hh=0 ; hh < m ;hh++ ){
int f=0;
int k=0;
int z=1;
String s1="";
String s2="";
String s3="";
String [] search=BooksInformation[hh].split(",");
while( f<title.length() && k<search[0].length()){
s1= Character.toString(title.toLowerCase().charAt(f)) ;
s2=Character.toString(search[0].charAt(k));
if (s1.equals(s2))
{
f++;
k++;
s3 +=s2;
}
else{
k=z;
f=0;
s3="";
z++;
}
if(title.toLowerCase().equals(s3)){
SearchResultT[mm]=BooksInformation[hh];
mm++;
}
if( s3.equals(title.toLowerCase()))
break;
}
}
return SearchResultT;
}
//method to get the Search result array size
public static int getSearchTiltlarraySize()
{int gg=mm+1;
return gg;
}
//method to search by the ISBN number
public static String [] SearchWithISBN(String ISBN)
{
String [] SearchResultI=new String[BooksInformation.length];
SearchResultI[0]="book is not found";
for(int o=1; o<SearchResultI.length; o++)
SearchResultI[o]="";
IS=0;
int hh;
ii=0;
for( hh=0 ; hh<m;hh++ ){
int z=1;
String s1="";
String s2="";
String s3="";
int f=0;
int k=0;
String [] search=BooksInformation[hh].split(",");
while( f<ISBN.length() && k<search[3].length()){
s1= Character.toString(ISBN.charAt(f)) ;
s2=Character.toString(search[3].charAt(k));
if (s1.equals(s2))
{
f++;
k++;
s3 +=s2;
}
else{
k=z;
f=0;
z++;
s3="";
}
}
if(ISBN.equals(s3)){
IS=hh;
SearchResultI[ii]=BooksInformation[hh];
ii++;
}
}
return SearchResultI;
}
//method to get the Search result array size
public static int getSearchISBNarraySize()
{int ll=ii+1;
return ll;
}
//method to search for books by the Author of the book
public static String [] SearchWithAuthor(String author)
{
String [] SearchResultA=new String[BooksInformation.length];
SearchResultA[0]="book is not found";
for(int o=1; o<SearchResultA.length; o++)
SearchResultA[o]="";
int hh;
aa=0;
for( hh=0 ; hh<m;hh++ ){
int z=1;
String s1="";
String s2="";
String s3="";
int f=0;
int k=0;
String [] search=BooksInformation[hh].split(",");
while( f<author.toLowerCase().length() && k<search[1].length()){
s1= Character.toString(author.toLowerCase().charAt(f)) ;
s2=Character.toString(search[1].charAt(k));
if (s1.equals(s2))
{
f++;
k++;
s3 +=s2;
}
else{
f=0;
k=z;
s3="";
z++;
}
}
if(author.toLowerCase().equals(s3)){
SearchResultA[aa]=BooksInformation[hh];
aa++;
}
}
return SearchResultA;
}
public static int getSearchAuthorarraySize(){
int jj=aa+1;
return jj;
}
//method to search for books by the Category of the book
public static String [] SearchWithCategory(String category)
{
String [] SearchResultC=new String[BooksInformation.length];
SearchResultC[0]="book is not found";
for(int o=1; o<SearchResultC.length; o++)
SearchResultC[o]="";
int hh;
ss=0;
for( hh=0 ; hh<m;hh++ ){
int z=1;
String s1="";
String s2="";
String s3="";
int f=0;
int k=0;
String [] search=BooksInformation[hh].split(",");
while( f<category.toLowerCase().length() && k<search[7].length()){
s1= Character.toString(category.toLowerCase().charAt(f )) ;
s2=Character.toString(search[7].charAt(k));
if (s1.equals(s2))
{
f++;
k++;
s3 +=s2;
}
else{
f=0;
k=z;
s3="";
z++;
}
}
if(category.toLowerCase().equals(s3)){
SearchResultC[ss]=BooksInformation[hh];
ss++;
}
}
return SearchResultC;
}
public static int getSearchCategoryarraySize(){
int pp=ss+1;
return pp;
}
// method to add new copy
public static void AddNewCopies(String ISBN, int NumberOfCopies){
booksmanagement.SearchWithISBN(ISBN);
String [] ff=new String[8];
ff=BooksInformation[IS].split(",");
String totalnmber=String.valueOf(Integer.parseInt(ff[5])+NumberOfCopies);
String totalAVilible=String.valueOf(Integer.parseInt(ff[6])+NumberOfCopies);
ff[5]=totalnmber;
ff[6]=totalAVilible;
String totalbook=ff[0]+","+ff[1]+","+ff[2]+","+ff[3]+","+ff[4]+","+ff[5]+","+ff[6]+","+ff[7];
BooksInformation[IS]=totalbook;
if(IS==0)
System.out.println("Wrong ISBN ");
}
public static void DeleteBooks(String ISBN){
booksmanagement.SearchWithISBN(ISBN);
BooksInformation[IS]="";
if(IS==0)
System.out.println("Wrong ISBN ");
}
public static void RewriteBooksInformations() throws IOException{
int w;
BufferedWriter out= new BufferedWriter(new FileWriter("C:\\Users\\VooDoo\\Desktop\\LibrarySys tem\\books.txt"));
String trans;
for(w=0;w<=m;w++){
trans=BooksInformation[w];
out.write(trans+"\n");
}
out.close();
}
}
- 11-26-2008, 07:35 PM #2
Member
- Join Date
- Oct 2008
- Location
- Egypt
- Posts
- 26
- Rep Power
- 0
the main class
import java.io.*;
public class theMainClass {
public static void main(String[] args) throws IOException {
new booksmanagement();
booksmanagement.LoadBooksInFormation();
booksmanagement.TakeBooksInformationfromUser();
booksmanagement.AddNewCopies("0-13-089468-0", 3);
String [] search=booksmanagement.SearchWithTitle("core java");
for(int q=0 ; q<booksmanagement.getSearchTiltlarraySize(); q++)
System.out.println(search[q]);
String [] search1=booksmanagement.SearchWithISBN("123123");
for(int q=0 ; q<booksmanagement.getSearchISBNarraySize(); q++)
System.out.println(search1[q]);
String [] search3=booksmanagement.SearchWithAuthor("cay");
for(int q=0 ; q<booksmanagement.getSearchAuthorarraySize(); q++)
System.out.println(search3[q]);
String [] search4=booksmanagement.SearchWithCategory("pro");
for(int q=0 ; q<booksmanagement.getSearchCategoryarraySize(); q++)
System.out.println(search4[q]);
booksmanagement.RewriteBooksInformations();
}
}
- 11-26-2008, 07:36 PM #3
Member
- Join Date
- Oct 2008
- Location
- Egypt
- Posts
- 26
- Rep Power
- 0
this is the problem
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at booksmanagement.SearchWithISBN(booksmanagement.jav a:163)
at booksmanagement.AddNewCopies(booksmanagement.java: 320)
at theMainClass.main(theMainClass.java:13)
- 11-27-2008, 01:34 AM #4
Arrays are index based
Arrays are indexed based (start at 0). Could the following be the problem?
Luck,Java Code:for([COLOR="Red"]int o=1[/COLOR]; o<SearchResultI.length; o++) [COLOR="Red"]//Should int o = 0 ?[/COLOR]
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-27-2008, 05:19 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes you are accessing an invalid index of the array, index 3 according to your error message. As CJSLMAN explain above, arrays are zero base indexing.
Similar Threads
-
code for £20;-)
By sparky2003752 in forum JCreatorReplies: 0Last Post: 04-17-2008, 05:31 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
help with oop code
By nhlfan in forum New To JavaReplies: 1Last Post: 11-27-2007, 08:21 PM -
Need help with my code.
By stormviper in forum New To JavaReplies: 0Last Post: 07-12-2007, 03:18 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks