Java, Searching through array question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benyboy
    New Member
    • Mar 2008
    • 3

    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.leng th; 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.leng th; row++)
    {
    for (col=0; col<matrix[row].length; col++)
    outFile.print(m atrix[row] [col].toUpperCase() + System.getPrope rty("line.separ ator"));
    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.prin tln("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
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    If you were doing it by hand how would you do it? Now how would you turn that into code?

    Comment

    • benyboy
      New Member
      • Mar 2008
      • 3

      #3
      ahh im not sure i seem to be dreaming about code ive seen that much of it.

      I Just want the program to output to file all the book titles by the authors with a given surname.

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        If you are trying to search for the books with respect to it's author....

        It is better to have a file that was named by that author...

        And that file should have the contents ( books ) made by that author....

        If the user likes to choose JUDITH BISHOP, just copy the name from the inputfile, and try to search that name in the directory....(t reat it as a name of a file )

        If successful, then retrieve what that file has....

        for example, you have

        SNOW
        BISHOP
        ROBERTS

        and i have also files named

        SNOW.txt
        all books that was made by snow
        This........... ............... ............... .

        BISHOP.txt
        all books that was made by Bishop
        This........... ............... ............... .

        ROBERTS.txt
        all books that was made by Roberts
        This........... ............... ............... .


        If the user were to choose ROBERTS, you would simply seek the file (ROBERTS.txt) if exists.. Then retrieve that file and show its content...

        You must set those files, name it in author, add contents to it, and save....
        That is you reference for browsing the books of the author...

        Sukatoa...

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by benyboy
          ahh im not sure i seem to be dreaming about code ive seen that much of it.

          I Just want the program to output to file all the book titles by the authors with a given surname.
          Get a night's sleep then try again. How would you do it by hand?

          Comment

          • benyboy
            New Member
            • Mar 2008
            • 3

            #6
            Originally posted by BigDaddyLH
            Get a night's sleep then try again. How would you do it by hand?
            i did it !!!!!

            hard work pays off after all !!

            Comment

            Working...