0

here is what i have so far. the problem i am faceing is how do i find the number of elemets in the file so i can initialze the queue. Your suggestions will b most appritated.

class FileHandler {
BufferedReader data;
DataInputStream in;

    public FileHandler(String fileName) {
    try {
        data = new BufferedReader(new FileReader(fileName));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public FiFo fileToLines() {

///////////////////here is where i need ur help  whats teh string size////////////////////

    private FiFo lines=new FiFo(data.)
        String line = "";
    try {
        while ((line = data.readLine()) != null) {
            lines.add(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }*/
    return lines;
}

public void closeFile() {
    try {
        data.close();

    }

    catch (Exception e) {
        e.printStackTrace();
    }
}

public class FiFo {

     private int index;
     private String[] queue;

     public FiFo(int size)
{
          queue=new String[size];
         index=0;

}

public void add(String element)
{
    queue[index]=element;
    index++;
}
public void remove()
{
    int temp=0;
    while(temp!=index)
    {
        queue[temp]=queue[temp+1];
        temp++;
    }
    index--;
}

public String get()
{
    return queue[0];
}

public int size()
{
    return index;
}

public void printQueue()
{
    for (int i=0;i<=index;i++)
        System.out.println(queue[i]);
}
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
Shaz
  • 193
  • 1
  • 3
  • 7
  • Your formatting is very badly messed up. Could you try editing your question, paste your code in again, and then select it and press the "Code Sample" button which reads `101010`? It looks like you pressed the blockquote button which doesn't do what you intended. – Brian Campbell Feb 10 '10 at 05:01
  • i am sorry i am new to this...i have tried to refomat it....leme know if it still needs work – Shaz Feb 10 '10 at 05:11
  • I don't mean to be a grammar nazi, but I think people will appreciate your question more if you take the time to spell the whole words ("be" instead of "b", "let me" instead of "leme" etc.). If you show that you put some effort into the question, people will be more inclined to put some effort into answers. – Eli Acherkan Feb 10 '10 at 08:04

2 Answers2

0

Why do you want to implement your Fifo class ? Is it homework ?

Why wouldn't you use this class :

public Queue fileToLines() {

    LinkedList<String> lines = new LinkedList<String>();


    String line = "";
    try {
        while ((line = data.readLine()) != null) {
            lines.add(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }*/
    return lines;
}

With this one, you don't have to know the number of lines in the file.

Il you really want to have the count of lines in a file, see this topic.

Community
  • 1
  • 1
Laurent K
  • 3,503
  • 3
  • 30
  • 36
  • In that case, you could implement a queue for which it's not necessary to know the initial size. Initialize your queue with a size of (let's say) 100, then each time you add an item, check if the size is big enough. If not, add more size to your Array. (create a new storage array, and use java.util.Arrays.copy to copy the old into the new) – Laurent K Feb 10 '10 at 07:15
0

You have no way of understanding how many lines are in a file when you open it. One thing you can do is to is add all lines to a List, Your FiFo can take that list as its constructor param and use it as its backing data.

akf
  • 38,619
  • 8
  • 86
  • 96
  • thanks can suggest thats its better to amke the String Array in FiFo private or make it public cuz i also have to compare 2 quese and find teh common elements – Shaz Feb 10 '10 at 05:27