1

I am trying to search a string in a file in java and this is what, I tried . In the below program I am getting output as No Data Found and I am sure that the file has the word which I am searching

import java.io.*;
import java.util.Scanner;

public class readfile {
    static String[] list;
    static String sear = "CREATE";

    public void search() {
        Scanner scannedFile = new Scanner("file.txt");

        while (scannedFile.hasNext()) {
            String search = scannedFile.next();
            System.out.println("SEARCH CONTENT:"+search);

            if (search.equalsIgnoreCase(sear)) {
                System.out.println("Found: " +search);
            } 
            else  {
                System.out.println("No data found.");
            }
        }
    }

    public static void main (String [] args) throws IOException {
        readfile read = new readfile();
        read.search();
    }
}
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
krisshi
  • 11
  • 3
  • possible duplicate of [Find specific word in text file and count it](http://stackoverflow.com/questions/13029922/find-specific-word-in-text-file-and-count-it) – Chandu9999 Sep 21 '15 at 09:27
  • Unless you've changed the Scanner instance's delimiter pattern, your tokens will still contain things like commas. Check your file; does it contain something like `Create:`? If it does, that won't match what you're looking for. Use `search.toUpperCase().contains(sear)` or `search.toUpperCase().indexOf(sear) > -1` to look for your word. – JonK Sep 21 '15 at 09:33
  • As Scanner is getting tokens in a sequence, maybe few tokens might not contain 'CREATE' but some token will definitely have it as you have mentioned, and moreover, as Tilo is pointing out, you can check using contains(), instead of equalsIgnoreCase(). – a3.14_Infinity Sep 21 '15 at 09:33

6 Answers6

0

Don't do:

search.equalsIgnoreCase(sear)

Try:

search.toUpperCase().contains(sear)

I think the search is the whole String of the File, so you never would become true with equals.

Tilo
  • 427
  • 4
  • 10
0

Ok, here is my understanding of your program.

You search in the file file.txt the word CREATE. To do so, you read each word in the file and if it is CREATE you print Found create.

The issue here is that for every word in the file, if it isn't CREATE you print No data found.

Instead you should wait for the end of the file and then if you haven't found it you will print the error message.

0

Use nextLine() instead of next() and then use split. Like this :

What's the difference between next() and nextLine() methods from Scanner class?

Difference :

next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.

nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

Use following code :

   String search = scannedFile.nextLine();
   String[] pieces = data.split("\\s+");

  for(int i=0; i<pieces.length(); i++)
  {
          if(pieces[i].equalsIgnoreCase(sear))
        {
            System.out.println("Found: " +search);
        } 
        else 
        {
            System.out.println("No data found.");
        }
   }
Community
  • 1
  • 1
Sagar Joon
  • 1,151
  • 10
  • 21
0

Try this :

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class readfile {
    static String[] list;
    static String sear = "CREATE";

    public void search() throws IOException {

        List<String> saveAllLinesForRewriting = new ArrayList<String>();

        // Need to read file line by line
        BufferedReader bufferedReader = new BufferedReader(new FileReader("file.txt"));
        String saveLine;
        while ((saveLine = bufferedReader.readLine()) != null) {
            saveAllLinesForRewriting.add(saveLine);
        }
        bufferedReader.close();

        // Check if your word exists
        if (saveAllLinesForRewriting.toString().contains(sear)) {
            System.out.println("Found: " + sear);
        } else {
            System.out.println("No data found.");
        }
    }

    public static void main(String[] args) throws IOException {
        readfile read = new readfile();
        read.search();
    }
}
FullStack
  • 615
  • 7
  • 26
0

Instead of reading file using scanner, first create a file resource to read by adding the below line

File file = new File("Full Path of file location");

before

Scannner scannedfile = new Scanner("file.txt");

and change the above line to

Scanner scannedfile = new Scanner(file);

rest your code is working fine.

amit28
  • 166
  • 8
0

The problem is that the scanner is scanning the String "file.txt" and not the file.

To fix this you have to do what amit28 says. Your finally code is as follows

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class readfile {
    static String[] list;
    static String sear = "CREATE";

    public void search() {

        File f = new File("file.txt");
        Scanner scannedFile;
        try {
            scannedFile = new Scanner(f);

            while (scannedFile.hasNext()) {
                String search = scannedFile.next();
                System.out.println("SEARCH CONTENT:"+search);

                if (search.equalsIgnoreCase(sear)) {
                    System.out.println("Found: " +search);
                } 
                else  {
                    System.out.println("No data found.");
                }
            }

        } catch (FileNotFoundException e) {
            // FIXME Auto-generated catch block
            e.printStackTrace();
        }






    }

    public static void main (String [] args) throws IOException {
        readfile read = new readfile();
        read.search();
    }
}
Antonio
  • 139
  • 1
  • 9