Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Searching for a word in file

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Searching for a word in file

    Hello,

    I'm having issues with searching for a word in a file. The word is a user input from console, then it should look in a file for it. And print out the position. I'm using indexOf() for this.

    Can you please tell me whats wrong with my code? My problem is that when I insert the word and then try to look for it in the file,it doesnt work.

    this is the code for The input

     if(input.startsWith("modify"))
                {
                    // Call the modify method and pass a string array as argument which does not
                    // include "modify" and has split the remaining arguments by whitespace.
                    list();
                    System.out.println("Please insert the id of the file which you wish to modify: ");
                    Scanner scanner = new Scanner(System.in);
     
                    choose = scanner.nextLine();
     
                    System.out.println("In order to modify an event you can change one field at the time" + "\n" +
                    " write the name of the field and then write the new Value");
     
                    Changefield  = scanner.nextLine();
                    String newValue = scanner.nextLine();
     
                    modify(new  String[]{choose,Changefield,newValue} );
                    //modify(input.substring(7).split("\\s+"));
                }
     
                // Fall through case
             //   System.out.println("Illegal command: " + input);
     
            }while(!input.equals("quit"));
            // Cleanup.
            System.out.println("EventManager is terminating...");
            in.close();
        }

    And here is the code for checking the input if it's in the file:

     try {
                BufferedReader in = null;
                try {
                    in = new BufferedReader(new FileReader(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                in.readLine();
     
                System.out.println("Searching for :" + Changefield + " in file: " + file.getName());
                String line;
                while((line = in.readLine()) != null) {
                    int index = line.indexOf(Changefield);
     
                     System.out.println(" Position of " + Changefield + "is at the position: " + index);
                }
     
                Path from = Paths.get(String.valueOf(file));
                Path to = Paths.get("D:\\Uni.lu\\events\\" + id + ".events-Temp.txt");
                CopyOption[] option = new CopyOption[]{
                        StandardCopyOption.REPLACE_EXISTING,
                        StandardCopyOption.COPY_ATTRIBUTES
                };
                for (Path path : Files.copy(from, to, option)) {
     
                }
     
     
     
     
     
     
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }

    Thank you for your help,there must be sth wrong with the loop, it doesnt seems to be going into th while to check if the input exists.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Searching for a word in file

    Unable to pickup any obvious reasons for your troubles, and the fact you've never shown us the content of the file and/or if you get any exceptions, I've compiled a quick method which kind of does very similar behaviour to what you're after. Hopefully, to help yourself, you can study the method and see if it helps you understand your goal a little better.

    public String findWord(String word, String filename){
    	try(Scanner scanner = new Scanner(new File(filename))){
     
    	    int lineCount = 0;
    	    while(scanner.hasNextLine()){
                    lineCount++;
     
                    final String line = scanner.nextLine();
                    int column = line.indexOf(word);
     
                    if(column != -1){
                        return String.format("Word found at line %d, column %d", lineCount, column);
                    }
                }
            }catch(FileNotFoundException e){
                System.out.println("Oops! Something went wrong.");
    	}
     
    	return "Word not found";
    }
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Searching for a word in file

    Sorry,for not posting th content of the file, here it goes:

    Wed Jan 12 14:00:00 CET 1898(heacok
    Basically what i want it to check if the input is in the file. For example to check if 14:00 exists in the file.

    And then give the position of it.

    And about the errors. There are none

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Searching for a word in file

    Well if you've only got 1 line in the file, it is not entering the while loop because you're consuming a line before you reach the loop.

               BufferedReader in = null;
                try {
                    in = new BufferedReader(new FileReader(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                in.readLine(); //CONSUMING A LINE

    Does it work when you remove that line?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. The Following User Says Thank You to newbie For This Useful Post:

    justyStepi (October 4th, 2013)

  6. #5
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Searching for a word in file

    OOMG !! It's working..I cant believe it. That I didn't see this. Thank yoou so much I was stacked at this for such a long time..

    --- Update ---

    *unstuck

Similar Threads

  1. word count in a in a file
    By raj4322 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 20th, 2012, 09:54 AM
  2. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  3. Content in text file to MS Word file help!
    By ComputerSaysNo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 27th, 2011, 11:39 AM
  4. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  5. MS word file in Oracle
    By tandonpiyush in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 07:41 AM