#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Converting Scanner class to a string- reading data from file


    Hello!
    I have an assignment that I need to input a string (User), and then open a file, read from that file and then check if that file contains the String User. If it does, I also have to count how many times it occurs.

    What I am getting stuck on is while it is reading the file, comparing it to User. I believe I need to covert the input file from scanner to string, but my question is how. I keep running into incompatible file types when I want to compile.
    Code:
    while (inputFile.hasNextLine())
             {
              String STR2 = inputFile; //where I tried to get the input file to be a string. 
                   if( STR2.equalsIgnoreCase(User))
                   {
                      return true;
                   }
                   else
                   return false;
    }
            //close file
            inputFile.close();
            }//end of static
      }//end of class
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    If inputFile is an instance of the Scanner class, you need to read the API doc for the Scanner class to see what method you want to use to read with.

IMN logo majestic logo threadwatch logo seochat tools logo