[英]Using input file to create an object and add that said object into an array, and have it print out the information on that array
這是我的代碼,它接收一個文本文件並創建一個Account對象,我遇到的問題是打印出銀行帳戶組中的帳戶對象
public static void main (String[] args)
{
try {
// Read data from a file into a Bank.
// Each line of the file has info for one account.
BankInterface myBank = readFromFile("BankAccounts.txt");
// Print all the data stored in the bank.
System.out.println (myBank);
} // end try
catch (IOException ioe) {
System.out.println("IOException in main: " + ioe.getMessage());
ioe.printStackTrace();
} // end catch
catch (Exception e) {
System.out.println("Exception in main: " + e.getMessage());
e.printStackTrace();
} // end catch
} // end main
/**
* readFromFile: **** INSERT COMMENTS ****
*
*/
public static BankInterface readFromFile (String fileName) throws IOException
{
// Creata a bank.
BankInterface myBank = new Bank("Bank");
// Open a file for reading.
Scanner inputSource = new Scanner (new File(fileName));
// while there are more tokens to read from the input source...
while (inputSource.hasNext()) {
// Read one line of input from the file into an Account object
Account acct = InputManager.readOneAccountFrom (inputSource);
// Store the account info in the bank.
//**** INSERT CODE TO ADD acct TO THE BANK ****
myBank.addAccount(acct);
} // end while
return myBank;
} // end readFromFile
當我運行主要我沒有得到帳戶打印出來時我得到:
Reading: name,id,balance
Exception in main: null
有人可以解釋為什么會這樣,以及如何解決它? 堆棧跟蹤:
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at InputManager.readOneAccountFrom(InputManager.java:30)
at ATM.readFromFile(ATM.java:48)
at ATM.main(ATM.java:15)
at __SHELL3.run(__SHELL3.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at bluej.runtime.ExecServer$3.run(ExecServer.java:730)
這是readOneAccountFrom方法:
public static Account readOneAccountFrom (Scanner inputSource)
{
// Read one line of account data into oneLine
System.out.println ("Reading: name,id,balance");
String oneLine = inputSource.nextLine();
// Parse line of account data, separated by commas.
Scanner lineTokenizer = new Scanner (oneLine);
lineTokenizer.useDelimiter (",");
// Get account data (i.e. name, accountNum and balance) from oneLine
String name = lineTokenizer.next ();
String accountNum = lineTokenizer.next ();
Money balance = new Money(lineTokenizer.nextLong());
// Create and return an Account object with the data read for one account.
Account oneAccount = new Account (name, accountNum, balance);
System.out.println ("Account read: " + oneAccount);
return oneAccount;
} // end readOneAccountFrom
即使你的評論說
//從文件中讀取一行輸入到Account對象
你沒有正確讀線。
您應該使用hasNextLine()和nextLine()從掃描程序中讀取一行。 如下所示:
while (inputSource.hasNextLine()) {
String line = inputSource.nextLine();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.