Reputation: 788
I have written the code for searching a string in the text file. This is the code i have tried so far.
import java.io.*;
import java.util.*;
public class testing
{
public static void main(String arg[])
{
try{
Scanner scanner=new Scanner("demo.txt");
List<String> list=new ArrayList<>();
while(scanner.hasNextLine()){
list.add(scanner.nextLine());
}
if(list.contains("Boys"))
{
System.out.print("found");
}
else
{
System.out.print("Not found");
}
}
catch(Exception e)
{
System.out.print(e);
}
}
}
I have read many questions and those question don't provide me an solution. This code search for the given string and returns "not found" even if the string is present.
the text file to search from is,
1. SPINAL ANESTHESIA AGENTS
"Little Boys Prefer Toys":
Lidocaine
Bupivicaine
Procaine
Tetracaine
2. XYLOCAINE: WHERE NOT TO USE WITH EPINEPHRINE
"Nose, Hose, Fingers and Toes"
Vasoconstrictive effects of xylocaine with epinephrine are helpful in
providing hemostasis while suturing. However, may cause local ischemic necrosis
in distal structures such as the digits, tip of nose, penis, ears.
3. GENERAL ANAESTHESIA: EQUIPMENT CHECK PRIOR TO INDUCING
“MALES”
Masks
Airways
Laryngoscopes
Endotracheal tubes
Suction/ Stylette, bougie
Can anyone suggest me what changes i can bring in this code? and what is the problem with this code?
Upvotes: 1
Views: 78
Reputation: 1087
Your code searches the list for the element "Boys" that doesnt exist. You have other, longer strings. The solution is to check every string if it contains desired word
try{
Scanner scanner=new Scanner("demo.txt");
List<String> list=new ArrayList<>();
while(scanner.hasNextLine()){
list.add(scanner.nextLine());
}
boolean has = false;
for (String str : list) {
if (str.contains("Boys")) {
has = true;
break;
}
}
if (has) {
System.out.print("found");
} else {
System.out.print("Not found");
}
}
catch(Exception e)
{
System.out.print(e);
}
If the search is the only thing you want to do, dont use lists
Upvotes: 2
Reputation: 11
First of all are you reading a line and adding it to the the list. and comparing all the as a single string with the "Boys". You need to get the list value (string) and compare that string to your string.
Upvotes: 0
Reputation: 5414
java 7 solution :
public static void main(String[] args) throws IOException {
String content = new String(Files.readAllBytes(Paths.get("demo.txt")));
System.out.println(content.contains("Boys") ? "FOUND" : "NOT FOUND");
}
Upvotes: 0
Reputation: 7494
What you are doing is to find if the List contains the string "Boys" - but what you should actually be checking for is if the List has a line that contains the terms "Boys". Since you are adding one line of the text file to the List at a time and one of these lines contains this search term, you have to retrieve every entry from the List and then check the string to see if your search term is present. Do the following instead:
while(String line : list) {
if(line.contains("Boys")) {
// Do whatever you need here
}
}
Upvotes: 0
Reputation: 37023
Instead of:
if(list.contains("Boys"))
Use:
for (String line : list) {
if(line.contains("Boys")) {
System.out.println("Line is " + line);
break;
}
}
Upvotes: 0