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");
}
}
}
}
new Scanner(new File("..."))is perfectly fine.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.