简体   繁体   中英

How to identify a string and print line in a file in java?

I am Java beginner and I am trying to find a word in the file and print out the line where it exists.
My file looks like:

dog  one 100.11
cat two 200.22
doggg  four 400.44

And if I input "dog", the output should be

dog one 100.11

But currently I have an error the word is not found.
Please Help! Thanks!

File file = new File("G:\\Eclipse\\src\\test.txt");
Scanner s = new Scanner(file);
Scanner input = new Scanner(System.in);
System.out.println("Input:");
String name = input.next();
while (s.hasNext()) {
  String line = s.nextLine();
  String name = s.next();
  if (name.equals(name)) {
    System.out.println(line);
  }
}

The answer is to use a HashMap.

Declare your HashMap as

HashMap<String, String> map = new HashMap<String, String>();

Then, go through your string and add the first word to the map.

while (s.hasNext()) {
    String current = s.nextLine();
    String[] currentWords = current.split(" ");
    map.put(currentWords[0], current);
}

Then, when you read the string, simply put

System.out.println(map.get(line));

To find if there is single word in sentence use contains()

if (name.contains(input)) {
    System.out.println(line);
}

Right now you are checking if dog is equals to dog one 100.11

In case you have more then one line who can fit dog you can split the sentence and check if the first string matches

String[] words = line.split(" ");
if (words[0].equals(input)) {
    System.out.println(line);
}

Corrected one.

File file = new File("G:\\Eclipse\\src\\test.txt");
Scanner s = new Scanner(file);
Scanner input = new Scanner(System.in);
System.out.println("Input:");
String in = input.next();
while (s.hasNext()) {
    String line = s.nextLine();
    if (line.contains(in)) {
        System.out.println(line);
    }
}

Since Guy's solution gives you "doggg four 400.44", too. Just add a " " to the input.

if (name.contains(input + " ")) {
    System.out.println(line);
}

Although this is quite inefficient, it works for a small programm.

However, this also finds "heres a text dog one 11111" and wont find "one 100.11 dog"

EDIT: Just saw, Guy's edit. His solution is better.

It looks like your code needs yet another while loop to scan each word in a line

// Get user input
Scanner input = new Scanner(System.in);
System.out.print("Input:");
String name = input.next();
// Get lines from file
File file = new File("G:\\Eclipse\\src\\test.txt");
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNext()) {
  String line = fileScanner.nextLine();
  // scan each word in a line
  Scanner lineScanner  = new Scanner(line);
  while (lineScanner.hasNext()) {
    String word = lineScanner.next();
    if (name.equals(word)) {
      System.out.println(line);
      break; // we done with the current line
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM