Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Searching a text file using Java

Options
  • 02-02-2004 11:43am
    #1
    Registered Users Posts: 10,148 ✭✭✭✭


    I need to scan through a text file checking for the first number on each line and comparing it to a number that the user has already inputted. The text file has the format . . .

    1 Blah Blah Blah
    2 Blah Blah Blah
    3 . . ... . . . . . .. . . . . . etc

    I've got no problem getting the user input, it's just scanning each line, to check if the number the user entered is equal to the one at the begining of each line.

    Can anyone help?


Comments

  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Show your code, buddy :)


  • Registered Users Posts: 10,148 ✭✭✭✭Raskolnikov


    [PHP] private void buyCd()
    {
    try {
    FileInputStream fis = new FileInputStream("cds.txt");
    int n;
    while ((n = fis.available()) > 0) {
    byte[] b = new byte[n];
    int result = fis.read(b);
    if (result == -1) break;
    String s = new String(b);
    System.out.print(s);
    } // End while
    } // End try
    catch (IOException e) {System.err.println(e);}
    System.out.println();

    readValue();

    RandomAccessFile raf = new RandomAccessFile("cds.txt","rw");
    StringBuffer beforeDelete = new StringBuffer();
    boolean found = false;
    while(raf.getFilePointer() != raf.length())
    {
    String compareAgainst = raf.readLine();

    if(compareAgainst.equals(instr))
    {
    found = true;
    toDelete = compareAgainst.length() + 1;
    }
    else
    {
    beforeDelete.append(compareAgainst + "\n");
    }
    }
    }[/PHP]


  • Registered Users Posts: 10,148 ✭✭✭✭Raskolnikov


    You can ignore the whole random access file bit after readValue(); that was just my attempt at finding the value, hasn't worked for me and there is probably an easier way.


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Originally posted by Raskolnikov
    I need to scan through a text file checking for the first number on each line and comparing it to a number that the user has already inputted. The text file has the format . . .

    1 Blah Blah Blah
    2 Blah Blah Blah
    3 . . ... . . . . . .. . . . . . etc

    I've got no problem getting the user input, it's just scanning each line, to check if the number the user entered is equal to the one at the begining of each line.

    Can anyone help?

    Before i go on... let me get this straight, all you want to do is get the content of the line, who's line number == user input? Or do you just want to check that a line with that number exists? (i.e 1,2,3...(n)).


  • Registered Users Posts: 10,148 ✭✭✭✭Raskolnikov


    Originally posted by sjones
    Before i go on... let me get this straight, all you want to do is get the content of the line, who's line number == user input? Or do you just want to check that a line with that number exists? (i.e 1,2,3...(n)).

    I just want to check the number at the start of each line, if the number matches the user input, I want the line in the text file returned, else return an error.

    1 Boyzone Love Me For a Reason ISland Records 10 Pop 19.99
    2 NWA Straight Outta Compton Nigaa Records 20 Rap 10.99
    3 King Crimson ThrakAttack Island Records 2 Rock 0.99
    4 Roger Taylor I'm A Git Self-Released 1000 PapRock 10.99

    That's what gets printed out to the user, they select the number of the cd they want, I need to then search the cds.txt to match them.


  • Advertisement
  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    2nd year UCC by any chance?

    Here's the pseudocode:
    foreach line in the file
       if line startsWith userInput
          return line
    

    Use a StringTokenizer then to split up the line into it's own parts


  • Registered Users Posts: 10,148 ✭✭✭✭Raskolnikov


    Yep.

    Found a solution, thanks anyway.


  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    Doesnt anyone use BufferedReader these days?


Advertisement