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.


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

Results 1 to 3 of 3

Thread: Scanner with problem

  1. #1
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    My Mood
    Thanked 0 Times in 0 Posts

    Default Scanner with problem

    Hello guys
    Here I am again, now with a problem in I/O.

    The objective of this program is to input a word in the terminal/console and then the scanner would search the word in a specific txt file and would return with the result if the word exists or not.
    import comp102x.IO;
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class SearchRecordDemo
        // for loading OCR libraries
        private static Loader loader = new Loader();
         * Askes user for an input word and searches the word from a record.
         * Prints whether the word exist in the record or not.
        public void searchWord() throws IOException
            // Input from console
            String word = IO.inputString();
            /* Input from image by performing Optical Character Recognition(OCR)
            //String word = IO.inputTextImage();
            word = removeExtraSpace(word);
            boolean exist = searchFromRecord("record.txt", word);
            if (exist) {
                System.out.println("The word \"" + word + "\" is in the record.");
            } else {
                System.out.println("The word \"" + word + "\" is not in the record.");
         * Removes the extra spaces from a word.
         * @param word The String to be processed.
        private String removeExtraSpace(String word)
            return word.replace("\n", "").replace("[ ]{2, }", "");
         * Searches the record and returns if a specified word is in the record.
         * @param recordName The name of the record text file.
         * @param word The word to be searched.
         * @return true if the word presents in the record, false otherwise.
        [B]private boolean searchFromRecord(String recordName, String word) throws IOException[/B]
       //method with problems
       Scanner scanner=new Scanner("record.txt");
         return true;
     // not found
     return false;

    After running the method searchWord(), I input in the terminal the word "ME" (it exists in the text file) and it return with false statement. Actually it return false with any word I input.
    Debugger is saying that recordName="record.txt". It's correct, no ? recordName is the variable that will store the words that exists in the text file.
    I need help

  2. #2
    Senior Member
    Join Date
    Jul 2013
    Thanked 121 Times in 105 Posts

    Default Re: Scanner with problem

    is not how a scanner works.

    What you do here is to check whether the String referenced by "word" is contained within the String referenced by "recordName".
    But the Variable recordName is always "record.txt". Its the argument you use to call the method.

    You have to actually use the Scanner object that you have created. Right now you create it and never do anything with it.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: Scanner with problem

    Also you have set up the Scanner object to read the text "record.txt" and not the file called record.txt.
    Improving the world one idiot at a time!

Similar Threads

  1. Scanner problem
    By RonW in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 14th, 2014, 01:11 AM
  2. Scanner problem
    By melki0795 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2013, 05:13 AM
  3. Problem with Scanner
    By Rafal75 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 7th, 2012, 08:34 PM
  4. Scanner problem - need help
    By destructobob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 23rd, 2011, 02:08 AM
  5. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM