Java: Error when reading a file

All,

I'm trying to read and output the contents of a file called credentials.txt. The code is below and attached is the error I'm getting.

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

public class TestingRandomMethods {

   
    public static void main(String[] args) throws IOException {    //I had an error until I added "throws IOException" when trying to open the file.
         
        FileInputStream fileByteStream = null;
        Scanner credFile = null;
        String credData = "";

        //Attempt to open file
        fileByteStream = new FileInputStream("credentials.txt");
        credFile = new Scanner(fileByteStream); //credFile now contains what's in credentials.txt.
        System.out.println("Reading and printing data.");
        credData = credFile.next(); //credData now contains what's in credentials.txt as well. It allows me to read through the file.
       
        while (credData.charAt(0)){
            System.out.println(credData);
           
            credData = credFile.next();
        }
        System.out.println(credData);
       
        //Close the file
        System.out.println("Closing the credential.txt file.");
        fileByteStream.close();
        return;
    }
   
}

ErrorMessage.png
LVL 1
isamesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
No need to post a new question. This would have come out from your last one
You need to look at examples of using a Scanner.

while(credData.charAt(0))

Open in new window

won't compile because the method of String that you're calling returns char, not boolean. Unlike other languages, conversion to  boolean is not possible

while(credFile.hasNext())

Open in new window

is the sort of thing you should be using

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.