Searching Arrays

Hi

Ive just created a programme that models a file system. I used arrays instead of linked lists etc.

The problem im having is i would like the end user to be able to search (ie company ID) and get the entire company details returned however im unable to think of the method to do this.

Can anyone help?

Thanks

[341 byte] By [Komey999a] at [2007-10-2 5:30:17]
# 1
What exactly is your data structure. Do you have an array of Company objects? If so, what is the code of the Company class?
jbisha at 2007-7-16 1:31:48 > top of Java-index,Java Essentials,New To Java...
# 2
Have you considered Map classes, such as Hashmap?Baron Samedi
BaronSamedia at 2007-7-16 1:31:48 > top of Java-index,Java Essentials,New To Java...
# 3

Here is the code ive written, it抯 a prototype of the final project. My aim is to let the end user search via flight no and it returns the destination.

import java.io.*;

import java.util.*;

public class ReadFromAirlineFile

{

// sequential text file input of characters

static BufferedReader fileInput;

public static void main(String args[]) throws IOException

{

String inputString;

String flight,dest;

int index = 0;

StringTokenizer tokens;

// 20 flights Indicated

Airline [] airlineArray = new Airline[20];

fileInput = new BufferedReader( new FileReader("flight_info.txt"));

inputString = fileInput.readLine();

// read the file until null is returned

while(inputString != null)

{

// First you need to split the string using the String Tokenizer

tokens = new StringTokenizer(inputString);

flight = tokens.nextToken(); // I have extracted the first component

// dest = Integer.parseInt(tokens.nextToken());// I have extracted the second component

dest = tokens.nextToken();

airlineArray[index] = new Airline(flight, dest);

index++;

inputString = fileInput.readLine();

} // end of while

// close the file

fileInput.close();

System.out.println("File read and closed");

// invoid mehtod to print the objects

printAirlineArray(airlineArray);

} // end of main

// Prints the array

public static void printAirlineArray(Airline [] airlineArrayIn)

{

for(int index = 0; index < airlineArrayIn.length; index++)

System.out.println(airlineArrayIn[index].getFlight() + " " + airlineArrayIn[index].getDest());

}

} // end of class ReadFromAirlineAsciiFile

Komey999a at 2007-7-16 1:31:48 > top of Java-index,Java Essentials,New To Java...
# 4

> Here is the code ive written, it抯 a prototype of the

> final project. My aim is to let the end user search

> via flight no and it returns the destination.

> (irrelevant code snipped)

And so far you're doing absolutely nothing to fulfill any part of the search logic. Are you just sitting there waiting for someone to finish your code for you?

You were given advice earlier about using a Map (key-value pair). Sounds like the right track. The "key" to the map could be the flight number. The "value" to the map could be the Airline object.

warnerjaa at 2007-7-16 1:31:48 > top of Java-index,Java Essentials,New To Java...
# 5
Ok will try that.Thanks
Komey999a at 2007-7-16 1:31:48 > top of Java-index,Java Essentials,New To Java...
# 6

... if your homework assignment (presumably that's what this is for) prohibits using a Map, then you're left with iterating over that array until you find the flight# or exhaust the array. You need to do your part in doing your homework. You can ask specific questions if needed, but so far you're really just giving the assignment requirements and haven't done anything to work towards accomplishing it.

warnerjaa at 2007-7-16 1:31:48 > top of Java-index,Java Essentials,New To Java...