Reputation: 189
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");
}
}
}
}
Upvotes: 1
Views: 2971
Reputation: 1171
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
Upvotes: 0
Reputation: 268
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.
Upvotes: 1
Reputation: 131
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();
}
}
Upvotes: 1