Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java - searching through array question

    hi, after hours of staring at my computer screen, and browsing through various internet forums i am totally stuck!

    I have created the following array which reads from an input file the following list of book titles followed by author then book title author etc


    Concurrent programming ---- book title
    C. R. Snow ---- author
    Pascal Precisely ----book title
    Judy Bishop ---- author etc
    THINKING RECURSIVELY WITH JAVA
    ERIC ROBERTS
    Concurrent Programming
    Stephen J. Hartley
    Java Gently
    Judith Bishop
    Petri Nets
    Wolfgang Reisig
    Finite Transition Systems
    Andre Arnold
    Data Structures and Problem Solving Using Java
    mark allen weiss



    this is the array i have created which reads from the input file


    int row, col;
    String [] [] matrix = new String [numberOfBooks] [2]
    for (row=0; row<matrix.length; row++)
    {
    for (col=0; col<matrix[row].length; col++)
    matrix[row] [col] = inFile.nextLine();
    }


    when using the following piece of code i am able to print the array

    for (row=0; row<matrix.length; row++)
    {
    for (col=0; col<matrix[row].length; col++)
    outFile.print(matrix[row] [col].toUpperCase() + System.getProperty("line.separator"));
    outFile.println();
    }

    when using this code the array is printed as follows which is book title followed by author


    CONCURRENT PROGRAMMING
    C. R. SNOW

    PASCAL PRECISELY
    JUDY BISHOP

    THINKING RECURSIVELY WITH JAVA
    ERIC ROBERTS

    CONCURRENT PROGRAMMING
    STEPHEN J. HARTLEY

    JAVA GENTLY
    JUDITH BISHOP

    PETRI NETS
    WOLFGANG REISIG

    FINITE TRANSITION SYSTEMS
    ANDRE ARNOLD

    DATA STRUCTURES AND PROBLEM SOLVING USING JAVA
    MARK ALLEN WEISS



    i am now very stuck i need to be able to allow the user to enter the authors surname and then the program will display all books by this author to the outfile (there may be more than one book by the author)

    i have started with the following


    String AuthorSurname;
    System.out.println("please enter an author's surname to view available books"); AuthorSurname = Keyboard.next();

    i now need some sort of code which will allow me to display all the books by a particular author.

    i very much appreciate any help,
    thanks

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    As this is clearly a homework assignment, i will give some hints

    The 2 dimensional array matrix holds 2 columns. The second column (matrix[row][1]) holds the name of the author. You can check if the given surname exists in this column. If so, then print the record as you do already. If not, then do nothing. The only thing then is when someone types a surname which can also occur as a first name, there the logic will fail.
    I am the luckiest man in the world


  •  

    Posting Permissions

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