在GetArray方法中,生成的数组始终为空

发布于 2025-01-24 10:24:07 字数 876 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2025-01-31 10:24:07

您应该在第一个时间之后重新调整读者,读者没有更多的读数要读取,因此您需要再次将其分配给新的扫描仪(如果您需要重复使用Coins文件的相同数据,则在此处尚不清楚。 ):

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;

        // re-initialise the reader here :
        reader = new Scanner(coins);

        while (reader.hasNextDouble()) {
             jodCoins [i] = reader.nextDouble();
             i++;
             for (int j= 0; j < i; j++) {
                 System.out.println(jodCoins[j]);
             } 
        }
    }
    catch (Exception e) {
        // ????
    }

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 ) :

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;

        // re-initialise the reader here :
        reader = new Scanner(coins);

        while (reader.hasNextDouble()) {
             jodCoins [i] = reader.nextDouble();
             i++;
             for (int j= 0; j < i; j++) {
                 System.out.println(jodCoins[j]);
             } 
        }
    }
    catch (Exception e) {
        // ????
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文