Results 1 to 6 of 6
  1. #1
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default Help printing specific ArrayList elements

    Hello everyone,

    This should be an easy question for th pro's to answer.

    I have an applciation where a user enters a book title, author name, publisher, ISBM#, and book price. This is then converted into a single String and stored in an ArrayList.

    Here is the code for doing just that.

    public class Book {

    // ArrayList to store Book objects
    ArrayList bookList = new ArrayList();
    ArrayList bookListB = new ArrayList();
    Scanner userInput = new Scanner(System.in);

    // variables to store Book information
    private String bookTitle, firstName, lastName, publisher;
    private long ISBM;
    private double price;
    private int index;

    // no argument constructor calls 6 argument constructor with default values
    public Book(){
    this("", "", "", "", 0, 0.00, 0);
    }// end 0 argument constructor Book()

    // begin six argument Book constructor
    public Book (String bookTitle, String firstName, String lastName,
    String publisher, long ISBM, double price, int index){

    this.bookTitle = bookTitle;
    this.firstName = firstName;
    this.lastName = lastName;
    this.publisher = publisher;
    this.ISBM = ISBM;
    this.price = price;
    this.index = index;
    }// end six argument Book constructor

    // begin addBookRecord method
    public void addBookRecord(){

    // variable to hold all 6 book entries into one final string
    String stringLine;

    // Ask user to enter book title and adds it to bookTitle variable with
    // a text space at the end for print formatting
    System.out.println("Please enter the book title");
    bookTitle = userInput.next() + " ";

    // Ask user to enter author first name and adds it to firstName
    // variable with a text space at the end for print formatting
    System.out.println("Please enter the author's first name");
    firstName = userInput.next() + " ";

    // Ask user to enter author last name and adds it to lastName
    // variable with a text space at the end for print formatting
    System.out.println("Please enter the author's last name");
    lastName = userInput.next() + " ";

    // Ask user to enter book publisher and adds it to publisher
    // variable with a text space at the end for print formatting
    System.out.println("Please enter the publisher");
    publisher = userInput.next() + " ";

    // Ask user to enter ISBM number and adds it to ISBM
    // variable with a text space at the end for print formatting
    System.out.println("Please enter 10 digit ISBM number");
    ISBM = userInput.nextLong();

    // Converts Long to String
    String ISBMString = Long.toString(ISBM) + " ";

    // Ask user to enter book price and adds it to price
    // variable with a text space at the end for print formatting
    System.out.println("Please enter the book price");
    price = userInput.nextDouble();

    // Converts Double to String
    String priceString = Double.toString(price) + " ";


    // adds all six user input values and creates one single String line
    stringLine = bookTitle + firstName + lastName + publisher +
    ISBMString + priceString;

    // adds the new line of text to the arrayList
    bookList.add(stringLine);

    }// end method addBookRecord

    // begin printTotal method
    public void printTotal(){

    // prints the toal amount of objects within the ArrayList
    System.out.print("Initial size of book List : " + bookList.size());
    System.out.print("\n");
    }// end method printTotal

    // begin printBooks method
    public void printBooks(){

    // prints all books stored within the arrayList
    Iterator itr = bookList.iterator();
    while(itr.hasNext())
    System.out.println(itr.next());
    System.out.println();
    }// end method printBooks

    // begin searchIndex method
    public void searchIndex(){

    int userIndex;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter the index number you wish to search");
    userIndex = scanner.nextInt();




    }// end method searchIndex
    }// end Book class


    Look at my last method. It allows a user to enter an index number and then sets this input to the String variable "userIndex"

    How can I have the ArrayList take the userInput value, search the ArrayList for this index and then display the elements of that index?

    In other words, a user enters a 1 or a 2 and presses enter, the method searches the ArrayList for index 1 or 2 (depending on which number was entered) and then prints the element of that index to the screen.

    Thanks in advance for any help!

  2. #2
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Disregard the booklistB. It is not in use only bookList

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    sort the list and/or use a search algorithm based on the index. sorry, but no automatic solution to this.

  4. #4
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    ok ill look into that, thanks!

  5. #5
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Thanks everyone,

    I figured it out. the final method should look like this

    int userIndex;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter the index number you wish to search");
    userIndex = scanner.nextInt();
    System.out.println(bookList.get(userIndex));

    If a user enters an index number that does not exist it errors but if a user enters an index that does exist it succesfully prints the element of that index.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    are you trying to get by the index in the array or the index field of the object? i was assuming the latter. if you only wanna get the i'th element of the array, that is fine. but if you wanna get it based on the index field of the object, you're gonna have to do more than that. and if the two are equivalent, then i guess it isn't that much of a problem here

Similar Threads

  1. Reading specific lines
    By ivvgangadhar in forum New To Java
    Replies: 8
    Last Post: 01-12-2009, 08:53 AM
  2. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 07:47 AM
  5. Rejecting connections from a specific IP
    By javaplus in forum Networking
    Replies: 2
    Last Post: 12-21-2007, 02:28 PM

Posting Permissions

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