Results 1 to 7 of 7
  1. #1
    alfred199205 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Hello,Can you give me some advice of my program?

    I have done a program about Library Management System,There are some error in it, but I can't find it, can you give me a hand?


    Java Code:
    import java.util.*;
    import java.io.*;
    	class book{
    		Vector book = new Vector();
    	 	void insertbook(int b,String s1){
            	  book.add(b,s1);
            	  }
          	void searchbook(String s1){
          		int h = book.indexOf(s1);
          		int j = book.lastIndexOf(s1);
          		for(int i = h; i <= j; i++){
          		System.out.println(s1+" "+i);
          		}
          	}	
          	int s(){
          		int e = book.size();
          		return e;
          	}
        }
    	class newspaper{
    		Vector newspaper = new Vector();
    		void insertnewspaper(String s1,int b){
    			newspaper.add(b,s1);
    		}
    		void searchnewspaper(String s1){
    			int h = newspaper.indexOf(s1);
    			int j = newspaper.lastIndexOf(s1);
    			for(int i = h; i <= j; i++){
    				System.out.println(s1 + " " + i);
    			}
    		}	
    		int s(){
    			int e = newspaper.size();
    			return e;
      		}
    	}
    	class vedio{
    		Vector vedio = new Vector();
    	 	void insertvedio(String s1,int b){
            	  vedio.add(b,s1);
            	  }
    	 	void searchvedio(String s1){
    	  		int h = vedio.indexOf(s1);
    	  		int j = vedio.lastIndexOf(s1);
    	  		for(int i = h; i <= j; i++){
    	  		System.out.println(s1 + " " + i);
    	  		}
    	  	}	
          	int s(){
          		int e = vedio.size();
          		return e;
          	}
    	  }
    public class xing{
    	public static void main(String[] args) throws IOException{
    	    book b=new book();
    		newspaper n = new newspaper();
    		vedio v = new vedio();
    		InputStreamReader isr = new InputStreamReader(System.in);
    		BufferedReader br = new BufferedReader(isr);
    		String select;
    		String s1;
    		int c;
    		while(true){
    			System.out.println("<< 1.Add Books  2.Add Newspaper 3.Add CDs 4.Find Books 5.Find Newspaper 6.Find CDs  >>");
    			System.out.println("Please choose...");
    			select = br.readLine();
    			if(select.equals("1")){
    				System.out.println("Please enter the Book's  name which you want to Add:");
    				s1 = br.readLine();
    				c = n.s() + 1;
    				b.insertbook(c,s1);
    			}
    			
    			if(select.equals("2")){
    				System.out.println("Please enter the Newspaper's name which you want to Add:");
    				s1 = br.readLine();
    				c = n.s() + 1;
    				n.insertnewspaper(s1,c);
    			}
    			if(select.equals("3")){
    				System.out.println("Please enter the CDs' name which you want to Add:");
    				s1 = br.readLine();
    				c = v.s()+1;
    				v.insertvedio(s1,c);
    			}
    			if(select.equals("4")){
    				System.out.println("Please enter the Book's  name which you want to find:");
    				s1 = br.readLine();
    				b.searchbook(s1);
    			}
    			if(select.equals("5")){
    				System.out.println("Please enter the newspaper's  name which you want to find:");
    				s1 = br.readLine();
    				n.searchnewspaper(s1);
    			}
    			if(select.equals("6")){
    				System.out.println("Please enter the CDs' name which you want to find:");
    				s1 = br.readLine();
    				v.searchvedio(s1);
    			}
    		}
    	}
    }
    Thanks everyone.
    Last edited by JosAH; 11-30-2012 at 10:10 AM. Reason: added [code] ... [/code] tags

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Hello,Can you give me some advice of my program?

    Can you post any error message that you've found in your program?

  3. #3
    alfred199205 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Hello,Can you give me some advice of my program?

    yeah,When I Insert the book's name, throws the exception that
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 > 0
    at java.util.Vector.insertElementAt(Unknown Source)
    at java.util.Vector.add(Unknown Source)
    at book.insertbook(xing.java:6)
    at xing.main(xing.java:72)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Hello,Can you give me some advice of my program?

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Hello,Can you give me some advice of my program?

    Stick some debugging in the code to see what values you are using.

    I would ask why you are not simply doing an book.add(s1) rather than giving it an insertion point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Hello,Can you give me some advice of my program?

    Quote Originally Posted by alfred199205 View Post
    yeah,When I Insert the book's name, throws the exception that
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 > 0
    at java.util.Vector.insertElementAt(Unknown Source)
    at java.util.Vector.add(Unknown Source)
    at book.insertbook(xing.java:6)
    at xing.main(xing.java:72)
    The javadoc if this Vector.add(int index, E element) says like this:

    Inserts the specified element at the specified position in this Vector.
    Shifts the element currently at that position (if any) and any
    subsequent elements to the right (adds one to their indices).

    @param index index at which the specified element is to be inserted
    @param element element to be inserted
    @throws ArrayIndexOutOfBoundsException if the index is out of range
    ({@code index < 0 || index > size()})
    You get the ArrayIndexOutOfBoundsException because the index you've specified as the add() method argument is out of range. At that time your Vector size is 0 and you tried to add to index 1. Your add should start with index 0. Or you can add without defining an index which will make your code simpler.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Hello,Can you give me some advice of my program?

    Still don't see why not just using the basic add()...but I just saw why the index is wrong.
    You're using the size of the newspaper, not book, array.

    This is a pretty good example of why meaningful variable names are helpful.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. A program that give change
    By Nixter1997 in forum New To Java
    Replies: 4
    Last Post: 08-23-2012, 02:42 PM
  2. Just some advice on my first program in java.
    By allen1952 in forum New To Java
    Replies: 4
    Last Post: 12-22-2010, 02:32 PM
  3. Replies: 1
    Last Post: 08-13-2010, 08:13 AM
  4. Replies: 1
    Last Post: 07-07-2010, 09:25 AM
  5. [Help] Change to give back program
    By Duranx in forum New To Java
    Replies: 5
    Last Post: 02-06-2010, 03:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •