2017-04-17 51 views
0

我曾嘗試搜索關於此的任何可能的問題,分配全線飄紅,但沒有似乎正是我Java的閱讀文本文件和字符串

在這裏工作的一些代碼,我已經閱讀在所有文件子目錄,該方法需要的文件名來尋找和其目錄

public static Account FileRead(String fname, String dir) throws blankException { 
    File file = new File(dir + fname); 
    try { 

     Scanner sc = new Scanner(file); 

     while (sc.hasNextLine()) { 

      String type = sc.nextLine(); 

      if (type.equals("S") || type.equals("C")) { 
       String pin = sc.nextLine(); 
       double balance = sc.nextDouble(); 
       String name = sc.next(); 
       String address = sc.next(); 
       String date = sc.next(); 

       if (type.equals("S")) { 

        return new SavingAccount(pin, balance, name, address, date); 
       } 
       if (type.equals("C")) { 

        return new CheckingAccount(pin, balance, name, address, date); 
       } 
       throw new blankException("no account type given"); 
      } 
     } 
     sc.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return null; 
}//end file reader 

我的問題是,地址,掃描儀只需要弗里斯特「字」,在該文件中的地址是「1234房子驅動器「,地址只有1234.how我可以在哪裏地址是」1234房屋驅動器「

還,所有文件都是這種格式

type 
pin 
balance 
name 
address 
date 
+0

只需使用'Scanner#nextLine()'並分別讀取每行輸入。 –

回答

0

只要它使用.nextLine()。它將採用您輸入的所有內容。

+0

由於某種原因,它不起作用,可能有一個原因,爲什麼? – nafis

+0

啊沒關係,我似乎已經得到它的工作 – nafis

+0

那麼,它的完全工作? –