我試圖將用戶的名字和姓氏與文本文件中的名稱進行比較,如果匹配,則會生成輸出。如果名稱不匹配,則程序出錯。當我運行該程序時,我不斷收到錯誤「線程中的異常」main「java.lang.ClassCastException:java.io.File無法在ReadFile.main上轉換爲java.lang.Readable (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");
}
}
}
}
您無意中標記了這個問題的c#。我刪除了標籤。 –
你爲什麼對演員感到困擾? '新的掃描儀(新的文件(「...」))'是非常好的。 – Makoto
'java.io.File不能轉換爲java.lang.Readable' ...'(可讀)新文件(UserFile)'...現在這裏還不清楚什麼?真的,我不明白。 – Tom