[Solved] I need to re-update the old text file from new text file JAVA


As per my comments,

  • get user interaction out of the search method, in other words, avoid using System.in and System.out (except for temporary use for debuggin).
  • Instead, pass in a String cnic parameter, so that other code that has user interaction can query information from the user, get the desired cnic String and pass it into this method.
  • don’t output the results in System.out.println(…) fashion, but rather return the result to the calling code either as a String or as a new object.
  • A big bug — you’re calling br.readLine() and discarding/ignoring the result returned, i.e., you’re not assigning the String returned into a String: line = br.readLine(); except once before the loop.
  • I would read in the line within the while loop condition: String line = "" and then while ((line = br.readLine()) != null) {
  • You probably want to use the String contains(…) method in there.

Pseudo-code to explain the steps. There really is no need at this stage to use index of, since all you need to do is call contains(...) on each line of String read in from the BufferedReader. You will need to use indexOf(...) when parsing the line of interest to extract the data it contains however.

public search method, takes a cnic String parameter, returns a String
    declare result String, set = to ""
    open file within a try block, create BufferedReader
        declare String variable line, assign "" to it
        while loop, in condition, read from buffered reader into line, and loop while not null
            Check if line contains cnic String. No need for index of here, just contains
                if so, assign line to result String.
        end while loop
    end open file, create BufferedReader
    close BufferedReader (or use try with resources)
    return result String

end of method   

When you want to parse the line of interest, you will need to get several ints:

  • the index of plus the length of the "CNIC: " String
  • the index of the " Voter Number: " String
  • plus its length
  • And the length of the String.

Using these numbers, experiment with extracting your data… you’ll get it.

solved I need to re-update the old text file from new text file JAVA