...

View Full Version : java - searching through array question



smoothdude2005
03-12-2008, 01:04 PM
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

Roelf
03-12-2008, 03:09 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum