The Artima Developer Community
Sponsored Link •

Java Answers Forum
Adding, removing, searching of an array

1 reply on 1 page. Most recent reply: Dec 15, 2004 11:50 PM by Varanasi R Pushpeswar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Christina Lee

Posts: 2
Nickname: christy
Registered: Dec, 2004

Adding, removing, searching of an array Posted: Dec 10, 2004 10:57 PM
Reply to this message Reply
Advertisement
I've tried and tried to get my java coding to work, but it's just not working....

I'm trying to add, remove, and search in an array.

For the adding, I am reading 3 strings from a user, verifying them, then making them into one object and then putting them into the array. The removnig/selling and I just don't know how to do it.

Help is so much appreciated. I pray for someone to help me with this. I would love them forever. <3

import java.io.*;
import java.util.*;
 
public class BookStore {
   static InputStreamReader isr = new InputStreamReader(System.in);
   static BufferedReader br = new BufferedReader(isr);
   
  public static void main (String [] arg) throws Exception {
 
    final int iSize = 200;
    Book book = new book (iISBN, sTitle, iQuantity); 
    String [] arrayBook = new String[iSize];
    arrayBook[i] = book;
 
//I have a switch statement for the menu, but took it out to save the space
}//close main
 
  public static void addBook() throws Exception {
    for (int i = 0; i<arrayBook.length; i++) {      
 
//getting the ISBN number
 boolean bISBN = false;
  while(!bISBN) {
 
    System.out.print("Enter the ISBN number: ");
    String sISBN = br.readLine();
    sISBN = sISBN.trim();
 
    try {
      int iOtherISBN = Integer.parseInt(sISBN);
      if(iOtherISBN >= 0 && iOtherISBN <= 199) {
         bISBN = true;
      }//end if
    }//end try
    catch (NumberFormatException nfe) {
      System.out.println("Number not within 0-199, back to the menu.");
      bISBN = false;
    }//end catch
  }//end while
 
//getting the Title
 boolean bTitle = false;
  while(!bTitle) {
 
    System.out.print("Enter your Title: ");
    String sOtherTitle = br.readLine();
    sOtherTitle = sOtherTitle.trim();
 
    if(sOtherTitle.length !=0) {
      bTitle = true;
    }//end if
    else {
      System.out.println("Your title is blank, go back to the menu");
      bTitle = false;
    }//end else
  }//end while
      
//getting the quantity
 boolean bQty = false;
  while(!bQty) {
 
   System.out.print("Quantity of books: ");
    String sQty = br.readLine();
    sQty = sQty.trim();
    
    try {     
      int iOtherQuantity = Integer.parseInt(sQty);
      if (iOtherQuantity >=0) {
        bQty = true;
      }//end if
      else {
        System.out.println("Your number is negavtive, go back to menu.");
        bQty = false;
      }//end else
    }//end try
    catch (NumberExceptionFormat nfe) {
      System.out.println("You did not enter a number, back to the menu you go.");
      bQty = false;
    }//end catch
 
    if(arrayBook[iISBN] ==null) { //checking if there's a book already
      arrayBook[iISBN] = new book();
    }//close if
    else {
       System.out.println("Book exists in there, cannot add, try again.");
    } //end else
 
    arrayBook[i] = new Book; //No idea how to add the object into the array
    }//end for
 
}//close addBook
 
//adding more copies to an exsiting book
public static void addMore(int iISBN, int iQuantity) throws Exception {
  for(int i = 0; i<arrayBook.length; i++) {
    if(iISBN !=null) {
      iQuantity = iQuantity + iOtherQuatity;
    }//end if
    else {
      System.out.println("There's no book here.");
    }//end else
  }//end for  
 
}//close addMore
 
 //selling books
  public static void sellBook(int iISBN, int iQuantity) {
    for (int i = 0; i<arrayBook.length; i++) {
      if(iISBN != null) { 
        if(iQuantity <= iOtherquantity) {
    	  iQuantity = iQuantity - iOtherQuantity;
	}//end if
	else {
	  System.out.println("There's only " + iQuantity + " amount of books left.");
	}//end else
      else {
	System.out.println("There's no book.");
      }//end else
    }//end for
 
}//end sellBook
 
 //searching for a book by the Title
  public static void findBook(String sOtherTitle) {
    for (int i = 0; i<arrayBook.length; i++) {
      //I have absolutely no clue how to search the array by the title
 
  }//close find
 
}//close class
 


Varanasi R Pushpeswar

Posts: 6
Nickname: ravieswar
Registered: May, 2004

Re: Adding, removing, searching of an array Posted: Dec 15, 2004 11:50 PM
Reply to this message Reply
Hi this is Eswar,

why do u messup ur good prog...using arrays...cant u just make use of Vectors...

like as u have threeobjects added up ...

make a new class...with 3 member variables...

put the values u wanted to added to array ...(makeing an object of dat class)

add this object to the vector...it may make ur prog...ease..

andthe....

like
class aaa
{
int a,b,c;
}

class abb
{
jpublicvoid fun()
{
aaa a1 = new aaa();
a1.a =1; a1.b=1;a1.c=2;

Vector v= new Vector();
v.add(a1);

...do samefor all those values u wanted to add in an array..instead use this class instance and put it into the vector...

in retaining theobject..use
aaa b=new aaa();
b=(aaa) v.next();orsome other vector..retieving functio...
ok...if it worksplzdo enform..meok bye
}
}

Flat View: This topic has 1 reply on 1 page
Topic: use of static in overridden method Previous Topic   Next Topic Topic: Adding an object as an array

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use