1

I'm trying to compare the user first name and last name to the names in the text file and if it match then i produce an output.If the names don't match then the program gives an error. When i run the program, i keep getting the error "Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to java.lang.Readable at ReadFile.main(ReadFile.java:24)"

public class ReadFile {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        @SuppressWarnings("resource")
        Scanner Input = new Scanner(System.in);
        System.out.print("Enter First Name:");
        String FirstName = Input.nextLine();

        @SuppressWarnings("resource")
        Scanner Input1 = new Scanner(System.in);
        System.out.print("Enter Last Name:");
        String LastName = Input1.nextLine();

        String UserFile = "UserFile.txt";
        @SuppressWarnings("resource")
        //BufferedReader inputStream =new BufferedReader(new FileReader("myfile.txt")); 
        Scanner inputStream = new Scanner((Readable) new File(UserFile));


        String line = inputStream.nextLine();
        inputStream.useDelimiter("[\\r,]");
        while (inputStream.hasNext())
        {
            //contains name
        //String line = inputStream.next();

            //split names
             String [] arrayName = line.split(",");
             String FName = arrayName[0];
             String LName = arrayName[1];           

             if(FirstName.equals(FName) && LastName.equals(LName)) 
             {
                 System.out.println("You are Logged IN");
             }

             else 
             {
                 System.out.println("You need to create a new account");
             }

        }


    }



    }
3
  • You accidentally tagged this question c#. I removed the tag. Commented Apr 12, 2017 at 15:32
  • Why are you bothering with the cast at all? new Scanner(new File("...")) is perfectly fine. Commented Apr 12, 2017 at 15:34
  • 3
    java.io.File cannot be cast to java.lang.Readable ... (Readable) new File(UserFile) ... now what is unclear here? Really, I don't get it. Commented Apr 12, 2017 at 15:34

3 Answers 3

1

You're casting a File to Readable.

Scanner inputStream = new Scanner((Readable) new File(UserFile));

Remove it and surround the statement with try and catch.

Scanner inputStream = null;
try
{
    inputStream = new Scanner(new File(UserFile));
}
catch(Exception e)
{
    e.printStackTrace();
}
finally
{
    if(inputStream != null)
    {
        inputStream.close();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I did change that. Thanks but i'm getting another error which says ":Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ReadFile.main(ReadFile.java:38)"
@Becca And have you tried find out what this exception means? Obviously not: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
1

Change the line

Scanner inputStream = new Scanner((Readable) new File(UserFile));

to

Scanner inputStream = new Scanner(new FileReader(UserFile));

Seems that your file is in different format than expected. Change inputStream.useDelimiter("[\r,]"); to: inputStream.useDelimiter("\n"); So your delimiter will be newline character.

Besides you are reading first line and ignoring it. Is it on purpose (some header)? If yes your file should have at least two lines in your file.

Also uncomment the line, so you will be reading more that one line

//String line = inputStream.next();

Try running it in debugger so You will see what happens in code.

1 Comment

I did change that. Thanks but i'm getting another error which says ":Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ReadFile.main(ReadFile.java:38)"
0

Exception is human readable - you are trying to make unapropriate class cast, plus row with error was provided for you ReadFile.java:24

You can remove this cast from your code or start using new features of language such as Streams API, lambda, try-with-resources and so on, like below

try (Stream<String> stream = Files.lines(Paths.get(UserFile))) {
    stream.forEach(line -> {
        String[] arrayName = line.split(",");
        if (arrayName.length >= 2) {
            String FName = arrayName[0];
            String LName = arrayName[1];

            System.out.println(FirstName.equals(FName) && LastName.equals(LName)
                    ? "You are Logged IN" : "You need to create a new account");
        }
    });
} catch (IOException e) {
        e.printStackTrace();
    }

Plus you will got ArrayIndexOutOfBoundsException if you will not check size of your arrayName after spliting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.