我完全迷失在阵列中并需要帮助......这是该计划的最终目标....
在名为AccountArray.java的文件中,编写一个客户端程序(您的主方法),该程序从名为customers.txt的文件中读取。读取文件中的第一个数字并创建一个 具有该数量的元素的Account对象数组。使用“for”循环为从文件中读取的每行信息创建一个Account对象,并将其存储到数组的元素中
到目前为止,我所处的位置......主要关注的是FileNotFound异常错误....我在程序文件夹中保存了一个名为customers.txt的文件但是我需要以某种方式初始化它还是什么?
关于我在这个程序中做错的事情的任何其他输入都会被大大接受,我才开始学习这些东西。
public class AccountArray {
/**
* @param args
*/
public static void main(String[] args) {
List<Account> accountsArray = new ArrayList <Account>();
String name, accountnumber, balance;
Scanner diskScanner = new Scanner(new File("customers.txt"));
Scanner scanner= new Scanner ("customers.txt");
scanner.useDelimiter(" ");
int objects= scanner.nextInt();
Account[] accounts=new Account[objects];
while (objects>0){
name = scanner.nextLine();
accountnumber = scanner.nextLine();
balance = scanner.nextLine();
for(int i = 1; i < objects; i++) {
accountsArray.add(new Account(i, name, accountnumber, balance));
}
objects=objects-1;
System.out.println(name+ " " + accountnumber + " " + balance +"\n"); }// just for debugging
}
}
文件样本:
4
John Anderson
4565413
250.00
Louise Carter
2323472
1250.45
Paul Johnson
7267881
942.81
Sarah Wilson
0982377
311.26
答案 0 :(得分:1)
嗯,首先,你使用了错误的Scanner
对象:
Scanner diskScanner = new Scanner(new File("customers.txt")); // Scans through your file --Use this one
Scanner scanner= new Scanner ("customers.txt"); // Scans through the String "customers.txt" --Not helpful
要修复FileNotFound异常,您需要按照Freaky Thommi的建议将文件customers.txt移动到new File("customers.txt").getAbsoultePath();
输出的文件夹。
你还会遇到其他一些错误,但我会让你自己解决这些错误......
答案 1 :(得分:0)
这是否是eclipse的运行。如果是,则需要在项目根文件夹下包含此文件。您总是可以使用
找出绝对路径new File("customers.txt").getAbsoultePath();
将此打印到控制台,查看该位置是否存在文件