在GetArray方法中,生成的数组始终为空
public static void main(String[] args) throws FileNotFoundxception {
File usdCoins = new File("C:\\Users\\saif9\\OneDrive\\Desktop\\USD coins.txt");
getArray(usdCoins);
}
public static void getArray (File coins) {
try {
Scanner reader = new Scanner(coins);
int counter = 0;
while (reader.hasNextDouble()) {
System.out.print(reader.nextDouble() + " / ");
System.out.println(counter);
counter++;
}
double jodCoins[] = new double[counter];
int i = 0;
while (reader.hasNextDouble()) {
jodCoins [i] = reader.nextDouble();
i++;
for (int j= 0; j < i; j++) {
System.out.println(jodCoins[j]);
}
}
}
catch (Exception e) {
// ????
}
public static void main(String[] args) throws FileNotFoundxception {
File usdCoins = new File("C:\\Users\\saif9\\OneDrive\\Desktop\\USD coins.txt");
getArray(usdCoins);
}
public static void getArray (File coins) {
try {
Scanner reader = new Scanner(coins);
int counter = 0;
while (reader.hasNextDouble()) {
System.out.print(reader.nextDouble() + " / ");
System.out.println(counter);
counter++;
}
double jodCoins[] = new double[counter];
int i = 0;
while (reader.hasNextDouble()) {
jodCoins [i] = reader.nextDouble();
i++;
for (int j= 0; j < i; j++) {
System.out.println(jodCoins[j]);
}
}
}
catch (Exception e) {
// ????
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在第一个时间之后重新调整读者,读者没有更多的读数要读取,因此您需要再次将其分配给新的扫描仪(如果您需要重复使用Coins文件的相同数据,则在此处尚不清楚。 ):
You should re-initialise the reader ,after the first while , the reader dont have more double to read , so you need to assign it again to a new Scanner (entries are not clear here if you need to reuse the same data of coins file ) :