2017-04-12 85 views
0

我試圖將用戶的名字和姓氏與文本文件中的名稱進行比較,如果匹配,則會生成輸出。如果名稱不匹配,則程序出錯。當我運行該程序時,我不斷收到錯誤「線程中的異常」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"); 
      } 

     } 


    } 



    } 
+0

您無意中標記了這個問題的c#。我刪除了標籤。 –

+0

你爲什麼對演員感到困擾? '新的掃描儀(新的文件(「...」))'是非常好的。 – Makoto

+3

'java.io.File不能轉換爲java.lang.Readable' ...'(可讀)新文件(UserFile)'...現在這裏還不清楚什麼?真的,我不明白。 – Tom

回答

1

您正在將文件轉換爲可讀。

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

刪除它並用try和catch來包圍語句。

Scanner inputStream = null; 
try 
{ 
    inputStream = new Scanner(new File(UserFile)); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
finally 
{ 
    if(inputStream != null) 
    { 
     inputStream.close(); 
    } 
} 
+1

歡迎來到2017:https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – Tom

+0

我確實改變了這一點。謝謝,但我得到另一個錯誤,說:「線程」主「異常」java.lang.ArrayIndexOutOfBoundsException:1 \t at ReadFile.main(ReadFile.java:38)「 – Becca

+0

@Becca你試過找出這是什麼例外的意思是?顯然不是:[什麼導致java.lang.ArrayIndexOutOfBoundsException,我該如何防止它?](// stackoverflow.com/q/5554734) – Tom

1

更改線路

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

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

看來你的文件是不同的格式比預期的。 更改 inputStream.useDelimiter(「[\ r,]」); 至: inputStream.useDelimiter(「\ n」); 所以你的分隔符將是換行符。

此外,您正在閱讀第一行,並忽略它。這是否有意(有些標題)?如果是,你的文件應該至少有兩行文件。

還要取消行,所以你會讀一個以上的線

//String line = inputStream.next(); 

嘗試在調試器中運行它,你會看到在代碼中會發生什麼。

+0

我確實改變了這一點。謝謝,但我得到另一個錯誤說:「線程」主「java.lang中的異常。ArrayIndexOutOfBoundsException:1 \t at ReadFile.main(ReadFile.java:38)「 – Becca

0

異常是人類可讀的 - 你試圖讓unapropriate類轉換,再加上錯誤的行會爲你提供了ReadFile.java:24

您可以刪除你的這個代碼轉換或開始使用新功能語言如流API,λ,嘗試與 - 資源等等,像下面

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(); 
    } 

另外,您將得到ArrayIndexOutOfBoundsException異常,如果你不劈裂

0123後檢查arrayName中的大小
相關問題