-1

Searching txt in a file using java

This is my txt file

"hhhead":"Melvin","respondent":"Edlen"

I Want to search hhhead and program will return Melvin

how can I do that?

gprathour
  • 14,813
  • 5
  • 66
  • 90
Shaine
  • 80
  • 1
  • 10
  • You could easily use `String.split(",")` and a `Map` for the data. What have you tried so far, code wise? – vikingsteve Jun 15 '17 at 11:39
  • nothing so far. i just want to think before i code. – Shaine Jun 15 '17 at 11:41
  • "hhhead":"Melvin","respondent":"Edlen","int_date":"2017-06-15","start_time":"09:46","interviewer":"Kit","house_type":"1","nbr":2,"nstorey":1,"roof":"1","wall":"2","floor":"7","nnucfam":1,"phsize":5,"hpq_mem":[{"memno":"1","msname":"Malabarbas","mfname":"Melvin","mmname":"Pirneto","reln":"1","reln_o":"","nucfam":"1","sex":"1","birth_date":"1969-12-18" – Shaine Jun 15 '17 at 11:41
  • heres more. all i want is that if i search hhhead it will return Melvin and if i search respondent it will return Edlen. – Shaine Jun 15 '17 at 11:42
  • @Shaine Have a look at [this](https://stackoverflow.com/a/5600442/599528) and [this](https://stackoverflow.com/a/15577756/599528) – Jacob Jun 15 '17 at 11:57
  • can u help me with this? – Shaine Jun 15 '17 at 11:57
  • thanks.. i know how to search hhhead but dont know how to return melvin. – Shaine Jun 15 '17 at 12:00
  • @Shaine Whatever code you have written, copy and paste here by editing your question and then ask where exactly you are stuck rather than asking solution. – Jacob Jun 15 '17 at 12:06
  • `public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) { String sCurrentLine; String Seach="hhhead"; while ((sCurrentLine = br.readLine()) != null) { if(sCurrentLine.contains(Seach)){ System.out.println("hhhead FOUND!!"); } } } catch (IOException e) { e.printStackTrace(); } }` – Shaine Jun 15 '17 at 12:13
  • Share your effort – PRATHAP S Jun 15 '17 at 12:21

3 Answers3

2

I assume you have the code ready to read the .txt file and write into a string.

In the below code, str is the string having text file content.

String str = "\"hhhead\":\"Melvin\",\"respondent\":\"Edlen\",\"int_date\":\"2017-06-1\u200C\u200B5\",\"start_time\":\"09:\u200C\u200B46\",\"interviewer\":\"K\u200C\u200Bit\",\"house_type\":\"1\"\u200C\u200B,\"nbr\":2,\"nstorey\":1\u200C\u200B,\"roof\":\"1\",\"wall\":\"\u200C\u200B2\",\"floor\":\"7\",\"nnuc\u200C\u200Bfam\":1,\"phsize\":5,\"h\u200C\u200Bpq_mem\":[{\"memno\":\"1\u200C\u200B\",\"msname\":\"Malabarb\u200C\u200Bas\",\"mfname\":\"Melvin\u200C\u200B\",\"mmname\":\"Pirneto\"\u200C\u200B,\"reln\":\"1\",\"reln_o\"\u200C\u200B:\"\",\"nucfam\":\"1\",\"se\u200C\u200Bx\":\"1\",\"birth_date\":\u200C\u200B\"1969-12-18\"";
        Map<String,String> map = new HashMap<String,String>();
        String[] split = str.split(",");
        for(String s: split){
            String[] split2 = s.split(":");
            map.put(split2[0], split2[1]);
        }

        System.out.println(map.get("\"hhhead\""));

I copied the data from your comments and had to use backslash("\") to escape string, used split method having "," as an argument and stored in to a key-value map. If you search with the key, "\"hhhead\"" you get "Melvin".

Hope this helps!

harshavmb
  • 3,404
  • 3
  • 21
  • 55
0

You can read the file and parse the data.

Then you can store the data in key-value pairs like a Map then you can easily get it.

gprathour
  • 14,813
  • 5
  • 66
  • 90
  • 1
    @Shaine We are not here to write code for you. If you face any problem then people might try to help you. Don't ask for code without putting your own efforts. – gprathour Jun 15 '17 at 11:45
0
public static void main(String[] args) {

    try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {

        String sCurrentLine;
                    String Seach="hhhead";
        while ((sCurrentLine = br.readLine()) != null) {
                        if(sCurrentLine.contains(Seach)){
            System.out.println("hhhead FOUND!!");
                        }
        }

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

}
Shaine
  • 80
  • 1
  • 10
  • in System.out.println("hhhead FOUND!!"); – Shaine Jun 15 '17 at 12:15
  • With all due respect, answers are for solutions to your question. So what you should do is copy and paste your code by editing the question and should delete this answer. – Jacob Jun 15 '17 at 12:18